|
I believe there may be an issue with extending command-line args via I currently have a plugin our team uses which dynamically generates the flag for JUnit report files. It works by defining the I have also added some features to this plugin to add some additional CLI flags and, with everything enabled, However, none of the other flags result in their anticipated effects:
I have confirmed the contents via debugging the plugin when used from an editable install in a separate project. In this separate project, I also have a test to debug which uses the For reference, the plugin is open-source here: https://github.com/neuralmagic/pytest-nm-releng A simple set of test files I am using locally to test it (the unit tests all work to the best of my capability with regards to testing the plugin) – will not include any of the pytest-cov flags by design: # test file: test_example.py
# requirements: pytest, pytest-nm-releng
import pytest
def test_example(request: pytest.FixtureRequest):
print(request.config.option)# running with flags added via plugin - report naming works, rest doesn't
NMRE_JUNIT_BASE="test-results" NMRE_JUNIT_PREFIX="junit-report-" NMRE_JUNIT_FULL="1" pytest test_example.py
# running with flags added via PYTEST_ADDOPTS - work as expected
PYTEST_ADDOPTS='--junit-xml test-results/report-pytest_addopts.xml -o junit_logging=all -o junit_log_passing_tests=True' pytest test_example.py
# running with flags passed directly - works as expected
pytest test_example.py --junit-xml test-results/report-direct.xml -o junit_logging=all -o junit_log_passing_tests=True |
Replies: 1 comment 1 reply
|
Hi @dbarbuzzi The issue is when those arguments are being added.
That explains why the values can appear later in I would not solve this with For dynamically generated defaults that must behave exactly like real command-line options, generate them before pytest starts configuration, for example with a small wrapper entry point: import sys
import pytest
def main():
generated_args = [
"--junit-xml=test-results/report.xml",
"-o",
"junit_logging=all",
"-o",
"junit_log_passing_tests=True",
"--cov=MODULE_NAME",
]
raise SystemExit(pytest.main([*generated_args, *sys.argv[1:]]))Another valid option is to generate So the argument mutation itself is not generally broken; these particular options depend on startup work that has already happened by the time your hook adds them. Please mark this answer as accepted if it helped, thank you. |
Hi @dbarbuzzi
The issue is when those arguments are being added.
pytest_load_initial_conftestscan modifyargs, but not every option can safely be introduced there. In particular:-o junit_logging=.../-o junit_log_passing_tests=...are too late because pytest has already processed the ini overrides before this hook finishes.pytest-covalso usespytest_load_initial_conftestsand declares it withtryfirst=True, so adding--cov...from another implementation of the same hook can happen after pytest-cov has already inspected the early options.That explains why the values can appear later in
request.config.optioneven though the corresponding plugin behavior was never initialized from them.