[英]Mark test as skipped from pytest_collection_modifyitems
How can I mark a test as skipped in pytest collection process?如何在 pytest 收集过程中将测试标记为跳过?
What I'm trying to do is have pytest collect all tests and then using the pytest_collection_modifyitems
hook mark a certain test as skipped according to a condition I get from a database.我想要做的是让 pytest 收集所有测试,然后使用pytest_collection_modifyitems
钩子根据我从数据库中获得的条件将某个测试标记为已跳过。
I found a solution which I don't like, I was wondering if maybe there is a better way.我找到了一个我不喜欢的解决方案,我想知道是否有更好的方法。
def pytest_collection_modifyitems(items, config):
... # get skip condition from database
for item in items:
if skip_condition == True:
item._request.applymarker(pytest.mark.skipif(True, reason='Put any reason here'))
The problem with this solution is I'm accessing a protected member ( _request
) of the class..此解决方案的问题是我正在访问_request
的受保护成员( _request
)..
You were almost there.你快到了。 You just need item.add_marker
你只需要item.add_marker
def pytest_collection_modifyitems(config, items):
skip = pytest.mark.skip(reason="Skipping this because ...")
for item in items:
if skip_condition: # NB You don't need the == True
item.add_marker(skip)
Note that item
has an iterable attribute keywords
which contains its markers.请注意, item
有一个包含其标记的可迭代属性keywords
。 So you can use that too.所以你也可以使用它。
See pytest documentation on this topic.请参阅有关此主题的pytest 文档。
You can iterate over testcases (items) and skip them using a common fixture.您可以迭代测试用例(项目)并使用通用夹具跳过它们。 With 'autouse=True' you shouldn't pass it in each testcase as a parameter:使用 'autouse=True' 你不应该在每个测试用例中将它作为参数传递:
@pytest.fixture(scope='function', autouse=True)
def my_common_fixture(request):
if True:
pytest.skip('Put any reason here')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.