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)