gpt4 book ai didi

python - 如何为pytest命令指定几个标记

转载 作者:行者123 更新时间:2023-12-03 21:15:48 27 4
gpt4 key购买 nike

阅读http://doc.pytest.org/en/latest/example/markers.html我看到了基于标记包含或排除某些 python 测试的示例。

包含:

pytest -v -m webtest

不包括:
pytest -v -m "not webtest"

如果我想为包含和排除指定多个标记怎么办?

最佳答案

使用 and/or组合多个标记,与 -k 相同选择器。示例测试套件:

import pytest


@pytest.mark.foo
def test_spam():
assert True


@pytest.mark.foo
def test_spam2():
assert True


@pytest.mark.bar
def test_eggs():
assert True


@pytest.mark.foo
@pytest.mark.bar
def test_eggs2():
assert True


def test_bacon():
assert True

选择所有标有 foo 的测试并且没有标有 bar
$ pytest -q --collect-only -m "foo and not bar"
test_mod.py::test_spam
test_mod.py::test_spam2

选择所有未标记为 foo 的测试也不与 bar
$ pytest -q --collect-only -m "not foo and not bar"
test_mod.py::test_bacon

选择标有 foo 中的任何一项的测试, bar
$ pytest -q --collect-only -m "foo or bar"
test_mod.py::test_spam
test_mod.py::test_spam2
test_mod.py::test_eggs
test_mod.py::test_eggs2

关于python - 如何为pytest命令指定几个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60761243/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com