happiness
---xingyunpi
Automation Test plays an important role in our test work. Now I will record my work related to automation test, using Python to run REST API. Until now, I have design two kinds of test scenarios: parameter testing for each REST API and regression test for a project.
Overview:
1. define a tool file: tool.py
Des: This file includes some common methods. These methods can be called once needed. For example WriteFile(write the execution results to log files), Compare(to check if some str is in another one),readPars(read parameters from a ini file), setPars(set value for variable in ini file),ex(stop the execution). Take Compare() as an example:
def compare(**str):
‘‘‘
compare the string
uncertain parameters
e.g.compare(a=("sub","string"),b=("a","acds"),c=("a","ab")) or compare(a=("sub","string"),b=("a","acds"))
return "no"
return "yes"
‘‘‘
#print str
for x in str:
sub = str[x][0]
string = str[x][1]
if sub not in string:
return "no"
return "yes"
2. define a persistent class: base.py
Des: As we know, there are four main methods, POST/PUT/GET/DELETE, in REST API. And I can use CURL cmd to call REST API. Now I will list the common part for the for methods.
1) POST: curl -i -k -X POST -d $BODY -H "Content-Type:application/json" $URL
2) PUT: curl -i -k -X PUT -d $BODY -H "authToken:$TOKEN $URL
3)GET: curl -i -k -H "Content-Type:application/json" -H "authToken::$TOKEN $URL
4)DELETE:curl -i -k -X DELETE -H "authToken:$TOKEN $URL
So, I can extract four base methods to support the REST API application. Every method has different parameters to implement different functions. For example:
def createObjects(value,header1,header2,ip,resulttofilename,description): ‘‘‘ create the object: including:create LDAP default settings,create a tenant,create a account :param value: body data :param header1: request header :param header2: authToken :param ip: the URL of rest API :param resulttofilename: the file to record run result :return x: cmd result ‘‘‘ cmd = "curl -i -k -X POST -d ‘" + value + "‘ -H \""+ header1 +"\" -H \"" + header2 + "\" "+ip print cmd p = os.popen(cmd) x = p.read() tool.writeFiles(resulttofilename,"++++++++++++++++++++\n"+cmd+"\n"+"===description==="+"\n"+description+"\n" + "===result==="+"\n"+x) #write the result to log file return x
3. define a class for every REST API.
If I want to test a POST REST API, the validity of every parameter is the first thing we need to check, and in order to separate functional and parameter testing, I define one class for every API.
post_tenant.py:
import base import os,re,time ‘‘‘ test the GET rest ‘‘‘ file = open("post_tenant_info.txt") i = 0 #pass j = 0 #fail t = 0 for line in file: ‘‘‘ t = t + 1 if t%10 == 0: time.sleep(10) ‘‘‘ line = line.strip(‘\n‘) array = line.split("|") result_t = array[4] # right result result = base.createObjects(array[0],array[1],array[2],array[3],"log/post_tenant_log.txt",array[5]) #write the results into login_log.txt if result_t not in result: #run fail j = j + 1 base.writeFiles("log/post_tenant_res.txt",line + "===FAIL\n") else: # pass i = i + 1 base.writeFiles("log/post_tenant_res.txt",line + "===PASS\n") base.writeFiles("log/post_tenant_res.txt","pass:"+str(i)+"\nfail:"+str(j)+"\n") #write the run results to log
post_tenant_info.txt:
{"name":"aaa","domainName":"aaa","authType":"aaa"}|Content-Type:application/json|authToken:aaa|https://url/scosmgmtsvr/rest/aaa|200 OK|success
In post_tenant_info.txt, I can give different keywords and values to test this REST API.
4. define a regression test, including all REST APIs.
1) define a class for which can be regarded as an object. Such as Users, tenants, accounts,longrunTask, meteringData... And they have different methods. Users.py includes GET,POST. tenants.py includes GET/POST/PUT/DELETE. .........
2)design different cases. In every case, we can instance objects and call their methods to complete different functions.
For example:
c=c+1 s=0 tool.writeFiles(RESULT_FILE, "################################\n#case "+str(c)+" : create tenant\n################################") create_tenant_data = "{\"name\":\"aaa\",\"domainName\":\""+domain_02+"\",\"quota\":\"1024000\",\"authType\":\"aaa\"}" s=s+1 result = tes.post(create_tenant_data, TENANT_IP,"case "+str(c)+"-script "+str(s)) if tool.compare(a=("201 Created",result))=="yes": tool.writeFiles(RESULT_FILE, "case "+str(c)+"-script "+str(s)+" :create tenant 02,httpbasic type - pass") else: tool.writeFiles(RESULT_FILE, "case "+str(c)+"-script "+str(s)+" :create tenant 02,httpbasic type - fail") tool.ex("create tenant failed")5. Do Code Optimization.
1) Logs can be readable and easy to understand.
2) Reduce the dependence of each case
3) Provide a config file to help users to use their own environment, e.g. IP,UserName,Password...
Over.