pytest framework advanced self-study series | parameterized application

Book source: Fang Lizhi Liang Lili "pytest framework and automated testing application"

Organize the teacher's course content and experimental notes while studying, and share them with everyone. Any infringement will be deleted. Thank you for your support!

Attach a summary post: pytest framework advanced self-study series | summary


Data-driven via @pytest.mark.parametrize. The fundamental function of @pytest.mark.parametrize is to add calls (execution) to the marked object by assigning values ​​to the specified parameters during the process of collecting test cases. The following example illustrates how to use different data in specific parameterization.

single parametric application

Common usage scenarios: Only one piece of data in the test method is changed, that is, multiple sets of test data are passed in through a parameter. During execution, each set of data is executed once.

The specific steps to achieve are as follows:

(1) Enter @pytest.mark.parametrize in the test method.

(2) There are two parameters, one is the parameter name, and the other is the parameter value. This value can be multiple, and can be numbers or characters.

(3) The parameters in the test method are the same as the parameter names in parametrize.

(4) Call these data in the test method through the parameter name.

code show as below:

import pytest

@pytest.mark.parametrize("test_case", [1,2,3,'orange','apple'])
def test_string(test_case):
    print("\n要测试的数据:{}".format(test_case))

A test case is automatically executed as many times as there are pieces of data. The result of the execution is as follows:

D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_mark_parametrize.py
Testing started at 14:01 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_mark_parametrize.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4

============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book
collecting ... collected 5 items

test_mark_parametrize.py::test_string[1] PASSED                          [ 20%]
要测试的数据:1

test_mark_parametrize.py::test_string[2] PASSED                          [ 40%]
要测试的数据:2

test_mark_parametrize.py::test_string[3] PASSED                          [ 60%]
要测试的数据:3

test_mark_parametrize.py::test_string[orange] PASSED                     [ 80%]
要测试的数据:orange

test_mark_parametrize.py::test_string[apple] PASSED                      [100%]
要测试的数据:apple


============================== 5 passed in 0.02s ==============================

Process finished with exit code 0

Multi-parameter application

The input data of the test can be an expression, and the input parameters can be multiple. Multiple data can be organized by tuples. The following is a simple example of testing the calculator, the first two are variables, and the latter are the corresponding data. 3+5 corresponds to the test_input parameter name, 8 corresponds to the expected parameter name, and so on for the following data. eval evaluates the string str as a valid expression and returns the result.

code show as below:

import pytest

@pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+5", 7), ("7*5", 30)])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

Write one of the sets of data in the wrong form, and verify the details of the assertion. The execution results are as follows:

D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_mark_parametrize.py
Testing started at 14:03 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_mark_parametrize.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4

============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book
collecting ... collected 3 items

test_mark_parametrize.py::test_eval[3+5-8] 
test_mark_parametrize.py::test_eval[2+5-7] 
test_mark_parametrize.py::test_eval[7*5-30] PASSED                        [ 33%]PASSED                        [ 66%]FAILED                       [100%]
src\chapter-4\test_mark_parametrize.py:2 (test_eval[7*5-30])
35 != 30

Expected :30
Actual   :35
<Click to see difference>

test_input = '7*5', expected = 30

    @pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+5", 7), ("7*5", 30)])
    def test_eval(test_input, expected):
>       assert eval(test_input) == expected
E       assert 35 == 30
E         +35
E         -30

test_mark_parametrize.py:5: AssertionError








================================== FAILURES ===================================
______________________________ test_eval[7*5-30] ______________________________

test_input = '7*5', expected = 30

    @pytest.mark.parametrize("test_input, expected", [("3+5", 8), ("2+5", 7), ("7*5", 30)])
    def test_eval(test_input, expected):
>       assert eval(test_input) == expected
E       assert 35 == 30
E         +35
E         -30

test_mark_parametrize.py:5: AssertionError
=========================== short test summary info ===========================
FAILED test_mark_parametrize.py::test_eval[7*5-30] - assert 35 == 30
========================= 1 failed, 2 passed in 0.06s =========================

Process finished with exit code 1

Multiple parameterization

A use case can be marked with multiple @pytest.mark.parametrize markers.

code show as below:

import pytest

@pytest.mark.parametrize('test_input', [1,2,3])
@pytest.mark.parametrize('test_output, expected', [(1,2), (3, 4)])
def test_multi(test_input, test_output, expected):
    pass

The actual collected use cases are all possible combinations of them.

code show as below:

D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_multi.py
Testing started at 14:05 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_multi.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4

============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book
collecting ... collected 6 items

test_multi.py::test_multi[1-2-1] PASSED                                  [ 16%]
test_multi.py::test_multi[1-2-2] PASSED                                  [ 33%]
test_multi.py::test_multi[1-2-3] PASSED                                  [ 50%]
test_multi.py::test_multi[3-4-1] PASSED                                  [ 66%]
test_multi.py::test_multi[3-4-2] PASSED                                  [ 83%]
test_multi.py::test_multi[3-4-3] PASSED                                  [100%]

============================== 6 passed in 0.02s ==============================

Process finished with exit code 0

The combination of parameterization and fixture

When a test method not only injects dependencies, that is, uses fixtures, but also needs to be parameterized, there will be conflicts when using parametrize. In this case, parameterization can be realized through the parameter params that comes with the fixture. This is also a method of parameterization, see Section 3.8 for the implementation method, and Section 8.2.5 for the application.

pytestmark implements parameterization

You can try to parameterize a test module by assigning a value to pytestmark.

code show as below:

import pytest

pytestmark = pytest.mark.parametrize('test_input, expected', [(1,2),(3,4)])

def test_module(test_input, expected):
    assert test_input + 1 == expected
D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_module.py
Testing started at 14:09 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_module.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4

============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book
collecting ... collected 2 items

test_module.py::test_module[1-2] PASSED                                  [ 50%]
test_module.py::test_module[3-4] PASSED                                  [100%]

============================== 2 passed in 0.01s ==============================

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/131680572