2

I triy to skip particular tests dependent on the value of command-line arguments. I try to get arguments values with pytest.config.getoption("--some-custom-argument") like described in this related question suggestion in the test files and check the arguments values via skipif. But pyest has no config. And getting argument values via request.config.getoption("--some-custom-argument") seems only work in fixture functions. Can I get command line arguments prior to test execution somehow different that I can check them in skipif on a file scope level?

thinwybk
  • 4,193
  • 2
  • 40
  • 76

3 Answers3

3

Since the tests are collected after the configuration stage and before the test collection (i.e. also before test execution), the pytest.config is available at the module levelin test modules. Example:

# conftest.py
def pytest_addoption(parser):
    parser.addoption('--spam', action='store')

# test_spam.py
import pytest


print(pytest.config.getoption('--spam'))


@pytest.mark.skipif(pytest.config.getoption('--spam') == 'eggs', 
                    reason='spam == eggs')
def test_spam():
    assert True

Running with --spam=eggs yields:

$ pytest -vs -rs --spam=eggs
============================== test session starts ================================
platform linux -- Python 3.6.5, pytest-3.4.1, py-1.5.3, pluggy-0.6.0 -- /data/gentoo64/usr/bin/python3.6
cachedir: .pytest_cache
rootdir: /data/gentoo64/home/u0_a82/projects/stackoverflow/so-50681407, inifile:
plugins: mock-1.6.3, cov-2.5.1, flaky-3.4.0
collecting 0 items                                                                                                     
eggs
collected 1 item

test_spam.py::test_spam SKIPPED
============================ short test summary info ==============================
SKIP [1] test_spam.py:7: spam == eggs

=========================== 1 skipped in 0.03 seconds =============================
hoefling
  • 59,418
  • 12
  • 147
  • 194
1

If i understand the question right, you might want to look at this answer.

It suggests to use a fixture with a request object, and read input argument value from there request.config.getoption("--option_name") or request.config.option.name.

Code snippet (credit goes to ipetrik):

# test.py
def test_name(name):
    assert name == 'almond'


# conftest.py
def pytest_addoption(parser):
    parser.addoption("--name", action="store")

@pytest.fixture(scope='session')
def name(request):
    name_value = request.config.option.name
    if name_value is None:
        pytest.skip()
    return name_value
Sergey Makhonin
  • 350
  • 3
  • 13
0

You can try to do like that

@pytest.mark.skipif(pytest.config.option.some-custom-argument=='foo', 
                    reason='i do not want to run this test')

But why not to use mark expressions?

  • I want to skip dependent on value not dependent on non-/existence. As gar as this works with your suggestion I am happy. – thinwybk Jun 04 '18 at 17:16
  • @thinwybk If you know the cases you're skipping over are completely irrelevant to the value data dependency it's much cleaner to actually totally remove them in `def pytest_collection_modifyitems(config, items):` by manipulating the items list inplace, which could be accomplished by making a temp list and assigning its values like `items[:] = applicable_items`. That way the test report is a lot cleaner with no skipped items should there be a large volume of them related to the data value. – jxramos Jan 19 '21 at 17:54