有没有办法在PyTest夹具中定义标记?
当我在pytest中指定-m "not slow"时,我正在尝试禁用缓慢的测试。
我可以禁用单个测试,但不能禁用用于多个测试的fixture。
我的fixture代码如下所示:
@pytest.fixture()
@pytest.mark.slow
def postgres():
# get a postgres connection (or something else that uses a slow resource)
yield conn 几个测试都有这样的一般形式:
def test_run_my_query(postgres):
# Use my postgres connection to insert test data, then run a test
assert ...我在https://docs.pytest.org/en/latest/mark.html (updated link)上找到了下面的评论:
标记只能应用于测试,对夹具没有影响。这个评论的原因是,fixture本质上是函数调用,而标记只能在编译时指定吗?
有没有一种方法可以指定使用特定fixture (在本例中为postgres)的所有测试都可以标记为慢速,而无需在每个测试中指定@pytest.mark.slow?
发布于 2019-10-22 03:42:20
看起来你已经在文档中找到了答案。订阅https://github.com/pytest-dev/pytest/issues/1368观看此功能,可能会在较新的pytest版本中添加。
现在,你可以采取一些办法来解决这个问题:
# in conftest.py
def pytest_collection_modifyitems(items):
for item in items:
if 'postgres' in getattr(item, 'fixturenames', ()):
item.add_marker("slow")https://stackoverflow.com/questions/58492764
复制相似问题