Use Python to run REST API Automation Test

來源:互聯網
上載者:User

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 stringuncertain parameterse.g.compare(a=("sub","string"),b=("a","acds"),c=("a","ab")) or compare(a=("sub","string"),b=("a","acds"))return "no"return "yes"'''#print strfor 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  + "\" "+ipprint cmdp = os.popen(cmd)x = p.read()tool.writeFiles(resulttofilename,"++++++++++++++++++++\n"+cmd+"\n"+"===description==="+"\n"+description+"\n" + "===result==="+"\n"+x) #write the result to log filereturn 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 baseimport os,re,time'''test the GET rest'''file = open("post_tenant_info.txt")i = 0 #passj = 0 #failt = 0for line in file:'''t = t + 1if t%10 == 0:time.sleep(10)'''line = line.strip('\n')array = line.split("|")result_t = array[4] # right resultresult = base.createObjects(array[0],array[1],array[2],array[3],"log/post_tenant_log.txt",array[5])#write the results into login_log.txtif result_t not in result: #run failj = j + 1base.writeFiles("log/post_tenant_res.txt",line + "===FAIL\n")else: # passi = i + 1base.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.


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.