I run pytest from command line using the command:
python -m pytest tests --server_image default
where --server_image is an argument defined in conftest.py file:
def pytest_addoption(parser):
parser.addoption("--server_image", action="store_true", default="build_image")
parser.addoption("--other_attr", action="store_true", default="True")
...
with 2 possible string values build_image and default. tests is a directory with the tests. I got an error message ERROR: file or directory not found: default. So pytest treats default as path to the test directory and not as a value of the argument. When i do not pass any argument everything works. Pytest version 7.0.0, python 3.8.12. Does anyone has a clue what could be wrong?
CodePudding user response:
In your conftest.py file, --server_image is set to action="store_true", which means the variable is expected to be in a boolean format that becomes True if you call --server_image on the command-line. When doing so, --server_image can take no additional arguments, so the default becomes a completely separate argument. It would be the equivalent of calling:
python -m pytest default tests --server_image
... which is NOT what you want because you'll get the error from default unless that's the name of a folder.
Instead, you should change your conftest.py file so that you use action="store" instead for --server_image. And you'll need to set a destination into the dest field. That will let you use --server_image default so that you can assign a value.
def pytest_addoption(parser):
parser.addoption(
"--server_image",
action="store",
dest="server_image",
default="build_image",
)
parser.addoption(
"--other_attr",
action="store",
dest="other_attr",
default=None,
)
