tHeReaver
tHeReaver

Reputation: 244

Passing arguments to pytest.mark.parametrize via command line arguments

I am trying to pass command line arguments to pytest tests. Example from this question

print ("Displaying name: %s" % name)

In conftest

parser.addoption("--name", action="store", default="default name")


def pytest_generate_tests(metafunc):
    # This is called for every test. Only get/set command line arguments
    # if the argument is specified in the list of test "fixturenames".
    option_value = metafunc.config.option.name
    if 'name' in metafunc.fixturenames and option_value is not None:
        metafunc.parametrize("name", [option_value])

I want to pass name argument to pytest.mark.parametrize. Now, I am aware that according to the question I've referenced, the answer mentions that I shouldn't use @pytest.mark.parametrize. However, I am working on a code base that already utilizes it and I also need to pass arguments as part of CI process. Is there a work around or am I doomed to re-write all of the tests?

Edit: Since I wasn't clear enough- I cannot use the command line argument in mark.parametrize with pytest console line args. Example- I have a function that returns a list and has input for name and I add it to the mark.parametrize:

@pytest.mark.parametrize("return_list", foo(name)):
def test_name(return_list):
    pass

The name parameter, like I mentioned needs to come from the command line, and this doesn't work in any method that I've encountered. I don't know how to use a list in pytest without mark.parametrize, so it's a problem

Upvotes: 1

Views: 1323

Answers (1)

tHeReaver
tHeReaver

Reputation: 244

Ok, so I've found an answer using pytest-lazy-fixture. It can be pip installed for any version of pytest higher than 3.2.5 (and included). The Gist is calling a custom fixture (conftest of the test itself) by its keyword. Will leave a full answer here for future reference:

Full example:

In conftest

def pytest_addoption(parser):
    parser.addoption("--name", action="store", default="default name")

@pytest.fixture(scope = 'module', autouse = True)
def names(request):
    """ main code and manipulation on the data """
    return request.config.getoption("--name")

In test.py

@pytest.mark.parametrize('arg1',
[pytest.lazy_fixture('names')]
)
def test_something(arg1):
    # do something here
    pass

This also works for mark.parametrize if the lazy_fixture is fed into another function.

Upvotes: 1

Related Questions