Public account concerns: test charging treasure, communication together
In software testing, the same use case often needs to input multiple groups of different parameter combinations for functional coverage testing. In automated testing, we call this parameterization, which can be completed by using decorators in PyTest.
Pytest.mark.parametrize (argnames, argvalues) # Parameter: # argnames: comma-separated string # argValuse: Parameter value list, if there are more than one parameter, a group of parameters exist in the form of a tuple, all parameters containing multiple groups of parameters # exist in the form of a tuple listCopy the code
A parameter
Create a new test_05.py file
Import [email protected]. parametrize("arg_1",[1,2]) def test_01(arg_1): assert 1 == arg_1 if __name__ == '__main__': Pytest. main(['-s', 'test_05.py']) = = = = = = = = = = = = = = = = = = = = = = = = = = = = = test session starts = = = = = = = = = = = = = = = = = = = = = = = = = = = = = platform win32 - Python 3.7.1, Pytest-6.0.2, py-1.9.0, plugy-0.13.1 RootDir: D:\ Study \ Auto-PyTest collected 2 items test_05.py.. = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 2 passed in 0.07 s = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =Copy the code
Multiple parameters
Modify test_05. Py
import pytest values = [ (1, 1, 2), (1, 2, 4), (1, 3, 4) ] @pytest.mark.parametrize("a,b,c",values) def test_add(a,b,c): assert (a+b)==c if __name__ == '__main__': Pytest. main(['-s', 'test_05.py']) = = = = = = = = = = = = = = = = = = = = = = = = = = = = = test session starts = = = = = = = = = = = = = = = = = = = = = = = = = = = = = platform win32 - Python 3.7.1, Pytest - 6.0.2, py - 1.9.0, pluggy - 0.13.1 rootdir: D:\study\auto-pytest collected 3 items test_05.py .F. ================================== FAILURES =================================== _______________________________ test_add[1-2-4] _______________________________ a = 1, b = 2, c = 4 @pytest.mark.parametrize("a,b,c",values) def test_add(a,b,c): > assert (a+b)==c E assert (1 + 2) == 4 test_05.py:11: AssertionError =========================== short test summary info =========================== FAILED test_05.py::test_add[1-2-4] - assert (1 + 2) == 4 ========================= 1 failed, 2 Passed in 0.10s =========================Copy the code
Results When a+b=c is asserted, 1+2 is not equal to 4