How can we accept multiple values for one command line option in pytest

Viewed 994

I would want the user to be able to pass multiple values to one command line argument. For example, if the contents of my contest.py has:

parser.addoption("--url", action="store")

Then I want the user to be able to do this:

python -m pytest test_file.py --url "www.example1.com" "www.example2.com"

This question is basically how to accept multiple arguments for one option.

2 Answers

You should change action from store to append

parser.addoption('-u', '--url',
    type=str,
    action='append')

for me following is working fine

content of conftest.py

import pytest

def pytest_addoption(parser): parser.addoption( "-E", action="append", metavar="NAME", help="only run tests matching the environment NAME.", ) #opts, args = parser.parse_args(["-E"])

def pytest_configure(config): # register an additional marker config.addinivalue_line( "markers", "env(name): mark test to run only on named environment" ) def pytest_runtest_setup(item): envnames = [mark.args[0] for mark in item.iter_markers(name="env")] if envnames: if ( item.config.getoption("-E") is not None )and (not any( x in envnames for x in item.config.getoption("-E")[0].split(",") )): pytest.skip("test requires env in {!r}".format(envnames))

Related