Roland Puntaier
Roland Puntaier

Reputation: 3511

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

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.

Upvotes: 32

Views: 11201

Answers (3)

Roland Puntaier
Roland Puntaier

Reputation: 3511

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 config.getoption('longrun')")

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

Upvotes: 32

binaryfunt
binaryfunt

Reputation: 7127

This is a slightly different way.

Decorate your test with @pytest.mark.longrun:

@pytest.mark.longrun
def test_something():
    ...

At this point you can run everything except tests marked with that using -m 'not longrun'

$ pytest -m 'not longrun'

or if you only want to run the longrun marked tests,

$ pytest -m 'longrun'

But, to make -m 'not longrun' the default, in pytest.ini, add it to addopts:

[pytest]
addopts =  
    -m 'not longrun'
    ...

If you want to run all the tests, you can do

$ pytest -m 'longrun or not longrun'

Upvotes: 13

sax
sax

Reputation: 3806

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

Upvotes: 20

Related Questions