0
votes
python - pytest is not accepting a custom argument value - Stack Overflow
Asked
Viewed 776 times
0

I'm trying to run pytest with a custom input argument "--trustkey_jks".

Running like this:

sh-4.2$ pytest -m test_e2e.py --trustkey_jks somekey
ERROR: usage: py.test [options] [file_or_dir] [file_or_dir] [...]
py.test: error: argument --trustkey_jks: expected one argument

Also tried like this:

sh-4.2$ py.test --trustkey_jks somekey test_e2e.py
ERROR: usage: py.test [options] [file_or_dir] [file_or_dir] [...]
py.test: error: argument --trustkey_jks: expected one argument

Can omit a file name to let pytest to collect any tests:

sh-4.2$ py.test --trustkey_jks somekey
ERROR: usage: py.test [options] [file_or_dir] [file_or_dir] [...]
py.test: error: argument --trustkey_jks: expected one argument

My conftest.py (same level as tests_e2e.py):

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

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

As far as i understand, pytest fails at parsing input arguments. Appreciate any help.

NOTE: the above is happening inside an Openshift POD, not sure if it matters.

4
  • Did you try =?
    – jpmc26
    May 14 2019 at 17:54
  • Yes, i did try '=' and using quotes like --trustkey_jks=somekey or --trustkey_jks="somekey" May 15 2019 at 7:58
  • Usage says options go before file_or_dir argument, which you don't seem to have provided: usage: py.test [options] [file_or_dir] [file_or_dir] [...]
    – jpmc26
    May 15 2019 at 9:07
  • I edited the question to show I also tried to place options before a file. And also '-m' doesn't make any difference in my case. May 15 2019 at 9:49
0

I am able to reproduce your issue at my end and also found the root-cause.The issue is the conftest.py of pytest functionality.

You need to add all the parser.addoption in your conftest.py as it would be loaded by default from conftest by pytest. And If you want to add command line parameters in your python file(without creating conftest.py), use following snippet:-

parser = argparse.ArgumentParser()
parser.add_argument("--trustkey_jks", action="store_true")

Also, refer the conftest.py features:- https://docs.pytest.org/en/latest/example/simple.html.

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

    Not the answer you're looking for? Browse other questions tagged or ask your own question.

     
    1
    Did you try =?jpmc26
    Yes, i did try '=' and using quotes like --trustkey_jks=somekey or --trustkey_jks="somekey"Sergey Makhonin
    Usage says options go before file_or_dir argument, which you don't seem to have provided: usage: py.test [options] [file_or_dir] [file_or_dir] [...]jpmc26
    I edited the question to show I also tried to place options before a file. And also '-m' doesn't make any difference in my case.Sergey Makhonin

    1 Answers

    0
    votes

    I am able to reproduce your issue at my end and also found the root-cause.The issue is the conftest.py of pytest functionality.

    You need to add all the parser.addoption in your conftest.py as it would be loaded by default from conftest by pytest. And If you want to add command line parameters in your python file(without creating conftest.py), use following snippet:-

    parser = argparse.ArgumentParser()
    parser.add_argument("--trustkey_jks", action="store_true")
    

    Also, refer the conftest.py features:- https://docs.pytest.org/en/latest/example/simple.html.