Black bears on the internet to find the next interface test-related data, most of the focus is in a data-driven form, the use case is maintained in a text or a table, and does not explain how to generate the desired use case,
Problem:
Test the interface, such as parameter a,b,c, I want to first test a parameter, there is (not pass, empty, shape, floating point, String, object, too short, super long, SQL injection) These situations, one case is a use case, and to ensure that the b,c is correct, to ensure that a test is not affected by the B,c parameter error
Solution Ideas:
Parameters that conform to the interface specification can be filled in manually or prepared in the code base. Those parameters that do not conform to the specification (not passed, empty, shaped, floating point, String, object, too short, extra long, SQL injection) can also be ready to be reused as constants in the library
The main implementation of the function points:
1.api parameters are collated into dict to facilitate combination of parameter generation use cases
2. Loop execution of the generated use cases
3. Encapsulating a little code for ease of use and maintenance
SOURCE Analysis:
The canshuxinxi.py file is used to store API interface information. In the form of dict storage, so that you can api_all[' login interface '][url] this way to fetch, it seems more intuitive, know which interface to obtain the part of the information.
#!/usr/bin/env python#-*-coding:utf-8-*-# @Time: 2017-06-09 14:09# canshuxinxi.py# Interface Information Api_all = {' Login connection Port ': {' number ': ' 1 ', ' url ': ' http://www.baidu.com ', ' leixing ': ' Post ', ' head ': {' AA ': ' BB ', ' CC ': ' DD ',}, ' Cansh U ': {' username ': ' Wbfxs001 ', ' Password ': ' 1 11111Qq ', ' grant_type ': ' Password ',}, ' Qiwang ': {' code ': 200, ' Name ': ' Wbfxs001 ',},}, ' Exit interface ': { ' Number ': '1 ', ' url ': ' http://www.baidu.com ', ' leixing ': ' Get ', ' Canshu ': {' username ': ' Wbfxs001 ', ' Password ': ' 111111Qq ', ' grant_type ': ' Password ', } }}
changliang.py files are used to store unconventional (which may cause the interface to respond to exceptions) parameters, the same is also stored in the dict, convenient maintenance, such as to add a new SQL injection code snippet, can be added directly later
#!/usr/bin/env python#-*-coding:utf-8-*-# @Time : 2017-06-09 14:09# changliang.py# Common parameters do not pass, empty, reshape, float, string, object, too short , ultra-long, SQL injection objects1 = ' xxxx ' objects2 = ' ssss ' ZHCs = { ' is null ': ['], ' shaping ': [Ten, 1.11, 2.342, [], ', ', ' floating ': [ , -1.03], ' string ': [' aaaa ', ' bbbb ', ' cccc ', ' dddd '], ' object ': [Objects1, Objects2], ' Too short ': [' 1 ', ' 0 '], ' Extra Long ': [' 11111111111111111111111111111111111111111111111 '], ' SQL injection ': ['; and 1=1; and 1=2 ', '; and (select COUNT (*) From sysobjects) >0 mssql ","; and 1= (select Is_srvrolemember (' sysadmin ');--"], }
# gongju.py as a tool class, the following method is encapsulated for easy invocation. The parameter is implemented to combine the parameters of different combinations of dict types, and the dict parameters are saved to the list for easy access.
#!/usr/bin/env python#-*-coding:utf-8-*-# @Time : 2017-06-09 14:11# gongju.py# generate different combinations of parameters class GJ (): def listal LS (self, cstrue, csfalse): Fzgcs = [] # Get Cycanshu key, place all the informal parameters in a list listall = [] # Save parameter Dict to List ZHCs = Dict (cstrue) listall.append (cstrue) AAA = List (Csfalse.keys ()) for I in AAA: BBB = Csfalse[i] # Get specific parameters list for K in BBB: fzgcs.append (k) # Facilitate each parameter join Fzgcs list Zhcskey = List ( Zhcs.keys ()) # Get the parameters that will be combined for I in Zhcskey: a = zhcs[i] # retain the original parameter values, after replacing the correct parameters for the K in Fzgcs : zhcs[i] = k listall.append (str (ZHCS)) # Restore correct parameters after loop zhcs[i] = a return listall
The jiaoben.py file is used as a script class to loop through the assembled parameters and bring in the combined parameter request in turn. (Only request and print response information can be added to the response result assertion)
#!/usr/bin/env python#-*-coding:utf-8-*-# @Time: 2017-06-09 14:22# jiaoben.pyfrom changliang import ZHCSfrom Cansh Uxinxi Import api_allfrom Gongju import gjimport requests# script class, combining tool parameters Request GJ = GJ () def jball (): Apikeys = Api_all.keys () Print (Apikeys) for key in apikeys:apiname = key url = api_all[key][' url '] Number = Api_all[ke y][' number '] leixin = api_all[key][' leixing '] Canshus = gj.listalls (api_all[key][' Canshu '), ZHCs) if Leixin = = ' Post ': Print ("=======" + "API Name:" +apiname+ "=======") for CS in CANSHUS:MP = Requests.post (Url=url, data=cs) Fhcode = str (mp.status_code) XYSJ = str (mp.elapsed.microse conds) Print ("= Response =api Number:" +number+ "Response code:" +fhcode+ "Response Time:" +XYSJ ") if leixin = = ' Get ': P Rint ("=======" + "API Name:" +apiname+ "=======") for cs in canshus:mp = Requests.get (Url=url, Data=c s) fhcode = str (mp.status_code) XYSJ = str (mp.elapsed.microseconds) print ("= Response =api Number:" +number+ "response Co de: "+fhcode+" Response Time: "+XYSJ" Jball ()
tesone.py file As a use case execution file, familiar with the UnitTest framework is clear of the principle, do not long introduction, black bear is mainly used to control the execution of scripts, combined with the UnitTest framework, convenient follow-up extension.
#!/usr/bin/env python#-*-coding:utf-8-*-# @Time : 2017-06-09 8:53# tesone.pyimport requestsimport unittestimport t Imefrom jiaoben Import Jballclass testclassone (unittest. TestCase): def setUp (self): print (111) Pass def test_1 (self): jball () # Execute script pass def TearDown (self): print (333) passif __name__ = = ' __main__ ': unittest.main ()
Finally attach the use case after the execution: