A primer on the Python testing framework Pytest

insert image description here

Introduction to pytest

pytest is a powerful yet easy-to-use testing framework for Python. It provides simple syntax and flexible features for writing and organizing test code.

1. Ease of use: The syntax of pytest is concise and clear, which makes writing test cases more intuitive and easy to understand. It uses assert statements to verify expected results, making test code more concise and readable.

2. Automatically discover tests: pytest can automatically discover and execute test files and test functions. It follows the naming convention (files and functions starting with "test_") to find test cases without specifying them manually.

3. Rich assertion support: pytest provides rich assertion functions for comparing and verifying whether the expected results are consistent with the actual results. This makes writing assertions much simpler and more intuitive.

4. Fixtures: Fixtures are a powerful feature of pytest that are used to perform some preparation and cleanup operations before and after test cases. Fixtures can help create mock objects, set up test environments, share resources, and more.

5. Parametric testing: pytest supports parametric testing, allowing the same test case to be run with different input parameters. This can greatly reduce the effort of writing repetitive test code.

6. Plug-in system: pytest provides a rich plug-in ecosystem that can extend its functionality. Plugins are available to enhance test reporting, generate coverage reports, integrate with other testing tools, and more.

7. Compatibility with other testing frameworks: pytest is compatible with other popular testing frameworks (such as unittest), so existing test codes can be gradually migrated to pytest.

pytest install

Install pytest First, make sure you have Python installed, and install pytest using the pip package manager.
Open a terminal and enter the following command:

pip install pytest

Verify the installation results:

pytest --version

insert image description here

pytest execute

step 1:

Create a test file In the project folder, create a Python file starting with "test_", note: the test file name should conform to the test_.py or _test.py format (for example, "test_calculator.py")

Step 2:

Writing test cases In a test file, one or more test functions can be defined, and each function corresponds to a test case.

# test_calculator.py

def test_addition():
    assert 2 + 2 == 4

def test_subtraction():
    assert 5 - 3 == 2

def test_multiplication():
    assert 3 * 4 == 12

def test_division():
    assert 10 / 2 == 5

In each test function, use the keyword assert to determine whether the expected result is consistent with the actual result. If the assertion fails, pytest will display an error message and mark the test as failed.

Step 3:

Running the tests In a terminal, switch to the project folder and run the following command to execute the tests:

pytest

pytest will automatically discover and run the test files. You will see a test results report with the status of each test case (passed, failed or skipped) and detailed error information (if any).

Step 4:

Using command-line options pytest provides a number of command-line options so that test runs can be customized to suit your needs. Here are some commonly used options:

Run a test file in a specific directory:

pytest path/to/tests

Run a test function in a specific test file:

pytest path/to/tests/test_module.py::test_function

Show verbose printout:

pytest -s

Generate a JUnit XML report:

pytest --junitxml=path/to/report.xml

The above are just some basic usages of pytest. pytest also has more advanced features such as parameterized tests, fixtures, test coverage, and more. I will share further later, and give the editor more motivation by clicking likes.

Finally: The complete software testing video tutorial below has been sorted out and uploaded, and friends who need it can get the software testing interview documents by themselves【保100%免费】
insert image description here

We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.
insert image description here

insert image description here

Guess you like

Origin blog.csdn.net/m0_67695717/article/details/132207623