【发布时间】:2020-02-17 22:11:44
【问题描述】:
有没有办法在 PyTest 夹具中定义标记?
当我在 pytest 中指定 -m "not slow" 时,我试图禁用慢速测试。
我已经能够禁用单个测试,但不能禁用我用于多个测试的夹具。
我的夹具代码如下所示:
@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) 中发现了以下评论:
“标记只能应用于测试,对夹具没有影响。”这个注释的原因是夹具本质上是函数调用和标记只能在编译时指定吗?
有没有办法指定使用特定夹具(在本例中为 postgres)的所有测试都可以标记为慢而不在每个测试上指定 @pytest.mark.slow?
【问题讨论】:
标签: pytest