Python3+requests+unittest: Interface Automation Test (i)

Source: Internet
Author: User

Simply introduce the implementation logic of the framework, referring to the git address of the code:

Https://github.com/zhangying123456/python_unittest_interface

1. Environmental preparedness

Python3 + pycharm Editor

2. Frame Catalogue Display

(This set of code is only simple to get started, interested can continue to improve later)

(1) run.py The main running file, after the operation can generate the corresponding test report, and sent in the form of mail;

(2) Report folder to store test results reports;

(3) Unit_test folder is a storage test case (demo.py and test_unittest.py use case usage introduction, the actual project can follow different modules to create a new Python package, to store different modules of the interface use case);

(4) Util to test interface-related methods encapsulation: htmltestrunner.py Generate test report encapsulation, send_mail.py send message encapsulation, test_get_post.py interface request type encapsulation.

3. Code implementation Instructions

(1) Get, Post interface Request Example introduction

The "1" requests.get (), requests.post () usage can refer to the documentation: https://www.cnblogs.com/shapeL/p/9037035.html

"2" request after the return of the results asserted: assertequal (), and so on, the assertion method is many, can be online query

(2) In practice, some parameters or methods must be described before executing the use case, which involves the use of Setup, TearDown, SetupClass, Teardownclass

"1" setup (): Each test function runs before running; Teardown (): Executes after each test function is run; SetupClass (): Must use @classmethod adorner, all test runs before run; Teardownclass () : You must use the @classmethod adorner to run once after all test runs

"2" can also be skipped for some use cases: Unittest.skip (), specific use of the Internet query

(3) If each interface request is used Requests/get, POST request is cumbersome, we can encapsulate the method, the interface request is called directly

(4) After the test case is written, the main function run.py directly using the UnitTest module to execute all the use cases

"1" unittest module: TestSuite Multiple test cases are assembled together; Testrunner is to execute the test case, the results of the test are saved to the TestResult instance, including how many test cases were run, how many succeeded, How much information has failed (unittest. Texttestrunner (). Run (Suite)

"2" suite.addtests () and suite.addtest () can all be achieved

#2 ways to use: The first type of suite.addtest ()Suite.addtest (Case ('test_case01')) Suite.addtest (case ('TEST_CASE02')) Suite.addtest (Test ('test_01')) Suite.addtest (Test ('test_02'))#2 Usage: second type of suite.addtests ()Suite.addtests (Map (Test, ["test_01","test_02"]) suite.addtests (map (case, ["test_case01","TEST_CASE02"]))

"3" Testrunner test results output to the console, to output the test results to the report, using the htmltestrunner.py file (the file reference address: https://github.com/huilansame/HTMLTestRunner_PY3)

(5) After the test is completed, you want to send the test results to the relevant people

To send a message specific implementation method, reference document: Https://www.cnblogs.com/shapeL/p/9115887.html

Description

1. The test data involved in the code are screened out and can be directly used to understand the real data of the project.

2. The test script is relatively basic, need to perfect the place also a lot, welcome you to learn to communicate;

3. Follow-up will write a blog interface Automation test (ii), implementation of Excel to manage use cases and so on.

Python3+requests+unittest: Interface Automation Test (i)

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.