Objective:
Requests Introduction
Requests is an HTTP library that uses Apache2 Licensed licenses. Written in Python, really for the sake of mankind.
The urllib2 module in the Python Standard library provides most of the HTTP functionality you need, but its API is a bit behind. It was created for another era, another internet. It requires a huge amount of work, even covering a variety of methods, to accomplish the simplest task.
In short, we suggest you use requests bar.
Requests's Official document: http://cn.python-requests.org/zh_CN/latest/
Install requests by using the following method
PIP Install requests
Example implementation steps:
1. Use UnitTest to write each interface as a test script
2. A test script contains an interface, but can contain multiple test cases (that is, each interface needs to be validated in a variety of cases, the interface test case name has been test)
3. Using discover (), this method automatically finds the test case file based on the test catalog match and assembles the found test into the test suite, so you can perform the discover directly from the run () method, greatly simplifying the search and execution of the test case
4. Generate test reports with Htmltestrunner
Document structure: The following three files are available under Project projects file
- Report Store test Results
- Test_case Storing test Cases
- run_test_case.py executing test Case Files
Case:
1.get Interface test Case source code
# Coding:utf-8import Requestsimport unittestclass get_request (unittest. TestCase): def setUp (self): self.get_url = ' https://www.baidu.com/' def test_post_01 (self): url= Self.get_url r = requests.get (URL) print r.text def tearDown (self): passif __name__ = = "__main__": Unittest.main ()
2.post Interface test Case source code
# coding:utf-8import Requestsimport jsonimport unittestclass post_request (unittest. TestCase): def setUp (self): Self.post_url = ' ... ' #根据实际接口, fill in the Self.header = {' ... '} #根据实际内容, Fill in the Def test_post_01 (self): "" "" "" "" "" "" "" url=self.post_url Header = Self.header Data = {""}# According to the actual content, fill in the #将data序列化为json格式数据, pass to the data parameter r = Requests.post (URL, data=json.dumps (data), Headers=header) print R.text def test_post_02 (self): "" " Exception Data" "" url=self.post_url Header = Self.header data = {""} #根据实际内容, fill in r = Requests.post (URL, data=json.dumps (data), Headers=header) print R.text def tearDown (self): passif __name__ = = "__main__": Unittest.main ()
3. Execute the test case source code
# coding=utf-8import Unittestimport htmltestrunnerimport time# relative path Test_dir = './test_case ' Test_dir1 = './report ' Discover = Unittest.defaultTestLoader.discover (Test_dir, pattern= ' test*.py ') # defines a report with the current test time to prevent the previous report from being overwritten now = Time.strftime ("%y-%m-%d%h_%m_%s") filename = test_dir1 + '/' + now + ' result.html ' # binary open, ready to write to file FP = files (filename, ' WB ') # Define test Report Runner = Htmltestrunner.htmltestrunner (STREAM=FP, title=u ' Test report ', Description=u ' use case execution ') Runner.run (Discover)
Python+unittest+requests Implements interface automation