The high-rich hands of the unit test industry, Pytest framework, hand handle teaching, from getting started

Home > Sci-Tech

The high-rich hands of the unit test industry, Pytest framework, hand handle teaching, from getting started

2021-11-30 12:10:43 43 ℃

Pytest has not translated a better use document so far, many English is not very good, and it is still very difficult to read English documents during learning. Originally, I plan to write Pytest's detailed use of documentation, because the time relationship has been put on hold until today, I started writing. This article is the first, mainly introducing the introduction of Pytest, followed by the detailed use tutorial for each function in Pytest.

Pytest is a third-party library in Python. You need to install before use, run the following installation command in the command line:

Pip Insatll Pytest

Check if the installation is successful and installed, the command line command is as follows:

pytest --version

Execute the above command, you can output version information, then explain the installation is successful.

When we perform the use case by Pytest, Pytest automatically recursively traverses all directories under the path, automatically collects test cases according to the rules of the default cases of defaults in Pytest. All we first need to understand the default use case recognition rules when you use Pytest's test case.

1. Use example file: All files named_ start or _Test starts will be identified as the use case file. 2: Use an example class, the class that starts in each Test in the test file is a test case class. 3. Test case: The method of each Test in the test class is a test case. The function of each Test in the test file is also a test case.

Remarks: The default use case lookup rules can be modified in Pytest configuration files (subsequent chartes will detail the use of configuration files)

In addition, Pytest is compatible with UnitTest, written in the use case written by UnitTest, and Pytest can identify it.

By understanding the rules used in the above-described Pytest, you can know that the use of example in Pytest can use the form of functions. You can also use the form of the class, then you will introduce you to the two ways.

Rules: Take the name of the case to start with TEST

# testcasestest_demo1.pydef test_demo: assert 100 == 100

The test function can be executed using the command pytest, and the output is as follows:

C: Testcases> Pytest Test Session Starts Platform Win32 - Python 3.7.3, Pytest-5.4.2, PYTSTDIR: C: TestcaseSplugins: TestReport-1.1.2collected 1 item test_demo1 .py. [100 1 passed in 0.26s

Rules: Test class named Test, use case method starts with TEST

# test_demo2.pyclass testdome: Def test_demo1 (self): assert 11 == 11def test_demo (self): assrt 22 == 21

The command pytest runs the above use case, as follows:

Test session Starts Platform Win32 - Python 3.7.3, Pytest-5.4.2, Py-1.8.0, Pluggy-0.13.0Rootdir: C: TestcaseSplugins: TestReport-1.1.2Collected 2 items test_demo1.py .f [ 100 FAILURES TestDome.test_demo self = def test_demo (self):> assert 22 == 21E assert 22 == 21test_demo1.py:25: AssertionError short test summary info FAILED test_demo1.py::TestDome: : test_demo - assert 22 == 21 1 failed, 1 passed in 0.53s The above run results can be seen, one case execution passes, one execution failed

Third, execute test

In the above we use the pytest this command to perform test cases. About PyTest execution test, there are two ways, one is the command line through the pytest this command, and the test can be performed by the Pytest.main method in the code. Next, I will introduce the way Pytest execution test and common parameters.

1, execute parameters

Test case

# Test case class testdome: Def test_demo1 (self): print ('---- test case execution') assert 11 == 11

Parameters -V: Detailed parameter information for display

C: Testcases> Pytest -s Test Session Starts Platform Win32 - Python 3.7.3, Pytest-5.4.2, PYTSPLUGINS: TestReport-1.1.2collected 1 Item test_demo1.py::testdome:test_demo1 ---- Test case execution --- Output 1 test case execution --- Output 2 -------- Passed 1 passed in 0.28s

Pytest.main method Execute test parameters transmission mode:

So the parameters are placed in the list, each parameter is an element in the list.

pytest.main (['- v', '- s'])

Detailed parameters can be viewed using the command pytest -h

3. Specify the test directory executed

Command Pytest Test Directory Path

Pytest TestCase /

Pytest will execute all test cases under the specified directory path

Command Pytest test file path

pytest testcase / test_demo1.py

Pytest will perform all test cases in the specified test file.

Command Pytest Test File :: Test Class

pytest testcase / test_demo1.py :: testclass

Pytest will execute all test cases in the specified test class

Command Pytest Test File :: Test Class :: Test Method

pytest testcase / test_demo1.py :: testclass :: test_method

Pytest will execute the specified test method

Basic entry gives you here, from the next article, give you a detailed introduction of Pytest's related features.