Use case writing rules and use case running methods of pytest series

Get into the habit of writing together! This is the 10th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

1. Introduction to pytest

Pytest is a very mature python unit framework, which is more flexible and easy to use than unittest

Pytest can be combined with selenium, requests, and appium to realize web automation, interface automation, and APP automation

pytest can skip test cases and retry failed cases

pytest and allure can generate very beautiful test reports

pytest can be continuously integrated with Jenkins

pytest has many very powerful plug-ins, and these plug-ins can achieve many practical operations

Commonly used plugins are as follows:

pytest-html: Generate automated tests in HTML format

pytest-xdist: distributed execution of test cases, multi-CPU concurrency

pytest-ordering: used to change the execution order of test cases

pytest-rerunfailures: rerun after test failure

allure-pytest: for generating beautiful test reports

2. Use case writing rules

2.1 Module Naming Rules

The module name must start with test and an underscore or end with an 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 Test method naming rules

Test methods must start with test underscore

3. How the use case works

3.1 The main function runs

3.1.1 Run all use cases

pytest.main()
复制代码

3.1.2 Use Cases for Running Specified Modules

pytest.main(['-vs', 'test_login.py'])
复制代码

3.1.3 Run the use case in the specified directory:

pytest.main(['-vs', './interface_testcase'])   
复制代码

3.1.4 Specify the use case to run through modeid

nodeid consists of module name, separator, class name, method name, function name

3.1.4.1 Run the specified use case under the specified module

pytest.main(['-vs', './interface_testcase/test_interface.py::test_04_func'])
复制代码

3.1.4.2 Run the specified use case under the specified test class under the specified module

pytest.main(['-vs', './interface_testcase/test_interface.py::TestInterface::test_03_zhiliao'])
复制代码

3.2 Command Line Mode

3.2.1 Run all

Execute pytest directly

3.2.2 Use Cases for Running Specified Modules

pytest -vs test_login.py`
复制代码

3.2.3 Run the use case in the specified directory

pytest -vs ./interface_testcase
复制代码

3.2.4 Specify the use case to run through modeid

5.2.4.1 Run the specified use case under the specified module

pytest -vs ./interface_testcase/test_interface.py::test_04_func
复制代码

3.2.4.2 Run the specified use case under the specified test class under the specified module

pytest -vs ./interface_testcase/test_interface.py::TestInterface::test_03_zhiliao
复制代码

3.3 Run by reading the pytest.ini configuration file (the most common way)

The pytest.ini file is the core configuration file for the pytest unit testing framework

3.3.1 Location

Usually in the root directory of the project

3.3.2 Encoding

Must be ANSI, you can use notepad++ to modify the encoding format

3.3.3 Function

Change the default behavior of pytest

Examples of content are as follows:

[pytest]
addopts = -vs # 命令行的参数,用空格分隔
testpaths = '../pytest_project' # 测试用例的路径
python_files = test*.py # 模块名的规则
python_classes = Test* # 类名的规则
python_functions = test # 方法名的规则
复制代码

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

Guess you like

Origin juejin.im/post/7085503482542686244