>我有我的测试:
import pytest
def test_normal():
print("normal test runned")
assert 1 == 1
@pytest.mark.production
def test_production():
print("production test runned")
assert 1 == 1
我有我的 conftest.py:
def pytest_addoption(parser):
try:
parser.addoption('--production', action='store_true', dest="production", default=False, help="enable production tests")
except ValueError:
print('option already set')
def pytest_configure(config):
if not config.option.production:
setattr(config.option, 'markexpr', 'not production')
如果我运行:
pytest some_test.py -v -s
只有test_normal运行。
如果我运行:
pytest some_test.py -v -s --production
两个测试都运行。
我该如何使此命令运行两个测试:
pytest some_test.py -v -s -m 'production'
Pytest 文档声明您可以单独使用@pytest.mark
来实现您想要的:@pytest.mark.production
并运行pytest -m production
.
要将--m
与您的方法一起使用:
def pytest_addoption(parser):
try:
parser.addoption('--m', action='store', help="enable production tests")
except ValueError:
print('option already set')
def pytest_configure(config):
if config.option.m != "production":
setattr(config.option, 'markexpr', 'not production')
你不需要任何conftest。您可以直接从命令行执行此操作。 要仅运行生产测试,请执行以下操作:
pytest some_test.py -v -s -m 'production'
要仅运行非生产测试,请执行以下操作:pytest some_test.py -v -s -m 'not production'
要同时运行两者:pytest some_test.py -v -s
或pytest some_test.py -v -s -m 'production or not production'
类似地,您可以使用and
、or
和not
in marker 的组合来运行测试的任意组合。