postman interface testing - Restful interface development and testing

After developing the interface, next we need to test the interface we developed. There are many methods of interface testing, and you can use interface tools or Python to test. In terms of tools, such as Postman or Jmeter we have learned before, Python script testing can use Requests + unittest to test.

Test idea
Functional test: add, delete, modify and check data
Abnormal test: unauthorized, abnormal parameters, etc.
Postman test
Using the test tool Postman test results are as follows:

user interface test
query all users

 create user

modify user

delete users 

Unauthorized testing

 

groups interface test

Query all groups data

Modify group data

 

delete groups

 

Requests+Unittest

apiCreate a new one under the directory , test_unittest.pyand the code is implemented as follows:

tests_unittest.py

 
import requests
import unittest
 
class UserTest(unittest.TestCase):
    def setUp(self):
        self.base_url='http://127.0.0.1:8000/users'
        self.auth=('51zxw','zxw20182018')
 
    def test_get_user(self):
        r=requests.get(self.base_url+'/1/',auth=self.auth)
        result=r.json()
 
        self.assertEqual(result['username'],'51zxw')
        self.assertEqual(result['email'],'[email protected]')
 
 
    def test_add_user(self):
        form_data={'username':'zxw222','email':'[email protected]','groups':'http://127.0.0.1:8000/groups/2/'}
        r=requests.post(self.base_url+'/',data=form_data,auth=self.auth)
        result=r.json()
 
        self.assertEqual(result['username'],'zxw222')
 
 
    def test_delete_user(self):
        r=requests.delete(self.base_url+'/11/',auth=self.auth)
 
        self.assertEqual(r.status_code,204)
 
    def test_update_user(self):
        form_data={'email':'[email protected]'}
        r=requests.patch(self.base_url+'/2/',auth=self.auth,data=form_data)
        result=r.json()
 
        self.assertEqual(result['email'],'[email protected]')
 
 
    def test_no_auth(self):
        r=requests.get(self.base_url)
        result=r.json()
 
        self.assertEqual(result['detail'],'Authentication credentials were not provided.')
 
class GroupTest(unittest.TestCase):
    def setUp(self):
        self.base_url='http://127.0.0.1:8000/groups'
        self.auth=('51zxw','zxw20182018')
 
    def test_group_developer(self):
        r=requests.get(self.base_url+'/7/',auth=self.auth)
        result=r.json()
 
        self.assertEqual(result['name'],'Developer')
 
    def test_add_group(self):
        form_data={'name':'Pm'}
        r=requests.post(self.base_url+'/',auth=self.auth,data=form_data)
        result=r.json()
 
        self.assertEqual(result['name'],'Pm')
 
    def test_update_group(self):
        form_data={'name':'Boss'}
        r=requests.patch(self.base_url+'/6/',auth=self.auth,data=form_data)
        result=r.json()
 
        self.assertEqual(result['name'],'Boss')
 
    def test_detele_group(self):
        r=requests.delete(self.base_url+'/6/',auth=self.auth)
 
        self.assertEqual(r.status_code,204)
 
 
if __name__ == '__main__':
    unittest.main()
 

Django comes with a test module

Open the file apiunder the directory testsand write the following test code

tests.py

 
from django.test import TestCase
import requests
 
