Python Interface Test (v)

Source: Internet
Author: User

V: Using Python to organize an interface test case

Interface testing is actually a few steps.

    1. get the interface. URL Address
    2. See how the interface is sent
    3. Add request header, request body
    4. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.