Python + requests + unittest API test, requestsunittest

Source: Internet
Author: User

Python + requests + unittest API test, requestsunittest

The Black Bear searched for information related to the interface test on the Internet. Most of them focused on maintaining the use case in a text or table in the form of data-driven. They did not explain how to generate the desired use case,

Problem:

When testing interfaces, for example, parameters a, B, and c, I need to test the parameter first. There are (not transmitted, null, integer, floating point, String, object, too short, too long, SQL Injection) one of these situations is a use case. At the same time, ensure that the test of B and c is correct and that a is not affected by the errors of parameters B and c.

Solution:

Parameters that comply with the interface specifications can be filled in manually or prepared in the code library. Parameters that do not comply with the specifications (do not pass, null, integer, floating point, String, object, too short, too long, SQL injection) can also be used repeatedly as constants in the library.

Main functions:

1. Sort api parameters into dict to facilitate combination of parameters to generate Use Cases

2. Execute the generated use cases cyclically.

3. encapsulate some code for ease of use and maintenance

 

Source code analysis:

The canshuxinxi. py file is used to store api information. It is stored in dict format, so that you can obtain the information of API_ALL ['logon interface'] [url] in this way. It looks intuitive and you know which interface to obtain.

1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # @ Time: 4 # canshuxinxi. py 5 6 # interface information 7 API_ALL = {8 'logon interface': {9 'number': '1', 10' url': 'http: // www.baidu.com ', 11 'leixing': 'post', 12 'head': {13 'A': 'bb', 14 'cc': 'dd', 15 }, 16 'canonical': {17 'username': 'wbfxs001', 18 'Password': '1111qq', 19 'Grant _ type': 'Password', 20 }, 21 'qiwang': {22 'code':, 23 'name': 'wbfxs001', 24}, 25}, 26 27 'exit interface': {28 'number ': '1', 29 'url': 'http: // www.baidu.com ', 30 'leixing': 'get', 31 'canshu': {32 'username ': 'wbfxs001', 33 'Password': '111111qq', 34 'Grant _ type': 'Password', 35} 36} 37}

 

Changliang. the py file is used to store unconventional parameters (which may cause interface response exceptions). Likewise, it is stored in dict for easy maintenance. For example, to add new SQL Injection code segments, you can directly add them later.

1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # @ Time: 4 # changliang. py 5 6 # common parameters are not passed. They are null, integer, floating point, String, object, too short, too long, SQL Injection 7 objects1 = 'xxxx8objects2 = 'sss' 9 10 ZHCS = {11' blank ': [''], 12' integer': [10, 23, 44, 88, 99], 13 'floating point ': [1.11, 2.342,-1.03], 14 'string': ['aaa', 'bbbbb', 'cccccc ', 'ddddd'], 15 'object': [objects1, objects2], 16' too short ': ['1', '0'], 17' ultra long ': ['20140901'], 18 'SQL injection': ['; and 1 = 1; and 1 = 2', "; and (select count (*) from sysobjects)> 0 mssql ","; and 1 = (select IS_SRVROLEMEMBER ('sysadmin'); -- "], 19}

 

 

# Gongju. py is used as a tool class. The following methods are encapsulated for convenient calling. Parameters are combined to produce dict parameters of different combinations and save the dict parameters to the list for convenient use.
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # @ Time: 4 # gongju. py 5 6 # generate parameters of different combinations 7 8 class gj (): 9 10 def listall( self, csTrue, csFalse): 11 fzgcs = [] # Get the key of cycanshu, put all informal parameters in a list. 12 listall = [] # Save the parameter dict as list13 zhcs = dict (csTrue) 14 listall. append (csTrue) 15 aaa = list (csFalse. keys () 16 for I in aaa: 17 bbb = csFalse [I] # obtain the specific parameter list18 for k in bbb: 19 fzgcs. append (k) # Every parameter is added to the fzgcs list 20 21 zhcskey = list (zhcs. keys () # Get the parameter 22 for I in zhcskey: 23 a = zhcs [I] # retain the original parameter value, after replacement, restore the correct parameter 24 for k in fzgcs: 25 zhcs [I] = k26 listall. append (str (zhcs) 27 # restore the correct parameter after loop completion 28 zhcs [I] = a29 return listall

 

 

The jiaoben. py file is used as the script class to cyclically execute the combined parameters and carry them into the combined parameter requests in turn. (Only request and print response information can be added to the response result assertions)
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # @ Time: 4 # jiaoben. py 5 6 from changliang import ZHCS 7 from canshuxinxi import API_ALL 8 from gongju import gj 9 import requests10 # script class. Combine the tool parameters to request 11 gj = gj () 12 def jball (): 13 apikeys = API_ALL.keys () 14 print (apikeys) 15 for key in apikeys: 16 apiname = key17 url = API_ALL [key] ['url'] 18 number = API_ALL [key] ['number'] 19 leixin = API_ALL [key] ['leixing'] 20 canshus = gj. listall( API_ALL [key] ['canonical'], ZHCS) 21 if leixin = 'post': 22 print ("======" + "api Name: "+ apiname +" ======= ") 23 for cs in canshus: 24 mp = requests. post (url = url, data = cs) 25 fhcode = str (mp. status_code) 26 xysj = str (mp. elapsed. microseconds) 27 print ("= response = api NO.:" + number + "response code:" + fhcode + "response time:" + xysj) 28 if leixin = 'get': 29 print ("=" + "api name:" + apiname + "= ") 30 for cs in canshus: 31 mp = requests. get (url = url, data = cs) 32 fhcode = str (mp. status_code) 33 xysj = str (mp. elapsed. microseconds) 34 print ("= response = api NO.:" + number + "response code:" + fhcode + "response time:" + xysj) 35 jball ()

 

Tesone. as an example execution file, The py file is familiar with the unittest framework and has a clear understanding of its principles. The black bear is mainly used to control script execution. Combined with the unittest framework, it facilitates subsequent extension.
1 #! /Usr/bin/env python 2 #-*-coding: UTF-8-*-3 # @ Time: 4 # tesone. py 5 6 import requests 7 import unittest 8 import time 9 from jiaoben import jball10 class testclassone (unittest. testCase): 11 def setUp (self): 12 print (111) 13 pass14 def test_1 (self): 15 jball () # Run the script 16 pass17 def tearDown (self ): 18 print (333) 19 pass20 21 22 if _ name _ = '_ main _': 23 unittest. main ()

Attached to the execution of the use case:

 

Conclusion:

There are a lot of things involved in the interface test, and I can only explore and learn at a step by step. During this period, I would like to thank @ me for my careful guidance and give me great help to the black bear. Thank you! At the same timeYou are welcome to join us. Black Bear QQ: 915069792

 

Why?

Some people like to create the world. They are programmers.

Some people like to save the world. They are testers.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.