# Create your tests here.
class UserTest(TestCase):
    def setUp(self):
        self.base_url='http://127.0.0.1:8000/users'
        self.auth=('51zxw','xxxxx')
 
    def test_get_user(self):
        r=requests.get(self.base_url+'/1/',auth=self.auth)
        result=r.json()
 
        self.assertEqual(result['username'],'51zxw')
        self.assertEqual(result['email'],'[email protected]')
 
    # @unittest.skip('skip add user')
    def test_add_user(self):
        form_data={'username':'zxw222','email':'[email protected]','groups':'http://127.0.0.1:8000/groups/2/'}
        r=requests.post(self.base_url+'/',data=form_data,auth=self.auth)
        result=r.json()
 
        self.assertEqual(result['username'],'zxw222')
 
    # @unittest.skip('skip test_delete_user')
    def test_delete_user(self):
        r=requests.delete(self.base_url+'/11/',auth=self.auth)
 
        self.assertEqual(r.status_code,204)
 
    def test_update_user(self):
        form_data={'email':'[email protected]'}
        r=requests.patch(self.base_url+'/2/',auth=self.auth,data=form_data)
        result=r.json()
 
        self.assertEqual(result['email'],'[email protected]')
 
    def test_user_already_exists(self):
        form_data = {'username': 'zxw222', 'email': '[email protected]', 'groups': 'http://127.0.0.1:8000/groups/2/'}
        r = requests.post(self.base_url + '/', data=form_data, auth=self.auth)
        result = r.json()
        #预期返回值:{"username":["A user with that username already exists."]}
        self.assertEqual(result['username'][0], 'A user with that username already exists.')
 
    def test_no_auth(self):
        r=requests.get(self.base_url)
        result=r.json()
 
        self.assertEqual(result['detail'],'Authentication credentials were not provided.')
 
class GroupTest(TestCase):
    def setUp(self):
        self.base_url='http://127.0.0.1:8000/groups'
        self.auth=('51zxw','xxxxxx')
 
    def test_group_developer(self):
        r=requests.get(self.base_url+'/3/',auth=self.auth)
        result=r.json()
 
        self.assertEqual(result['name'],'Pm')
 
    # @unittest.skip('skip test_add_group')
    def test_add_group(self):
        form_data={'name':'Leader'}
        r=requests.post(self.base_url+'/',auth=self.auth,data=form_data)
        result=r.json()
 
        self.assertEqual(result['name'],'Leader')
 
    def test_update_group(self):
        form_data={'name':'Boss'}
        r=requests.patch(self.base_url+'/6/',auth=self.auth,data=form_data)
        result=r.json()
 
        self.assertEqual(result['name'],'Boss')
 
    def test_detele_group(self):
        r=requests.delete(self.base_url+'/6/',auth=self.auth)
 
        self.assertEqual(r.status_code,204)
 
 

Operation method: Open cmd and use the following command to run:

D:\django_restful>python manage.py test
 

The above command is to test all use cases by default. If you want to test some use cases, you can use the following command:

Test the specified test class

D:\django_restful>python manage.py test api.tests.UserTest
 

Test a specific specific use case

D:\django_restful>python manage.py test api.tests.UserTest.test_get_user
 

Error related

1. No permission to write when migrating the database

File "C:\Users\jli75\AppData\Local\Programs\Python\Python37\lib\site-packages\MySQLdb\connections.py", line 280, in query
    _mysql.connection.query(self, query)
django.db.utils.InternalError: (7, "Error on rename of '.\\httprunnermanager\\#sql-1178_7.frm' to '.\\httprunnermanager\\djcelery_taskstate.frm' (Errcode: 13 - Permission denied)")

Reason: It may be that antivirus software solves this problem by preventing frm files from being modified. Resolve this issue by disabling on-access scanning in the antivirus threat protection advanced options and setting the antivirus to ignore these extensions

  1. The previous migration files were not cleared when migrating the databasemigrations
  File "C:\Users\jli75\AppData\Local\Programs\Python\Python37\lib\site-packages\MySQLdb\connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1050, "Table 'djcelery_crontabschedule' already exists")

Solution: migrationsJust delete the folder.

  1. setting configuration error
raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(6) NOT NULL)' at line 1"))
 

 Solution: Django2.1 no longer supports MySQL5.5, it must be version 5.6 or above. You can use the following command to view the current Mysql version

mysql -V
mysql  Ver 8.0.1-dmr for Win64 on x86_64 (MySQL Community Server (GPL))

 Friends who are doing the test can come in and communicate. The group has compiled a lot of learning materials and interview questions, project resumes, etc.... 

Guess you like

Origin blog.csdn.net/2301_77645573/article/details/131401975