postman interface testing-Restful interface development and testing

After developing the interface, we need to test the interface we developed. There are many methods for interface testing. You can use interface tools or Python to test. Tools such as Postman or Jmeter that we have learned before. Python script testing can use Requests + unittest to test.

Testing Ideas
  • Functional testing: addition, deletion, modification and query of data
  • Abnormal testing: unauthorized, abnormal parameters, etc.
Postman test

The test results using the testing tool Postman 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

Create a new test_unittest.py under the api directory. 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 test module

Open the tests file under the api directory and 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()
        #Expected return value: {"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)
 
 

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

  1. D:\django_restful>python manage.py test

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

Test the specified test class

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

Test a specific use case

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

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 the anti-virus software solves this problem by preventing modification of the frm file. 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 migrations were not cleared 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)
_mysql_exceptions.OperationalError: (1050, "Table 'djcelery_crontabschedule' already exists")

Solution: Delete the migrations 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 and 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))

Finally, I would like to thank everyone who read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!