This article is participating in Python Theme Month. See the link to the event for more details

No road taken in vain, every step counts

preface

There are a lot of function in the project development, developers often need to write your own code test, in addition to the test with the aid of tools such as postman, also need to write the unit tests to test code developed by the unit test to tell whether the code can realize demand, this paper introduces the pytest module is a very useful framework, Support not only simple tests but also complex functional tests in the application.

Pytest profile

Pytest is a mature full-featured Python testing tool that helps you write better programs.

Getting start

Pytest installation

Pytest runs in python3.6 3.7 3.8 3.9 pypy3. On the command line, run the following command to install PyTest:

pip install pytest
Copy the code

After the installation, run the following command to check whether the installation is successful:

pytest --version
pytest 6.24.
Copy the code

The first test

You can create a simple test for a function with four lines of code.

# test.py
def func(x) :
    return x+1


def test_answer() :
    assert func(5) = =7
Copy the code

Now you can test the functionality by executing the Pytest command.

100% is the overall progress of running all the test cases, and when complete, PyTest displays a failure report indicating the specific cause of the error. Pytest can specify files to test. More often than not, PyTest will run all files in the current directory and subdirectories in the form test_*. Py or *_test.py.

Perform multiple tests in the class

If you have written multiple Test cases, you can put them in the same class, but make sure the type is prefixed with Test, otherwise it will be skipped by PyTest.

class TestDemo:

    def test_one(self) :
        x = 'this'
        assert 'h' in x

    def test_two(self) :
        x = 'hello'
        assert hasattr(x, 'check')
Copy the code

Here are the results of tests executed with PyTest. There are two important points to note when writing tests in a class: First, the class name must have the Test prefix. Second, there is no need to instantiate an object when testing with a class.

Usage and Invocations

Exit codes

Process Finished with exit code 0 is displayed when pyCharm is used to complete a program. Exit code is also displayed when PyTest is used to complete the program. And there may be about six different exit codes:

Exit Code 0: All tests have been collected and passed successfully

Exit Code 1: Tests were collected and run, but some tests failed

Exit Code 2: The test execution is interrupted

Exit Code 3: An internal error occurred during test execution

Exit code 4: Pytest command is incorrect

Exit Code 5: No tests are collected

Termination of the test

When PyTest is running, you can control to stop the test after one or N failure messages.

pytest -x           # stop after first failure
pytest --maxfail=2  # stop after two failures
Copy the code

Specify/select test cases

Pytest supports multi-party running and selection of test cases.

Test the specified py file: pytest test_mod.py

Test the specified directory: pytest /usr/local/testing/

Specify tests by nodeid: each collected test is assigned a unique nodeid, separated by module name, class name, function name, and so on, each part separated by ::.

Run the specified test case in the module: pytest test_mod.py::test_func Run a test method in the class: pytest test_mod.py::TestClass::test_methodCopy the code

Pytest options

The -r option can be used to display a more truncated test summary at the end of the test session. In large test cases, it is very clear to get all the failures, skips, and so on. The -r parameter can be used in combination with the following options to output different results.

F: failed E: wrong S: skipped X: failed P: passedCopy the code

To view only failed and skipped tests, you can run: pytest-rfs.

conclusion

The article was first published in the wechat public account program Yuan Xiaozhuang, synchronized with nuggets.

Please explain where it came from. If you pass by, please put out your cute little finger and click like before you go (╹▽╹)