pytest parametric pytest document 9- parametric parametrize

pytest document 9- parametric parametrize

 

Foreword

pytest.mark.parametrize decorative parametric test may be implemented.

parametrizing

1. Here is a typical example of inspection certain input and output desired test functions implemented

# content of test_expectation.py

# coding:utf-8

import pytest
@pytest.mark.parametrize("test_input,expected",
                         [ ("3+5", 8), ("2+4", 6), ("6 * 9", 42), ]) def test_eval(test_input, expected): assert eval(test_input) == expected if __name__ == "__main__": pytest.main(["-s", "test_canshu1.py"]) 

operation result


================================== FAILURES ===================================
_____________________________ test_eval[6 * 9-42] _____________________________

test_input = '6 * 9', expected = 42 @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), ("2+4", 6), ("6 * 9", 42), ]) def test_eval(test_input, expected): > assert eval(test_input) == expected E AssertionError: assert 54 == 42 E + where 54 = eval('6 * 9') test_canshu1.py:11: AssertionError ===================== 1 failed, 2 passed in 1.98 seconds ======================

In this example design, only one input / output value is a simple function test. as usual

Function parameters, you can see the input and output values ​​in operating results

2. It can also be marked in a single instance of the parameter tested, for example using the built mark.xfail

# content of test_expectation.py
import pytest
@pytest.mark.parametrize("test_input,expected", [
                        ("3+5", 8), ("2+4", 6), pytest.param("6 * 9", 42, marks=pytest.mark.xfail), ]) def test_eval(test_input, expected): print("-------开始用例------") assert eval(test_input) == expected if __name__ == "__main__": pytest.main(["-s", "test_canshu1.py"])

operation result:

test_canshu1.py -------开始用例------
.-------开始用例------
.-------开始用例------
x

===================== 2 passed, 1 xfailed in 1.84 seconds =====================

Use cases marked as failed, the expected result is a failure, the failure is the actual operation, the display xfailed

Parameter combination

1. To obtain the parameters of the plurality of all combinations of parameters, parameterization may be stacked decorator

import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3]) def test_foo(x, y): print("测试数据组合:x->%s, y->%s" % (x, y)) if __name__ == "__main__": pytest.main(["-s", "test_canshu1.py"])

operation result


test_canshu1.py 测试数据组合:x->0, y->2
.测试数据组合:x->1, y->2 .测试数据组合:x->0, y->3 .测试数据组合:x->1, y->3 . ========================== 4 passed in 1.75 seconds ===========================

This test will be run, the parameter is set to x = 0 / y = 2, x = 1 / y = 2, x = 0 / y = 3, x = 1 / y = 3 parameter combinations.

Foreword

pytest.mark.parametrize decorative parametric test may be implemented.

parametrizing

1. Here is a typical example of inspection certain input and output desired test functions implemented

# content of test_expectation.py

# coding:utf-8

import pytest
@pytest.mark.parametrize("test_input,expected",
                         [ ("3+5", 8), ("2+4", 6), ("6 * 9", 42), ]) def test_eval(test_input, expected): assert eval(test_input) == expected if __name__ == "__main__": pytest.main(["-s", "test_canshu1.py"]) 

operation result


================================== FAILURES ===================================
_____________________________ test_eval[6 * 9-42] _____________________________

test_input = '6 * 9', expected = 42 @pytest.mark.parametrize("test_input,expected", [ ("3+5", 8), ("2+4", 6), ("6 * 9", 42), ]) def test_eval(test_input, expected): > assert eval(test_input) == expected E AssertionError: assert 54 == 42 E + where 54 = eval('6 * 9') test_canshu1.py:11: AssertionError ===================== 1 failed, 2 passed in 1.98 seconds ======================

In this example design, only one input / output value is a simple function test. as usual

Function parameters, you can see the input and output values ​​in operating results

2. It can also be marked in a single instance of the parameter tested, for example using the built mark.xfail

# content of test_expectation.py
import pytest
@pytest.mark.parametrize("test_input,expected", [
                        ("3+5", 8), ("2+4", 6), pytest.param("6 * 9", 42, marks=pytest.mark.xfail), ]) def test_eval(test_input, expected): print("-------开始用例------") assert eval(test_input) == expected if __name__ == "__main__": pytest.main(["-s", "test_canshu1.py"])

operation result:

test_canshu1.py -------开始用例------
.-------开始用例------
.-------开始用例------
x

===================== 2 passed, 1 xfailed in 1.84 seconds =====================

Use cases marked as failed, the expected result is a failure, the failure is the actual operation, the display xfailed

Parameter combination

1. To obtain the parameters of the plurality of all combinations of parameters, parameterization may be stacked decorator

import pytest
@pytest.mark.parametrize("x", [0, 1])
@pytest.mark.parametrize("y", [2, 3]) def test_foo(x, y): print("测试数据组合:x->%s, y->%s" % (x, y)) if __name__ == "__main__": pytest.main(["-s", "test_canshu1.py"])

operation result


test_canshu1.py 测试数据组合:x->0, y->2
.测试数据组合:x->1, y->2 .测试数据组合:x->0, y->3 .测试数据组合:x->1, y->3 . ========================== 4 passed in 1.75 seconds ===========================

This test will be run, the parameter is set to x = 0 / y = 2, x = 1 / y = 2, x = 0 / y = 3, x = 1 / y = 3 parameter combinations.

Guess you like

Origin www.cnblogs.com/bonelee/p/12110957.html