Pytest参数化在最后一次迭代后运行



使用@pytest.mark.parametrize('arg', param)时,是否有办法查明param中的最后一项是否正在运行?我之所以这么问,是因为我想运行一个测试特有的清理函数,该函数应该只在param的最后一次迭代之后运行。

param = [(1, 'James'), (2, 'John')]
@pytest.mark.parametrize('id, user', param)
def test_myfunc(id, user):
# Do something
print(user)
# Run this only after the last param which would be after (2, 'John')
print('All done!')

我可以运行一个条件来检查param的值,但我只是想知道pytest是否有办法做到这一点。

您需要在pytest钩子,特别是pytest_runtest_teardown钩子中执行此逻辑。

假设您的测试如下所示,

import pytest
param = [(1, 'James'), (2, 'John')]
@pytest.mark.parametrize('id, user', param)
def test_myfunc(id, user):
print(f"Iteration number {id}")

在测试文件夹的根目录中,创建一个conftest.py文件,并放置以下

func_of_interest = "test_myfunc"
def pytest_runtest_teardown(item, nextitem):
curr_name = item.function.__qualname__
# check to see it is the function we want
if curr_name == func_of_interest:
# check to see if there are any more functions after this one
if nextitem is not None:
next_name = nextitem.function.__qualname__
else:
next_name = "random_name"
# check to see if the next item is a different function
if curr_name != next_name:
print("nPerform some teardown once")

然后,当我们运行它时,它会产生以下输出,

===================================== test session starts ======================================
platform darwin -- Python 3.9.1, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
cachedir: .pytest_cache
rootdir: ***
collected 2 items                                                                              
test_grab.py::test_myfunc[1-James] Iteration number 1
PASSED
test_grab.py::test_myfunc[2-John] Iteration number 2
PASSED
Perform some teardown once

正如我们所看到的,在测试调用的最后一次迭代之后,拆卸逻辑只被调用了一次。

相关内容

  • 没有找到相关文章