默认跳过测试,除非命令行参数出现在py.tes公司

2024-09-27 17:54:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个长时间运行测试,持续2天,我不想把它包括在通常的测试运行中。我也不想输入命令行参数,这样会在每次正常的测试运行中取消选择它和其他测试。我宁愿选择一个默认的取消选择的测试,当我真正需要它的时候。我尝试将测试从test_longrun重命名为longrun,并使用命令

py.test mytests.py::longrun

但这行不通。在


Tags: 命令行pytest命令参数重命名mytestslongrun
2条回答

作为上述pytest_configure解决方案的替代方案,我找到了pytest.mark.skipif。在

您需要将pytest_addoption()放入conftest.py

def pytest_addoption(parser):
    parser.addoption(' longrun', action='store_true', dest="longrun",
                 default=False, help="enable longrundecorated tests")

在测试文件中使用skipif。在

^{pr2}$

在命令行中

py.test

不会执行test_longrun(),但是

py.test  longrun

也将执行test_longrun()。在

尝试将测试装饰为@pytest.mark.longrun

在你的conftest.py

def pytest_addoption(parser):
    parser.addoption(' longrun', action='store_true', dest="longrun",
                 default=False, help="enable longrundecorated tests")

def pytest_configure(config):
    if not config.option.longrun:
        setattr(config.option, 'markexpr', 'not longrun')

相关问题 更多 >

    热门问题