Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip test depending on parameter in py.test

Tags:

python

pytest

I have a test fixture with session scope which is parametrized, e.g.

@pytest.fixture(scope="session", params=["one", "two", "three"])
def myfixture():
    ...

In my directory, I have files which use pytest.mark.usefixtures("myfixture") and one file which contains tests should be run only for myfixture with "two" parameter and py.test should skip it otherwise.

Are there any ways to achieve this in py.test or do I need to set a special variable in some class in myfixture() function?

like image 573
DuXeN0N Avatar asked Jul 15 '15 17:07

DuXeN0N


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.

What does pytest Mark skip do?

The skip is one such marker provided by pytest that is used to skip test functions from executing. We can specify why we skip the test case using the reason argument of the skip marker.

How do I ignore warnings in pytest?

Use warnings. filterwarnings() to ignore deprecation warnings Call warnings. filterwarnings(action, category=DeprecationWarning) with action as "ignore" and category set to DeprecationWarning to ignore any deprecation warnings that may rise.

How do I run a specific test case in pytest?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .


1 Answers

Found solution myself, one can define function in conftest.py:

def pytest_namespace():
    return {"param": None}

And in fixture function we can do:

@pytest.fixture(scope="session", params=["one", "two", "three"])
def myfixture():
    pytest.param = request.param
    # ...

So we can wrap test class with:

@pytest.mark.skipif("pytest.param == 'value'")
class TestSmth(object):
    ...
like image 75
DuXeN0N Avatar answered Oct 05 '22 00:10

DuXeN0N