Python+unittest+requests Implements interface automation

Source: Internet
Author: User

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

    1. Report Store test Results
    2. Test_case Storing test Cases
    3. 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

Related Article

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.