Analysis
Interface is based on HTTP protocol, then plainly, is to initiate an HTTP request on the line, for Python is a piece of cake. It's easy to get the job done using requests directly.
Architecture
The whole frame is relatively small, involving a few things, as long as a few modules to clear the function of the line.
Above is the complete flow of an interface test. As long as the step-by-step walk down on the line, not difficult.
Data source
Data sources I use JSON to save, of course, the more extensive is to use Excel to save, with JSON to save is because the JSON is easier to use, lazy to read Excel, Python support for JSON is very friendly. Of course, this is a personal preference.
{ "TestID": "testcase004", "Method": "Post", " Title": "Push Message Alone", "Desc": "Push message individually", "URL": "http ://xxx.xxx.xxx.xx ", " Inputarg ": { " action ":" 44803 ", " account ":" 1865998xxxx ", " UniqueID ":" 00d7c889-06a0-426e-bab1-5741a1192038 ", " title ":" Test Test ", " Summary ":" Bean Bean ", " message ":" 12345 ", "Msgtype": "+", " menuid": "203" }, "Result": { "Errorno": "0"} }
As shown in the code above, it can be adjusted according to the individual's business needs.
Send Request
Sending the request is simple, using the requests module, and then reading the sent parameters, post, get, or other from the JSON. To generate the test report, the data sent needs to be recorded, and I choose to use txt text as the container for the record.
f = File ("Case.json") TestData = Json.load (f) f.close () def senddata (TestData, num): payload = {} # Get send parameters from JSON C2/>for x in testdata[num][' Inputarg '].items (): payload[x[0] [] = x[1] with open (' Leftside.txt ', ' A + ') as F: F.write (testdata[num][' TestID ') f.write ('-') f.write (testdata[num][' Title ']) f.write (' n ') # Send request data = Requests.get (testdata[num][' Url '], params=payload) r = Data.json ()
Accept return
Since we need to generate a test report, we need to store the returned data first, can choose to use the database storage, but I think the database storage is too cumbersome, as long as the txt text as a storage container.
With open (' Rightside.txt ', ' A + ') as RS: rs.write (' send data ') rs.write (' | ') Rs.write (' title: ' +testdata[num][' title ')] rs.write (' | ') Rs.write (' Sending method: ' +testdata[num][' method ']) rs.write (' | ') Rs.write (' Case description: ' +testdata[num][' Desc ']) rs.write (' | ') Rs.write (' Send address: ' +testdata[num][' Url ']) rs.write (' | ') Rs.write (' Send parameters: ' +str (payload). Decode ("Unicode-escape"). Encode ("Utf-8"). Replace ("U '", "'")) rs.write (' | ') Rs.write (testdata[num][' TestID ') rs.write (' n ')
Result determination
The result is that I am using all equal to judgment. Because our interface only needs to do this, if necessary, can be written as a regular judgment.
With open (' Result.txt ', ' A + ') as rst: rst.write (' return data ') rst.write (' | ') For x, y in R.items (): rst.write (': '. Join ([x, y]) rst.write (' | ' ) # write test result try: if CMP (r, testdata[num][' Result ') = = 0: rst.write (' pass ') else: rst.write (' fail ' ) except Exception: rst.write (' No except result ') rst.write (' n ')
I have 3 results here, success, failure or no result. The setting of the result depends on your definition.
Generate Test Reports
The test report is a play, because I send the data, return data and results are stored in txt text, then each use of a + mode is added, will make the results more and more, and check up very painful.
My approach is to use Python to read the data in the TXT text after each test, and then use Django to dynamically generate a result, and then use requests to crawl the page and save it in the report folder.
Web Reports
I'm not going to say much about the Django approach, and I've got a whole series of articles in my blog. We need to open the 3 TXT file previously recorded in the views file, and then do some data processing, back to the front end, the front end with bootstrap to render, you can generate a more beautiful test report.
def index (request): Rightside = [] result = [] Rst_data = [] Leftside = [] passed = 0 Fail = 0 noresult = 0 with O Pen (OS.GETCWD () + '/porttest/leftside.txt ') as Ls:for x in Ls.readlines (): Lf_data = {' Code ': X.strip (). s Plit ('-') [0], ' title ': X.strip (). Split ('-') [1]} leftside.append (Lf_data) with open (OS.GETCWD () + '/port Test/rightside.txt ') as Rs:for x in Rs.readlines (): row = X.strip (). Split (' | ') Rs_data = {"FSSJ": row[0], "CSBT": row[1], "FSFS": row[2], "alms": row[3], "Fsdz": Row [4], "FSCS": row[5], ' TestID ': row[6]} rightside.append (Rs_data) with open (OS.GETCWD () + '/portt Est/result.txt ') as Rst:for x in Rst.readlines (): row = X.strip (). Split (' | ') If Row[len (Row)-1] = = ' fail ': Fail + = 1 elif row[len (Row)-1] = = ' Pass ': Passed + = 1 Elif row[len (r OW)-1] = = ' No except result ': Noresult + = 1 Rs_data = [] for y in ROw:rs_data.append (y) result.append (rs_data) for a, b in zip (rightside, result): data = {"SendData": A, "DealData": B, "result": B[len (b)-1]} rst_data.append (data) return render (Request, ' porttest/index.h Tml ', {"leftside": Leftside, "Rst_data": Rst_data, "pass": Passed, "Fail": Fail, "Noresult": Noresult})
Basically are some very basic knowledge, string segmentation and so on. Here for the convenience of data processing, in the acquisition of storage time to follow a certain format to store, views of the method is easy to do processing.
The front-end code is as follows:
<! DOCTYPE html>Test report
?
At last
It is easy to write a tool in Python, and it is important to make it easier to meet the needs of your actual work. If you want to do a complete interface test, try to use a proven tool.
Long press QR Code recognition concern, your support is our greatest power.
Public Number: Testing DreamWorks
QQ Group: 300897805
Example of interface test framework for Python