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

青春壹個敷衍的年華 提交于 2019-12-04 02:14:50

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')
Roland Puntaier

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().

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