pytest automated testing framework - basics

Pytest is an automated testing framework based on the Python programming language. It provides rich functions and flexible scalability, and can be used in various scenarios such as unit testing, integration testing, functional testing, and end-to-end testing. This article will introduce the basics of the Pytest framework, including installation, configuration, running tests, assertions, and parameterization.

1. Install Pytest

Before using Pytest, the framework needs to be installed first. It can be installed using the pip command as follows:

pip install pytest

2. Configure Pytest

Once installed, some configuration is required to start using Pytest. First, you need to clarify the naming convention of the test file, that is, name the test file starting with "test_" or ending with "_test". For example, a module named " calculator.py " should have a test module named "test_calculator.py".

Then you need to create a pytest.ini file, which is used to configure the Pytest framework, such as specifying the directory where the test file is located, the report output format, etc. Here is an example of a simple pytest.ini file:

[pytest]
testpaths = tests
addopts = --verbose

Among them, testpaths specifies the directory where the test file is located (the default is the current directory), and addopts is an additional option passed to the pytest command, for example --verbose indicates output details.

3. Run the test

After completing the installation and configuration of Pytest, you can start writing test cases and running tests. Running tests with the Pytest framework is very simple, just enter the following command in the terminal:

pytest

This command will automatically execute all test files starting with "test_" or ending with "_test" in the current directory, and output the test results. If you want to specify to run a specific test file or test function, you can add corresponding parameters after the pytest command, for example:

pytest tests/test_calculator.py

This command will only run the test file named "test_calculator.py".

4. Assertion

When writing a test case, the most important part is the assertion. Pytest provides many built-in assertion functions to judge whether the actual result meets the expected result. Here are some examples of commonly used assertion functions:

# 判断两个值是否相等
assert a == b

# 判断一个元素是否包含在列表中
assert element in lst

# 判断一个条件是否为真
assert condition

# 判断抛出异常是否符合预期
with pytest.raises(Exception):
    do_something()

In addition to the built-in assertion functions, you can also use third-party libraries for assertions, such as the assertpy library for richer assertions. The following is a sample code for assertion using the assertpy library:

from assertpy import assert_that

def test_addition():
    result = add(2, 3)
    assert_that(result).is_equal_to(5)

5. Parameterization

In testing, it is often necessary to perform multiple tests on the same test function with different input parameters. Pytest provides a parameterization function, which can pass multiple sets of parameters to the same test function for testing. Here is a simple parameterized example:

import pytest

@pytest.mark.parametrize("a, b, expected", [
    (2, 3, 5),
    (1, 4, 5),
    (0, 0, 0)
])
def test_addition(a, b, expected):
    result = add(a, b)
    assert result == expected

In the above example, the pytest.mark.parametrize decorator is used to parameterize the test_addition function, specifying three different sets of input parameters. Pytest will run each parameter combination in turn, make assertions, and output test results.

6. Summary

This article introduces the basics of the Pytest automated testing framework, including installation, configuration, operation, testing, assertion, and parameterization. The Pytest framework is easy to use and rich in functions, which can meet various types of testing needs. Here are some advantages of this framework:

1. Ease of use: Pytest adopts a concise and intuitive syntax, making the test code easier to read and understand.

2. Rich assertion library: Pytest has a large number of built-in assertion functions, and also supports third-party assertion libraries for more comprehensive test coverage.

3. Parameterization: Using the parameterization function of Pytest, you can quickly write test cases with multiple sets of input parameters to improve test efficiency.

4. Plug-ins and scalability: Pytest provides a rich plug-in system and API interface, and supports functions such as custom test reports and test data generators.

5. Cross-platform support: The Pytest framework can run on various operating systems and Python versions, and has good compatibility.

In short, Pytest is an automated testing framework that is very suitable for Python projects. It can help developers quickly write and test test cases, thereby improving software quality and development efficiency.

little help

I also compiled a set of the latest software testing system learning tutorials for you, including testing theory, Linux basics, MySQL basics, Web testing, interface testing, App testing, Python basics, Selenium related, performance testing, LordRunner related, etc. [Click at the end of the article Small card for free]

Guess you like

Origin blog.csdn.net/HUA1211/article/details/130201792