当前位置:网站首页>Pytest set (7) - parameterization
Pytest set (7) - parameterization
2022-07-29 07:44:00 【Do it.kiss】
One 、pytest.mark.parametrize()
(1) Grammatical sugar :
parametrize
(argnames, argvalues, indirect=False, ids=None, scope=None, *, _param_mark=None)
(2) Parameter description :
argnames: Parameter name , Comma separated strings , Represents one or more parameter names . Or a list of parameter strings / Tuples .
If it is a parameter , Use the string of parameter name .
If it's multiple parameters , A comma separated string between multiple parameter names , Or a list of multiple parameter names , Or tuples composed of multiple parameter names .
argvalues: Parameter values , Type is an iteratable object , It corresponds to the parameter name one by one .
If argnames For a parameter , be argvalues Is a list of values .
If argnames For multiple parameters , be argvalues Must be a list of nested tuples , The element value of each tuple corresponds to the parameter name one by one .
indirect: Parameter name list ( Subset of parameter names ) Or boolean values .
indirect Generally speaking, it is related to Pytest Of fixture,request.param Use a combination of
When indrect =True when ,argnames The parameter name is fixture Function name of fixture ,argvalues Is the parameter passed to the fixture function .
ids: Mark the execution name of the parameterized test case , Automatically generated by default , Use "-" Connect .
scope: Parameter range .
(3) Source code :
def parametrize( self, argnames: Union[str, List[str], Tuple[str, ...]], argvalues: Iterable[Union[ParameterSet, Sequence[object], object]], indirect: Union[bool, Sequence[str]] = False, ids: Optional[ Union[Iterable[Optional[object]], Callable[[Any], Optional[object]]] ] = None, scope: "Optional[_ScopeName]" = None, *, _param_mark: Optional[Mark] = None, ) -> None: """Add new invocations to the underlying test function using the list of argvalues for the given argnames. Parametrization is performed during the collection phase. If you need to setup expensive resources see about setting indirect to do it rather than at test setup time. Can be called multiple times, in which case each call parametrizes all previous parametrizations, e.g. :: unparametrized: t parametrize ["x", "y"]: t[x], t[y] parametrize [1, 2]: t[x-1], t[x-2], t[y-1], t[y-2] :param argnames: A comma-separated string denoting one or more argument names, or a list/tuple of argument strings. :param argvalues: The list of argvalues determines how often a test is invoked with different argument values. If only one argname was specified argvalues is a list of values. If N argnames were specified, argvalues must be a list of N-tuples, where each tuple-element specifies a value for its respective argname. :param indirect: A list of arguments' names (subset of argnames) or a boolean. If True the list contains all names from the argnames. Each argvalue corresponding to an argname in this list will be passed as request.param to its respective argname fixture function so that it can perform more expensive setups during the setup phase of a test rather than at collection time. :param ids: Sequence of (or generator for) ids for ``argvalues``, or a callable to return part of the id for each argvalue. With sequences (and generators like ``itertools.count()``) the returned ids should be of type ``string``, ``int``, ``float``, ``bool``, or ``None``. They are mapped to the corresponding index in ``argvalues``. ``None`` means to use the auto-generated id. If it is a callable it will be called for each entry in ``argvalues``, and the return value is used as part of the auto-generated id for the whole set (where parts are joined with dashes ("-")). This is useful to provide more specific ids for certain items, e.g. dates. Returning ``None`` will use an auto-generated id. If no ids are provided they will be generated automatically from the argvalues. :param scope: If specified it denotes the scope of the parameters. The scope is used for grouping tests by parameter instances. It will also override any fixture-function defined scope, allowing to set a dynamic scope using test context or configuration. """
1、 Single parameter name
newly build test_parametrize.py The documents are as follows :
import pytest # Single parameter name @pytest.mark.parametrize('a', [1, 2, 3]) def test_one(a): print(a)
Run the command :ytest test_parametrize.py -vs
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>pytest test_parametrize.py -vs
==================================== test session starts ====================================
platform win32 -- Python 3.8.8, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- e:\programs\python\python38\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.8', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.3', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0', 'rerunfailures': '9.1.1', 'assume': '2.2.0', 'requests-mock': '1.7.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_271'}
rootdir: C:\Users\057776\PycharmProjects\pytest-demo\testmark, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0, rerunfailures-9.1.1, assume-2.2.0, requests-mock-1.7.0
collected 3 itemstest_parametrize.py::test_one[1] 1 PASSED
test_parametrize.py::test_one[2] 2 PASSED
test_parametrize.py::test_one[3] 3 PASSED==================================== 3 passed in 0.04s ====================================
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>
explain :
argnames When the parameter name has only one parameter , Use the string of parameter name ,argvalues Parameter values use a list .
The test case test_one Use decorators parametrize,pytest Every time you run a test case , Parameter name a From the parameter value [1,2,3] Take a value from the to run the sub use case .
2、 Multiple parameter names
modify test_parametrize.py The documents are as follows :
import pytest # Single parameter name @pytest.mark.parametrize('a', [1, 2, 3]) def test_one(a): print(a) # Multiple parameter names @pytest.mark.parametrize("a,b", [('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')]) # @pytest.mark.parametrize(['a', 'b'], [('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')]) # @pytest.mark.parametrize(('a', 'b'), [('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')]) def test_two(a, b): print(a, b)
Run the command :pytest test_parametrize.py -vs
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>pytest test_parametrize.py -vs
=================================== test session starts ===================================
platform win32 -- Python 3.8.8, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- e:\programs\python\python38\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.8', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.3', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0', 'rerunfailures': '9.1.1', 'assume': '2.2.0', 'requests-mock': '1.7.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_271'}
rootdir: C:\Users\057776\PycharmProjects\pytest-demo\testmark, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0, rerunfailures-9.1.1, assume-2.2.0, requests-mock-1.7.0
collected 6 itemstest_parametrize.py::test_one[1] 1 PASSED
test_parametrize.py::test_one[2] 2 PASSED
test_parametrize.py::test_one[3] 3 PASSED
test_parametrize.py::test_two[a1-b1] a1 b1 PASSED
test_parametrize.py::test_two[a2-b2] a2 b2 PASSEDtest_parametrize.py::test_two[a3-b3] a3 b3 PASSED
=================================== 6 passed in 0.07s ===================================
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>
explain :
argnames When there are multiple parameter names , A comma separated string between multiple parameter names , A list of multiple parameter names , Tuple composed of multiple parameter names , All three of them can be .
argnames When there are multiple parameter names ,argvalues Must be a list of nested tuples , The element value of each tuple corresponds to the parameter name one by one .
ids Parameters , Automatically generated by default , Use "-" Connect .
pytest When it's running ,parametrize Decorative test cases test_two From the parameter value list [('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')] Remove from 3 Tuples , function 3 Second test case .
3、indrect =True Fixture parameterization
modify test_parametrize.py The documents are as follows :
import pytest# Fixture parameterization @pytest.fixture() def fixture_demo(request): return request.param + 1 lst = [1, 2, 3] # indirect=True,argnames Pass in the fixture function corresponding to the parameter name as a parameter @pytest.mark.parametrize('fixture_demo', lst, indirect=True) def test_three(fixture_demo): print(fixture_demo)
Run the command :pytest test_parametrize.py -vs
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>pytest test_parametrize.py -vs
================================= test session starts =================================
platform win32 -- Python 3.8.8, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- e:\programs\python\python38\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.8', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.3', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.
1', 'metadata': '1.11.0', 'rerunfailures': '9.1.1', 'assume': '2.2.0', 'requests-mock': '1.7.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_271'}
rootdir: C:\Users\057776\PycharmProjects\pytest-demo\testmark, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0, rerunfailures-9.1.1, assume-2.2.0, requests-mock-1.7.0
collected 3 itemstest_parametrize.py::test_three[1] 2 PASSED
test_parametrize.py::test_three[2] 3 PASSED
test_parametrize.py::test_three[3] 4 PASSED=================================== 3 passed in 0.05s ===================================
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>
explain :
When indrect =True when ,argnames The parameter name is fixture Function name of fixture ,argvalues Is the parameter that passes the value to the fixture function .
fixture pytest.fixture(params) It can be used to realize parameterization , When parameterizing, you need to use a parameter request, For reception fixture The result returned , adopt request.param To return the parameter content .
4、 Decoration test class
modify test_parametrize.py The documents are as follows :
import pytest # Decorating a class needs to pay attention to the consistency of parameters with all test functions in the class @pytest.mark.parametrize("a,b", [('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')]) class TestClass: def test_four(self, a, b): print(a, b) def test_five(self, a, b): print(a + b)
Run the command :pytest test_parametrize.py -vs
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>pytest test_parametrize.py -vs
=================================== test session starts ===================================
platform win32 -- Python 3.8.8, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- e:\programs\python\python38\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.8', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.3', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.
1', 'metadata': '1.11.0', 'rerunfailures': '9.1.1', 'assume': '2.2.0', 'requests-mock': '1.7.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_271'}
rootdir: C:\Users\057776\PycharmProjects\pytest-demo\testmark, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0, rerunfailures-9.1.1, assume-2.2.0, requests-mock-1.7.0
collected 6 itemstest_parametrize.py::TestClass::test_four[a1-b1] a1 b1 PASSED
test_parametrize.py::TestClass::test_four[a2-b2] a2 b2 PASSED
test_parametrize.py::TestClass::test_four[a3-b3] a3 b3 PASSED
test_parametrize.py::TestClass::test_five[a1-b1] a1b1 PASSED
test_parametrize.py::TestClass::test_five[a2-b2] a2b2 PASSED
test_parametrize.py::TestClass::test_five[a3-b3] a3b3 PASSED=================================== 6 passed in 0.20s ===================================
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>
explain :
parametrize Decorative class TestClass All test cases in call parameterization .
5、 Decorative module
modify test_parametrize.py The documents are as follows :
import pytest pytestmark = pytest.mark.parametrize("a,b", [('a1', 'b1'), ('a2', 'b2'), ('a3', 'b3')]) def test_two(a, b): print(a, b) class TestClass: def test_four(self, a, b): print(a, b) def test_five(self, a, b): print(a + b)
Run the command :pytest test_parametrize.py -vs
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>pytest test_parametrize.py -vs
=================================== test session starts ===================================
platform win32 -- Python 3.8.8, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- e:\programs\python\python38\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.8', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.3', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0', 'rerunfailures': '9.1.1', 'assume': '2.2.0', 'requests-mock': '1.7.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_271'}
rootdir: C:\Users\057776\PycharmProjects\pytest-demo\testmark, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0, rerunfailures-9.1.1, assume-2.2.0, requests-mock-1.7.0
collected 9 itemstest_parametrize.py::test_two[a1-b1] a1 b1 PASSED
test_parametrize.py::test_two[a2-b2] a2 b2 PASSED
test_parametrize.py::test_two[a3-b3] a3 b3 PASSED
test_parametrize.py::TestClass::test_four[a1-b1] a1 b1 PASSED
test_parametrize.py::TestClass::test_four[a2-b2] a2 b2 PASSED
test_parametrize.py::TestClass::test_four[a3-b3] a3 b3 PASSED
test_parametrize.py::TestClass::test_five[a1-b1] a1b1 PASSED
test_parametrize.py::TestClass::test_five[a2-b2] a2b2 PASSED
test_parametrize.py::TestClass::test_five[a3-b3] a3b3 PASSED==================================== 9 passed in 0.08s ====================================
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>
explain :
Use global variables pytestmark = pytest.mark.parametrize(), All test cases in the module can be parameterized .
6、 Built in mark.xfail, Mark the expected execution failure of the test case in the parameterization
modify test_parametrize.py The documents are as follows :
import pytest # Built in mark.xfail, Mark the expected execution failure of a single test case in parameterization @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): assert eval(test_input) == expected
Run the command :pytest test_parametrize.py -vs
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>pytest test_parametrize.py -v
================================== test session starts ==================================
platform win32 -- Python 3.8.8, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- e:\programs\python\python38\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.8', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.3', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0', 'rerunfailures': '9.1.1', 'assume': '2.2.0', 'requests-mock': '1.7.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_271'}
rootdir: C:\Users\057776\PycharmProjects\pytest-demo\testmark, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0, rerunfailures-9.1.1, assume-2.2.0, requests-mock-1.7.0
collected 3 itemstest_parametrize.py::test_eval[3+5-8] PASSED [ 33%]
test_parametrize.py::test_eval[2+4-6] PASSED [ 66%]
test_parametrize.py::test_eval[6*9-42] XFAIL [100%]=============================== 2 passed, 1 xfailed in 0.46s ===============================
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>
explain :
xfail The marked test case is expected to fail , Actual execution failed , Then the status is marked XFAIL.
7、 Stack multiple parametrize Decorator
modify test_parametrize.py The documents are as follows :
import pytest # Stack multiple parametrize Decorator , under these circumstances , Every call parameterizes all previous parameterizations . @pytest.mark.parametrize('a', ['a1', 'a2']) @pytest.mark.parametrize('b', ['b1', 'b2', 'b3']) def test_over(a, b): print(a, b)
Run the command :pytest test_parametrize.py -vs
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>pytest test_parametrize.py -vs
=================================== test session starts ===================================
platform win32 -- Python 3.8.8, pytest-6.2.3, py-1.10.0, pluggy-0.13.1 -- e:\programs\python\python38\python.exe
cachedir: .pytest_cache
metadata: {'Python': '3.8.8', 'Platform': 'Windows-10-10.0.19041-SP0', 'Packages': {'pytest': '6.2.3', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'html': '3.1.1', 'metadata': '1.11.0', 'rerunfailures': '9.1.1', 'assume': '2.2.0', 'requests-mock': '1.7.0'}, 'JAVA_HOME': 'C:\\Program Files\\Java\\jdk1.8.0_271'}
rootdir: C:\Users\057776\PycharmProjects\pytest-demo\testmark, configfile: pytest.ini
plugins: html-3.1.1, metadata-1.11.0, rerunfailures-9.1.1, assume-2.2.0, requests-mock-1.7.0
collected 6 itemstest_parametrize.py::test_over[b1-a1] a1 b1 PASSED
test_parametrize.py::test_over[b1-a2] a2 b1 PASSED
test_parametrize.py::test_over[b2-a1] a1 b2 PASSED
test_parametrize.py::test_over[b2-a2] a2 b2 PASSED
test_parametrize.py::test_over[b3-a1] a1 b3 PASSED
test_parametrize.py::test_over[b3-a2] a2 b3 PASSED=================================== 6 passed in 0.07s ===================================
(venv) C:\Users\057776\PycharmProjects\pytest-demo\testmark>
explain :
Stack multiple parametrize Decorator , under these circumstances , Every call parameterizes all previous parameterizations .
Two 、pytest.fixture(params)
To be sorted out
reference:
API Reference — pytest documentation
How to parametrize fixtures and test functions — pytest documentation
边栏推荐
- [summer daily question] Luogu p1601 a+b problem (high precision)
- Write some DP
- 207. Curriculum
- [summer daily question] Luogu P6500 [coci2010-2011 3] zbroj
- halcon的安装以及在vs2017中测试,vs2017中dll的配置
- 【暑期每日一题】洛谷 P4414 [COCI2006-2007#2] ABC
- In JS, 0 means false, and non-0 means true
- Meizhi optoelectronics' IPO was terminated: annual revenue of 926million he Xiangjian was the actual controller
- Cross domain problems when downloading webapi interface files
- Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention
猜你喜欢
What are the answers about older bloggers?
NLP introduction + practice: Chapter 5: using the API in pytorch to realize linear regression
[MySQL] - [subquery]
美智光电IPO被终止:年营收9.26亿 何享健为实控人
Meizhi optoelectronics' IPO was terminated: annual revenue of 926million he Xiangjian was the actual controller
What are the principles and methods of implementing functional automation testing?
Use custom annotations to verify the size of the list
【无标题】格式保存
梳理市面上的2大NFT定价范式和4种解决方案
SEGGER 的硬件异常 分析
随机推荐
EF core reading text type is slow_ EF core is slow to read large string fields
写点dp
Halcon installation and testing in vs2017, DLL configuration in vs2017
输出1234无重复的三位数
Go 事,如何成为一个Gopher ,并在7天找到 Go 语言相关工作,第1篇
What are the principles and methods of implementing functional automation testing?
工业互联网行至深水区,落地的路要怎么走?
树莓派的启动流程
RoBERTa:A Robustly Optimized BERT Pretraining Approach
String class
Monitor the bottom button of page scrolling position positioning (including the solution that page initialization positioning does not take effect on mouse sliding)
[deep learning] data preparation -pytorch custom image segmentation data set loading
基于高阶无六环的LDPC最小和译码matlab仿真
国内数字藏品的乱象与未来
【暑期每日一题】洛谷 P7760 [COCI2016-2017#5] Tuna
After the access database introduces DataGridView data, an error is displayed
[untitled] format save
Write some DP
【暑期每日一题】洛谷 P4414 [COCI2006-2007#2] ABC
技术分享| 快对讲综合调度系统