运行 循环中的参数化 pytest
Running parameterized pytests in a loop
我正在尝试 运行 参数化数据循环中的 pytest
@pytest.mark.repeat(2)
@pytest.mark.parametrize("username, password",[('abc@abc.com','aaa'),
('def@def.com','ddd'),('efg@efg.com','eee')])
如果我运行上面的pytest,执行顺序是
( 'abc@abc.com','aaa') executed 2 times , then ('def@def.com','ddd') is executed 2 times and then ('efg@efg.com','eee') is executed.
我正在尝试实现每次重复的每个参数化值都是 运行 的序列。使用@pytest.mark.repeat(2),我试图获得以下输出:
for 1st repeat: ('abc@abc.com','aaa'),('def@def.com','ddd'),('efg@efg.com','eee')
for 2nd repeat: ('abc@abc.com','aaa'),('def@def.com','ddd'),('efg@efg.com','eee')
任何指点将不胜感激。
您可以将重复放在 class 上,然后将 --repeat-scope
参数设置为 class
。 See the docs:
If you want to override default tests executions order, you can use --repeat-scope command line option with one of the next values: session, module, class or function (default). It behaves like a scope of the pytest fixture.
function (default) scope repeats each test count or repeat times before executing next test. session scope repeats whole tests session, i.e. all collected tests executed once, then all such tests executed again and etc. class and module behaves similar session , but repeating set of tests is a tests from class or module, not all collected tests.
@pytest.mark.repeat(2)
class Test:
@pytest.mark.parametrize(
"username, password",
[("abc@abc.com", "aaa"), ("def@def.com", "ddd"), ("efg@efg.com", "eee")],
)
def test(self, username, password):
print(username, password)
然后运行pytest -s --repeat-scope class test.py
:
============================ test session starts =============================
platform darwin -- Python 3.9.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/flakes/workspace
plugins: requests-mock-1.9.2, cov-2.12.0, mock-3.6.1, repeat-0.9.1
collected 6 items
test.py abc@abc.com aaa
.def@def.com ddd
.efg@efg.com eee
.abc@abc.com aaa
.def@def.com ddd
.efg@efg.com eee
.
============================= 6 passed in 0.01s ==============================
我正在尝试 运行 参数化数据循环中的 pytest
@pytest.mark.repeat(2)
@pytest.mark.parametrize("username, password",[('abc@abc.com','aaa'),
('def@def.com','ddd'),('efg@efg.com','eee')])
如果我运行上面的pytest,执行顺序是
( 'abc@abc.com','aaa') executed 2 times , then ('def@def.com','ddd') is executed 2 times and then ('efg@efg.com','eee') is executed.
我正在尝试实现每次重复的每个参数化值都是 运行 的序列。使用@pytest.mark.repeat(2),我试图获得以下输出:
for 1st repeat: ('abc@abc.com','aaa'),('def@def.com','ddd'),('efg@efg.com','eee')
for 2nd repeat: ('abc@abc.com','aaa'),('def@def.com','ddd'),('efg@efg.com','eee')
任何指点将不胜感激。
您可以将重复放在 class 上,然后将 --repeat-scope
参数设置为 class
。 See the docs:
If you want to override default tests executions order, you can use --repeat-scope command line option with one of the next values: session, module, class or function (default). It behaves like a scope of the pytest fixture.
function (default) scope repeats each test count or repeat times before executing next test. session scope repeats whole tests session, i.e. all collected tests executed once, then all such tests executed again and etc. class and module behaves similar session , but repeating set of tests is a tests from class or module, not all collected tests.
@pytest.mark.repeat(2)
class Test:
@pytest.mark.parametrize(
"username, password",
[("abc@abc.com", "aaa"), ("def@def.com", "ddd"), ("efg@efg.com", "eee")],
)
def test(self, username, password):
print(username, password)
然后运行pytest -s --repeat-scope class test.py
:
============================ test session starts =============================
platform darwin -- Python 3.9.4, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/flakes/workspace
plugins: requests-mock-1.9.2, cov-2.12.0, mock-3.6.1, repeat-0.9.1
collected 6 items
test.py abc@abc.com aaa
.def@def.com ddd
.efg@efg.com eee
.abc@abc.com aaa
.def@def.com ddd
.efg@efg.com eee
.
============================= 6 passed in 0.01s ==============================