Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 840
Roland Puntaier Avatar asked Oct 12 '15 14:10

Roland Puntaier


People also ask

How do I skip a test in pytest?

The simplest way to skip a test is to mark it with the skip decorator which may be passed an optional reason . It is also possible to skip imperatively during test execution or setup by calling the pytest. skip(reason) function. This is useful when it is not possible to evaluate the skip condition during import time.

Which of the following decorator is used to skip a test unconditionally with pytest?

Skipping test functions. The simplest way to skip a test function is to mark it with the skip decorator which may be passed an optional reason : @pytest. mark.

What is pytest mark?

Pytest allows us to use markers on test functions. Markers are used to set various features/attributes to test functions. Pytest provides many inbuilt markers such as xfail, skip and parametrize. Apart from that, users can create their own marker names.


1 Answers

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

like image 120
Roland Puntaier Avatar answered Nov 12 '22 21:11

Roland Puntaier