4

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.

hoefling
  • 59,418
  • 12
  • 147
  • 194
Rajat Bhardwaj
  • 129
  • 1
  • 11

2 Answers2

1

You should change action from store to append

parser.addoption('-u', '--url',
    type=str,
    action='append')
frankegoesdown
  • 1,898
  • 1
  • 20
  • 38
  • but this is not working as expected! Now when I pass something like this python -m pytest test_file.py --url "www.example1.com" "www.example2.com" it gives an error saying "file not found: www.example2.com" unless I remove the space between the two. So if I pass python -m pytest test_file.py --url "www.example1.com""www.example2.com" (without spaces between params) then it actually appends, but as one single string like this: ['www.example1.comwww.example2.com'] – Rajat Bhardwaj Jan 14 '19 at 09:30
  • @RajatBhardwaj args = parser.parse_args() – frankegoesdown Jan 14 '19 at 10:05
  • Can you please elaborate? These are the contents of my contest.py: `def pytest_addoption(parser): parser.addoption("--url", type=str, action="append") parser = addoption(parser) ` and then I use it as this: '''def pytest_configure(config): config = configure(config) if config.getoption("url"): urls = config.getoption("url") ''' – Rajat Bhardwaj Jan 14 '19 at 10:27
-1

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))