question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

parser.addoption has a buggy argument parser (relative path-like strings)

See original GitHub issue

If I provide an existing relative path to pytest via parser.addoption then I get the following error message:

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]
pytest: error: unrecognized arguments: --var-file
  inifile: None
  rootdir: /home/user/workspace

This does not happen to a relative path that is non-existent.

If you add an equal sign (=) instead of a blank space, this error does not occur.

This is slightly confusing to me as I do not see why parser.addoption cares about whether we’re passing in a file (I would have thought it was just deal with the string). And if it does care about whether something is a file, why is it OK with a nonexistent file?

A minimal bit of code that will reproduce this, conftest.py

def pytest_addoption(parser):
    parser.addoption(
        "--var-file",
        action="store",
    )

Place this in a folder with an existing relative file above it, e.g.:

foo.txt
test/conftest.py
test/bar.txt

Enter the test folder and execute pytest --var-file ../foo.txt and observe the error message above.

Running pytest --var-file bar.txt will not produce an error message Running pytest --var-file ../notthere.txt will not produce the error message Running pytest --var-file=../foo.txt will not produce the error message

Output of pip freeze:

pip freeze
atomicwrites==1.3.0
attrs==19.3.0
importlib-metadata==1.5.0
more-itertools==8.2.0
packaging==20.3
pluggy==0.13.1
py==1.8.1
pyparsing==2.4.6
pytest==5.3.5
six==1.14.0
wcwidth==0.1.8
zipp==3.1.0
lsb_release -a
LSB Version:	n/a
Distributor ID:	ManjaroLinux
Description:	Manjaro Linux
Release:	18.1.5
Codename:	Juhraya
python --version
Python 3.8.1

Note, observed this in pytest 5.0.1 as well.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
themanifoldcommented, Mar 13, 2020

@RonnyPfannschmidt Sorry, I’m not super experienced with pytest (or confest). I’m not sure what you mean by your message.

I don’t understand why pytest is trying to register a plugin when I use the addoption hook to create a unrelated flag.

When you say move “it” up one folder, do you mean confest? I can’t really do that in my example because the file I’m referring to is not part of the test project - conftest belongs in the test folder IMO.

0reactions
adrian-argcommented, Jan 10, 2021

I’m getting the same behavior I’m building a little framework for a demo and I have the webdrivers in a folder called webdrivers.

If I use something like –driver_path webdrivers/chromedriver, it breaks with the error:

“ERROR: usage: _jb_pytest_runner.py [options] [file_or_dir] [file_or_dir] […] _jb_pytest_runner.py: error: unrecognized arguments: --driver_path inifile: /home/adrian/Projects/myframework/pytest.ini rootdir: /home/adrian/Projects/myframework”

Using it like this makes it work as stated above –driver_path=webdrivers/chromedriver

Using anything that doesn’t match a folder works as well –driver_path linux/chromedriver

Read more comments on GitHub >

github_iconTop Results From Across the Web

argparse — Parser for command-line options, arguments ...
Create a new ArgumentParser object. All parameters should be passed as keyword arguments. Each parameter has its own more detailed description below, but...
Read more >
Getting full path from the relative path with Argparse
It can take any callable that takes a single string argument and returns the converted value: parser.add_argument("-src", required=True, ...
Read more >
jsonargparse — jsonargparse documentation
This package is an extension to python's argparse which simplifies parsing of configuration options from command line arguments, json configuration files ...
Read more >
Python: Loading pathlib Paths with argparse
All command line arguments come in as strings. The type parameter just takes a function that accepts a string and returns a value....
Read more >
Source code for _pytest.config.argparsing
The returned group object has an ``addoption`` method with the same ... After command line parsing, options are available on the pytest config...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found