Super detailed pytest tutorial (1) getting started

foreword

So far, pytest has not translated better and comprehensive use documents. Many friends who are not good at English still find it difficult to read English documents while studying. Originally, I planned to write the detailed usage documentation of pytest last year, but it has been put on hold due to time constraints, and I didn’t start writing until today. This article is the first one, which mainly introduces the introductory use of pytest, and the subsequent chapters will provide detailed tutorials for each function in pytest.

1. Environment installation

pytest is a third-party library in python. It needs to be installed before using it. Run the following installation command on the command line:

pip insatll pytest

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

pytest --version

Execute the above command, if the version information can be output, it means that the installation is successful.

2. Use case writing

When we execute a test case through pytest, pytest will automatically traverse all directories under the execution path recursively, and automatically collect test cases according to the identification rules of the default use case in pytest. Before using pytest to write test cases, we first need to understand the default use case identification rules when pytest collects test cases.

1. Default use case identification rules

  • 1. Use case files: All files whose file names start with test_ or _test will be identified as use case files.
  • 2: Use case class, the class at the beginning of each Test in the test file is a test case class.
  • 3. Test case: The method at the beginning of each test in the test class is a test case, and the function at the beginning of each test in the test file is also a test case.

Remarks: The above default use case search rules can be modified in the pytest configuration file (the use of the configuration file will be introduced in detail in subsequent chapters)

In addition, pytest is compatible with unittest, and pytest can recognize the use cases written according to the use cases of unittest

By understanding the above-mentioned rules of use case identification in pytest, you can know that use case writing in pytest can be written in the form of functions or classes. Then, I will introduce these two ways to write use cases.

2. Write use cases in the form of functions

Rules: Use case method name can start with test

# \testcases\test_demo1.py

def test_demo():
    assert 100 == 100

Use the command pytest to execute the test function, and the output is as follows:

C:\testcases>pytest 
======================test session starts ======================
platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0
rootdir: C:\testcases
plugins:  testreport-1.1.2
collected 1 item                                                           
test_demo1.py .    [100%]
====================== 1 passed in 0.26s ======================

3. Write use cases in the form of classes

Rules: Test class names start with Test, use case methods start with test

# test_demo2.py
class TestDome:

    def test_demo1(self):
        assert 11 == 11

    def test_demo(self):
        assert 22 == 21

Command pytest to run the above use case, the result is as follows:

====================== test session starts ======================
platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0
rootdir: C:\testcases
plugins:  testreport-1.1.2
collected 2 items                                                                   
test_demo1.py .F                  [100%]
====================== FAILURES ======================
___________ TestDome.test_demo ____________
self = <test_demo1.TestDome object at 0x0445F450>
    def test_demo(self):
>       assert 22 == 21
E       assert 22 == 21
test_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 ======================

From the above running results, it can be seen that one use case is executed and one execution fails.

3. Execute the test

Above we used the pytest command to execute test cases. There are two ways to execute tests with pytest. One is to execute the command line through the pytest command, and the test can be executed through the pytest.main() method in the code. Next, I will introduce in detail the way pytest executes the test and the commonly used parameters.

1. Execution parameters

test case

# 测试用例
class TestDome:

    def test_demo1(self):
        print('----测试用例执行-----------')
        assert 11 == 11

Parameter -v : Display detailed parameter information of the test

C:\testcases>pytest -v
========================== test session starts ==========================
platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0 
cachedir: .pytest_cache
rootdir: C:\git_project\pytest-report-me-main\testcases
plugins: testreport-1.1.2
collected 1 item                                                                         test_demo1.py::TestDome::test_demo1 PASSED          [100%]
========================== 1 passed in 0.27s ==========================

Parameter -s: Display the output information of the test execution

C:\testcases>pytest -s
=========================== test session starts ===========================
platform win32 -- Python 3.7.3, pytest-5.4.2, py-1.8.0, pluggy-0.13.0
rootdir: C:\testcases
plugins: testreport-1.1.2
collected 1 item     
test_demo1.py::TestDome::test_demo1 ----测试用例执行---输出1--------
----测试用例执行---输出2--------
PASSED
=========================== 1 passed in 0.28s ===========================

2. Parameter passing executed by pytest.main

The pytest.main method executes the test parameter passing method:

So the parameters are placed in a list, and 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 to execute

Command pytest test directory path

pytest testcase/

pytest will execute all test cases under the specified directory path

4. Specify the test file to execute

command pytest test file path

pytest testcase/test_demo1.py

pytest will execute all the test cases in the specified test file

5. Specify the test class to execute

command pytest test-file::test-class

pytest testcase/test_demo1.py::TestClass

pytest will execute all test cases in the specified test class

6. Specify the test cases to be executed

command pytest test-file::test-class::test-method

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

pytest will execute the specified test method

The basic introduction will be introduced here, and the relevant features of pytest will be introduced in detail from the beginning of the article.

Guess you like

Origin blog.csdn.net/a448335587/article/details/128259267