当前位置:网站首页>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) == expectedRun 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
边栏推荐
- Zero technology is deeply involved in the development of privacy computing financial scenario standards of the ICT Institute
- 零数科技深度参与信通院隐私计算金融场景标准制定
- [daily question in summer] Luogu p6408 [coci2008-2009 3] pet
- Mutationobserver document learning
- Monitor the bottom button of page scrolling position positioning (including the solution that page initialization positioning does not take effect on mouse sliding)
- In JS, 0 means false, and non-0 means true
- SEGGER 的硬件异常 分析
- Meeting notice of OA project (Query & whether to attend the meeting & feedback details)
- What are the principles and methods of implementing functional automation testing?
- EF core reading text type is slow_ EF core is slow to read large string fields
猜你喜欢

NFT 的 10 种实际用途

Jiamusi Market Supervision Bureau carried out special food safety network training on epidemic and insect prevention

jdbc入门

Sqlmap(SQL注入自动化工具)

OA项目之会议通知(查询&是否参会&反馈详情)

Android面试题 | 怎么写一个又好又快的日志库?

《nlp入门+实战:第五章:使用pytorch中的API实现线性回归》

The new generation of public chain attacks the "Impossible Triangle"

SEGGER 的硬件异常 分析

【MYSQL】-【子查询】
随机推荐
Pat class a 1146 topology sequence
NFT 的 10 种实际用途
蓝桥杯A组选数异或
MySQL uses date_ FORMAT(date,'%Y-%m')
Write some DP
Log4qt memory leak, use of heob memory detection tool
Mutationobserver document learning
Amazon cloud assistant applet is coming!
The smallest positive number that a subset of an array cannot accumulate
你学习·我奖励,21天学习挑战赛 | 等你来战
新生代公链再攻「不可能三角」
【暑期每日一题】洛谷 P6336 [COCI2007-2008#2] BIJELE
stm32 操作W25Q256 W25Q16 spi flash
Can the subset of the array accumulate K
Introduction and introduction of logback
Prometheus与Grafana
【深度学习】数据准备-pytorch自定义图像分割类数据集加载
CFdiv1+2-Bash and a Tough Math Puzzle-(线段树单点区间维护gcd+总结)
QT connects two qslite databases and reports an error qsqlquery:: exec: database not open
Multi thread shopping