How to access config.option command line argument in test script with py.test framework

Viewed 5115

I am taking value of setup from command line argument while running the test with py.test framework.

group.addoption("--setup", "--sC=", action="store", dest="setup", help="setup.")
def pytest_configure(config):
    print "config.option.setup: ", config.option.setup

Here, I am able to get the setup file name with config.option.setup, but the same file name which I pass here, I want to fetch it from my test script.

If I put the same line in my test script, I get below error:

>       print "config.option.setup_config: ", config.option.setup_config
E       NameError: global name 'config' is not defined

Can someone please let me know how can I access config.option.setup in my test script?

2 Answers

You have to create a fixture that extracts this value from pytest's request.

# content of conftest.py
import pytest


def pytest_addoption(parser):
    parser.addoption("--setup", action="store", help="setup.")


@pytest.fixture
def setup_option(request):
    return request.config.getoption("--setup")


# basic usage:
# content of test_anything.py

def test_that(setup_option):
    print("setup_option: %s" % setup_option)
Related