Hi! I'm trying to find a way to use doctests, but not for Windows. It seems that there's a way to do this at: https://docs.pytest.org/en/latest/example/simple.html#dynamically-adding-command-line-options
I tried the following minimal example but it did not work:
import platform
def pytest_load_initial_conftests(args):
if platform.system() != 'Windows':
args[:] = args + ['--doctest-modules']
Is there something I'm missing here?
Here's a minimum verifiable example.
Sorry for not looking into the zip, but I think you cannot use it from a conftest (IIRC you mentioned this on IRC).
You could use pytest_cmdline_preparse, but that is deprecated - see https://github.com/blueyed/pytest/blob/983883af16897b6c9868d3a23383646329cccaf9/testing/test_config.py#L679-L687.
Or use a real plugin, which you can add using -p in addopts.
The documentation states "dynamically adding command line options"... That's more or less exactly what I'm doing, right? It even states, in the comment:
# content of conftest.py
I may be missing something, but it seems that either the documentation is out of date or there's an actual bug.
Yes, it might be a bug.
Maybe the hook needs to be called after conftests are handled (historic)?
But I also think there should be a distinct/clear hook for manipulating args, i.e. it might make sense to undeprecate pytest_cmdline_preparse.
@hameerabbasi can you check if pytest_cmdline_preparse works for you? If it does, we might investigate why it is deprecated, as well as investigate the reason why your initial attempt with pytest_load_initial_conftests did not work.
@nicoddemus pytest_cmdline_preparse worked for me. Here's the updated ZIP.
pytest_test.zip
Edit: I also tried moving conftest.py out from tests into . and that did not work either.
I'd be willing to make a PR here, if it's a simple change to the docs. 馃槃
Yes, it might be a bug.
Maybe the hook needs to be called after conftests are handled (historic)?But I also think there should be a distinct/clear hook for manipulating args, i.e. it might make sense to undeprecate
pytest_cmdline_preparse.
I'm curious if there's been any movement on this. I use arg manipulation with pytest_cmdline_preparse in most my test cases, but I've noticed that it has not worked since version 5.2.4. At least, I see no way of accessing manipulated args from within a fixture that calls request.config.getoption.
Neither does pytest_load_initial_conftests seem to have any effect within the root conftest.py, as per the documentation. Is there a workaround somewhere in the documentation that I'm missing? Would love to get this working again 馃槃
Most helpful comment
Yes, it might be a bug.
Maybe the hook needs to be called after conftests are handled (historic)?
But I also think there should be a distinct/clear hook for manipulating args, i.e. it might make sense to undeprecate
pytest_cmdline_preparse.