Default skip test unless command line parameter present in py.test

試著忘記壹切 提交于 2019-12-05 16:43:30

问题


I have a long run test, which lasts 2 days, which I don't want to include in a usual test run. I also don't want to type command line parameters, that would deselect it and other tests at every usual test run. I would prefer to select a default-deselected test, when I actually need it. I tried renaming the test from test_longrun to longrun and use the command

py.test mytests.py::longrun

but that does not work.


回答1:


try to decorate your test as @pytest.mark.longrun

in your 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')



回答2:


Alternatively to the pytest_configure solution above I had found pytest.mark.skipif.

You need to put pytest_addoption() into conftest.py

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

And you use skipif in the test file.

import pytest

longrun = pytest.mark.skipif(
      not pytest.config.option.longrun,
      reason="needs --longrun option to run")

def test_usual(request):
    assert false, 'usual test failed'

@longrun
def test_longrun(request):
    assert false, 'longrun failed'

In the command line

py.test

will not execute test_longrun(), but

py.test --longrun

will also execute test_longrun().



来源:https://stackoverflow.com/questions/33084190/default-skip-test-unless-command-line-parameter-present-in-py-test

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!