Python-based interface test Learning note one (fledgling)

Source: Internet
Author: User

Write your blog notes for the first time, and talk about the interface automation tests you've learned recently. Online access to the relevant information, and finally decided to use the Python language to write interface testing, using Python's third-party library requests. Although the urllib2 module in the standard library of Python itself provides most of the HTTP functionality you need. But requests is better with simplicity. Start learning decisively.

Getting started, the code is relatively simple

Import Requestsimport unittestclass apitest (unittest. TestCase):d EF setUp (self): self.base_url= ' http://httpbin.org/get ' self.list={' key1 ': ' value1 ', ' key2 ': ' Valus2 '}def TearDown (self):    passdef Test_api (self): Response=requests.get (self.base_url,params=self.list) Self.assertequal (response.status_code,200)    if __name__== ' __main__ ': Unittest.main ()

First of all, import the corresponding requests module, it is our HTTP request and other related functions of the key, need to install, at the command line input

$ pip Install requests

Then import the UnitTest framework, which is a Python unit test framework, equivalent to the Java junit Framework.

Apitest inherits from Unittest.testcase and is a test case. Overrides the Setup () method for environment initialization, such as establishing a database connection in Setup () and some initialization, clearing the data generated in the database in Teardown (), and then closing the connection. Here the Setup () method declares the variables, URLs, and submitted parameters. It also defines a method that starts with test, and each method that starts with test constructs a TestCase object for it. Using requests to send network requests is straightforward. A GET Request: Response=requests.get (self.base_url,params=self.list), the two parameters in the Get method are the URL and the passed parameters, respectively. The requested URL is actually:http://httpbin.org/get?key2=value2&key1=value1. After adding an assertion, the user determines whether the response is the same as expected, and here is whether the response status code equals 200.

Python-based interface test Learning note one (fledgling)

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.