V: Using Python to organize an interface test case
Interface testing is actually a few steps.
- get the interface. URL Address
- See how the interface is sent
- Add request header, request body
- Send to view returned results, verify the returned results are correct
Understanding the test steps of the interface test, we can then organize our code.
Import requests# Interface Urlurl = "HTTP://FANYI.BAIDU.COM/V2TRANSAPI" # interface parameter params = {"From ": "en", "to ": "zh", "Query": "Test"}r = Requests.request ("Post", URL, params=params) # Print return result print (R.text) # to make the results look clearer, I fetch the translated field import Jsond = Json.loads (r.text) print (d[' liju_result ' [' tag '])
Results:
[' Test ', ' quiz ', ' Test ', ' assay ', ' test ', ' tested ', ' subject to test ', ' tested ', ' measured results ']
(The result is very long, intercept the last)
Modify the parameter request again;
Import Requestsurl = "Http://fanyi.baidu.com/v2transapi" params = { "from": "en", "to": "zh", "query": " Study "#}r = Requests.request (" Post ", URL, params=params) import Jsond = Json.loads (r.text) print (d[' liju_result ' [' tag ') ])
Results:
[' Learning ', ' research ', ' subject ', ' Den ', ' conclusion ', ' consider ', ' meditate ', ' endeavor ', ' come up ']
PS: Let's take a look at the tool to test the interface
Next, let's introduce the UnitTest Library optimization code:
Import Requests,unittest,jsonclass Testbaiduapi (unittest. TestCase): def setUp (self): url = "Http://fanyi.baidu.com/v2transapi" def Testzhen (self): params = { "from": "en", "to": "zh", "query": "Study" # } url = "Http://fanyi.baidu.com/v2transapi" r = Requests.request ("Post", URL, params=params) r=json.loads (r.text) assert u ' learning ' in r[' Liju_result '] [ ' Tag '] def testzhen1 (self): params = { "from": "en", "to": "H", "query": "Stud" #} url = "Http://fanyi.baidu.com/v2transapi" r = Requests.request ("Post", URL, params=params) r=json.loads (r.text assert u ' learn ' in r[' liju_result ' [' tag '] def tearDown (self): passif __name__== ' __main__ ': Unittest.main (verbosity=2)
Results:
In Python, a htmltestrunner.py is provided to generate a test report, which is downloaded and placed directly into the Lib directory of the Python installation file.
Import Requests,unittest,json,htmltestrunnerclass Testbaiduapi (unittest. TestCase): def setUp (self): url = "Http://fanyi.baidu.com/v2transapi" def Testzhen (self): params = { "From": "en", "to": "zh", "Query": "Study" #} URL = "Http://fanyi.baidu.com/v2transapi" r = Requests.request ("Post", URL, params=params) r=json.loads (R.text) Assert u ' learning ' in r[' liju_result ' [' t Ag '] def testzhen2 (self): params = {"from": "en", "to": "H", "Query": "Stud" #} U RL = "Http://fanyi.baidu.com/v2transapi" r = Requests.request ("Post", URL, params=params) r=json.loads (R.tex T) assert u ' learn ' in r[' liju_result ' [' tag '] def tearDown (self): passif __name__== ' __main__ ': report_dir = R ' s.html ' re_open= open (Report_dir, ' WB ') suite=unittest. Testloader (). Loadtestsfromtestcase (TESTBAIDUAPI) Runner=htmltestrunner.htmltestrunner (Stream=re_open, TI Tle=u ' Baidu translationAPI interface Test Report ', Description=u ' Baidu Translation API Interface Test Details ') Runner.run (suite)
After execution, a test report is generated in the current directory, as follows:
The other interface test method is also this idea,
Author's message:
The way forward we are full of confusion,
Every step of the way we will have a harvest.
At the foot of the road, we cannot decide where we come from, but we can try to change our future.
Farewell to yesterday's failure of their own, hard work today, the achievement of a better tomorrow
PADF Document sharing: Https://pan.baidu.com/s/1gfuXkpP
Python Interface Test (v)