Pytest test framework learning

Introduction to pytest

pytest is a very mature full-featured Python testing framework, which mainly has the following characteristics:

1. Simple and flexible, easy to use

2. Support parameterization

3. It can support simple unit testing and complex functional testing, and can also be used for automated testing such as selenium/appnium and interface automated testing (pytest+requests)

4. pytest has many third-party plug-ins, and can customize extensions, such as pytest-selenium (integrated selenium), pytest-html (perfect html test report generation), pytest-rerunfailures (failure case repeated execution), pytest -xdist (multi-CPU distribution), etc.

5. Skip and xfail processing of test cases

6. It can be well integrated with jenkins

7. Report framework - allure also supports pytest

Pytest naming convention:

1. The name of the test case file should start with test_ or end with _test.

2. Name the test function. The method name of the test class should start with test_.

3. The name of the test class should start with Test.

Test classes should not have constructors

How pytest test cases are run

1. Main function mode

(1) Run all: pytest.main()

(2 specifies the module: pytest.main(['-vs','test_login.py])

(3 Specify the directory: pytes.main(['-vs','./interface_testcase'])

(4 Use nodeid to specify the use case to run: nodeid is composed of module name, separator, class name, method name, and function name

pytest.main([’-vs’,’./interface_testcase/test_interface.py::test_04_func’])

pytest.main([’-vs’,’./interface_testcase/test_interface.py::Testinterface::test_04_func’])

2. Command line mode

(1) Run all: pytest

(2) Specify the module: pytest -vs test_login.py

(3) Specify the directory: pytes -vs ./interface_testcase

(4) Run by specifying the use case through nodeid: nodeid is composed of module name, separator, class name, method name, and function name

pytest -vs ./interface_testcase/test_interface.py::test_04_func

pytest -vs ./interface_testcase/test_interface.py::Testinterface::test_04_func

Detailed parameter explanation:

-s: Indicates to output debugging information, including the information printed by print

-v: display more detailed information

-vs: use two parameters together

-n: support multi-threaded or distributed running use cases

Such as: pytest -vs ./testcase/test_login.py -n 2

-return NUM: re-run of failed cases, the number of re-runs after num failures

-x: Indicates that as long as one use case reports an error, the test stops

--maxfall=2 : stop if two use cases fail

-k: Specify the test case according to the pace string of the test case

Such as: pytest -vs ./testcase -k "ao"

3. Run by reading the pytest.ini configuration file

pytest.ini This file is the core configuration file of the pytest unit testing framework

1. Location: generally placed in the root directory of the project

2. Encoding: must be ANSI, you can use notpad++ to modify the encoding format

3. Function: change the default behavior of pytest

4. Running rules: Whether it is running in the main function mode or command mode, it will read this configuration file

The order in which pytest executes the test cases:

unittes: The size of ASCII to determine the order of execution

pytest: default from top to bottom

Change the default execution order, use mark mark

@pytest.mark.run(order=2)

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive  

Guess you like

Origin blog.csdn.net/okcross0/article/details/130295362