Make writing a habit together! This is the 10th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

1. Pytest profile

Pytest is a very mature Python unit framework that is more flexible and easy to use than UnitTest

Pytest can be combined with Selenium, Requests, and APpium to implement Web automation, interface automation, and APP automation

Pytest allows skip of test cases and retry of failed use cases

Pytest can work with Allure to generate very beautiful test reports

Pytest can be continuously integrated with Jenkins

Pytest has a lot of very powerful plug-ins that can do a lot of useful things

Common plug-ins are as follows:

Pytest-html: Generates automated tests in HTML format

Pytest-xdist: Test cases are distributed and executed concurrently on multiple cpus

Pytest-ordering: Used to change the execution order of a test case

Pytest-rerunfailures: Rerun of a use case failure

Allure – Pytest: Used to generate aesthetically pleasing test reports

2. Write rules for use cases

2.1 Module naming rules

Module names must start with test and underscore or end with underscore and test

2.2 Test class naming rules

The Test class name must start with Test and cannot have an init method

2.3 Naming rules of test methods

Test methods must begin with test underlined

3. How use cases work

3.1 Main function operation

3.1.1 Run all use cases

pytest.main()
Copy the code

3.1.2 Run the use case of the specified module

pytest.main(['-vs', 'test_login.py'])
Copy the code

3.1.3 Running Use Cases in the Specified Directory:

pytest.main(['-vs', './interface_testcase'])   
Copy the code

3.1.4 Run the use case specified by modeID

Nodeid consists of module name, delimiter, class name, method name, and function name

3.1.4.1 Running the specified use case in the specified module

pytest.main(['-vs', './interface_testcase/test_interface.py::test_04_func'])
Copy the code

3.1.4.2 Run the specified use case of the specified test class in the specified module

pytest.main(['-vs', './interface_testcase/test_interface.py::TestInterface::test_03_zhiliao'])
Copy the code

3.2 Cli Mode

3.2.1 Running All

Execute PyTest directly

3.2.2 Run the use case of the specified module

pytest -vs test_login.py`
Copy the code

3.2.3 Running the use Cases in the specified directory

pytest -vs ./interface_testcase
Copy the code

3.2.4 Specify use cases to run by modeID

5.2.4.1 Running a Specified Use Case in a Specified Module

pytest -vs ./interface_testcase/test_interface.py::test_04_func
Copy the code

3.2.4.2 Run the specified use case of the specified test class in the specified module

pytest -vs ./interface_testcase/test_interface.py::TestInterface::test_03_zhiliao
Copy the code

3.3 Run by reading the Pytest.ini configuration file (most commonly used)

The PyTest.ini file is the core configuration file of the PyTest unit testing framework

3.3.1 position

Usually in the root directory of the project

3.3.2 rainfall distribution on 10-12 coding

It must be ANSI and can be changed using notepad++

3.3.3 role

Change the default behavior of PyTest

The following is an example:

[PyTest] addopts = -vs # Command line arguments, separated by Spaces testPaths = '.. Python_classes = test* # Python_functions = test # python_functions = test # Rules for method namesCopy the code

Running rules: whether the main function is running, or the command line mode is running, get read this configuration file