Super detailed pytest tutorial [Introduction]

foreword

Regarding automated testing , I have experienced too many pits over the years, some passive pits, and some pits I actively dug. Here are some summaries.

The main thinking is to summarize some basic things in the automated testing process over the years, such as when to automate, how to automate, or how to automate our testing work. Next, we will first explain some of pytest.

So far, pytest has not translated well and used documents comprehensively. Many friends who are not very good at English still find it difficult to read English documents while studying. I originally planned to write pytest detailed documentation 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 begin with or begin with will be recognized as use case files. test__test
  • 2: Use case class, if there is no type at the beginning of each Test in the test file, it 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 configuration file of pytest (the use of configuration files 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, we can know that pytest use case can be written in the form of function or class. Then, we will introduce these two ways to write use cases.

2. Write use cases in the form of functions

Rule: Use case method name can start with test

  # \testcases\test_demo1.py

def test_demo():
    assert 100 == 100

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

  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

The command runs the above use case, the result is as follows: 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 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 the test with pytest, one is to execute the command line through the pytest command, and the test can also be executed through this method in the code. Next, I will introduce in detail the way pytest executes tests and the commonly used parameters pytest.main()

1. Execution parameters

test case

  # 测试用例
class TestDome:

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

Parameters: Display detailed parameter information of the test -v

  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 ==========================

Parameters: display the output information of the test execution -s

  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 is to execute the test parameter passing method:

So put the parameters in a 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 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 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

write at the end

Finally, let me say that if you want to take testing as a long-term career goal, you need to keep learning all the time. To make yourself competitive, no matter how many years you work now, as long as you take action, you will already have an advantage. That's all for now, I wish everyone a promotion and salary increase in 2022, get an offer from your favorite company, and everything goes smoothly.

It is not easy to organize this software testing and learning resources. I hope everyone can help to "like" and "collect". Let's not be a prostitute! !

Guess you like

Origin blog.csdn.net/weixin_67553250/article/details/124674706