-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Config-file discovery gets confused by custom command-line arguments #9749
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I just realised that #8846 reported more-or-less the same issue. @nicoddemus closed that issue saying that "Unfortunately we don't have a solution to this atm." However, above I proposed one solution - I'm not sure it will work in every case but it would in mine: When finding a conftest.py, look for other files like pytest.ini in that directory and don't look at the command line again. I would like to propose another "solution": forbid the "--arg val" syntax. If "--arg val" didn't work and users were forced to use "--arg=val", we wouldn't have this problem. But this solution can cause backward compatibility problems :-( In any case, I think this is a real pytest bug, that is very frustrating to debug because the user can get errors from spurious configuration files without even knowing where these files are (I ended up adding printouts to pytest's internals to understand what's going on) - so I don't think that this issue or #8846 should be just closed even if we don't have an immediate solution. |
it indeed is a real bug, however disallowing the syntax without the equal sign is unfortunately going to be a pretty painful breaking change thats likely not sitting well with a lot of users that dont hit the issue as it would break their automation/scripts if we ever get around refining config initialization, we can certainly add a certain robustness around conftests, options added and early initialization |
Below is my code which was working perfectly fine till I upgraded pytest to version 7.1.0
and in conftest, I have below code:
This was working fine for earlier pytest versions. With latest one(pytest 7.1.0) its failing with below error:
This is occurring because latest change added for -k syntax: https://docs.pytest.org/en/stable/changelog.html#pytest-7-1-0-2022-03-13 But if I try to remove --aq-cfg, its failing at parsar and not able to locate file without "--". Any help on this would be appreciated @nyh @RonnyPfannschmidt |
Hi @pranjalihande, Some things to try:
|
Please try with the latest releases which has a bugfix wrt conftests finding |
By the way, the original issue above was about configuration files (pytest.ini at all), not conftest.py as is apparently your problem, so it's probably a different problem. Part of the problem in the original issue is arguably the fact there is separate code to find these two different things. |
I am encountering an issue that seemingly relates to this behavior and I wonder if there is a better way of handling it I am maintaining a team wide internal automation test framework that lets users perform end to end and integration tests for company products. As a result the framework itself ships many tests and utility packages/modules alongside it. Due to its size, it also includes unit tests for these utility packages/modules and is organized as
When users launch device tests they often time supply a CLI argument that provides a path to some required bin file somewhere else in the system, often times in CI these bin files are put in the root directory of this project and as a result this path makes it so that the wrong pytest.ini is picked up. Setting One solution is what @nyh has pointed out is to put "=" sign for the troublesome CLI argument, but this is incredibly implicit Are there perhaps better ways of solving this situation I am not aware of? Or maybe in the future there is a way to specify path to pytest.ini file explicitly? Apologies if this is off topic, I found this issue when trying to debug this behavior |
…require extra pytest option https://bugs.webkit.org/show_bug.cgi?id=286497 Reviewed by Carlos Alberto Lopez Perez. Add a `--enable-webdriver-bidi` flag to `run-webdriver-tests`. It'll forward the required `--bidi=1` to Selenium's pytest. Given the plans to control WebDriver-BiDi support behind a feature flag (bug283517), we skipped trying to figure out whether the build supports it, opting for a more explicit approach. During work on this issue, we found a limitation related to a 10-year old argparse issue[1], which leads to pytest's failing to load the right config files[2], which in turn might make it fail to recognize custom CLI switches added by the expected conftest.py. This happens, for example, with `--browser-args` if it receives a space-separated list, as we currently do. So this commit also adds a check for pytest's `ExitCode.USAGE_ERROR` when calling `pytest.main` for clarity. In a follow-up commit, we'll update the buildbot WebDriver steps to use the new `--enable-webdriver-bidi` in `run-webdriver-tests`. Also update some expectations based on recent WebDriver-BiDi commits. [1] python/cpython#66623 [2] pytest-dev/pytest#9749 * Tools/Scripts/run-webdriver-tests: * Tools/Scripts/webkitpy/webdriver_tests/pytest_runner.py: (run): * Tools/Scripts/webkitpy/webdriver_tests/webdriver_selenium_executor.py: (WebDriverSeleniumExecutor.__init__): * Tools/Scripts/webkitpy/webdriver_tests/webdriver_test_runner_selenium.py: (WebDriverTestRunnerSelenium.collect_tests): (WebDriverTestRunnerSelenium.run): * WebDriverTests/TestExpectations.json: Canonical link: https://commits.webkit.org/289454@main
Using the solution by @nyh, for example: |
pytest looks in the tests' directory, or one of its parents directory, for a configuration file such as
pytest.ini
ortox.ini
and uses the first one it finds. But how does pytest know which directory is the test directory? The relevant logic is in_pytest/config/findpaths.py
, starting indetermine_setup()
. So for example if I runThat logic determines that the test directory is "/some/directory/my". So far so good.
The problem starts when the user adds custom pytest arguments in conftest.py. For example, my project has in conftest.py:
And people start a test with
The problem now is that the logic in
_pytest/config/findpaths.py
looks at the command-line arguments, skips (inget_dirs_from_args()
) the argument starting with "-" (in this example,--scylla-path
) but then does not skip its parameter -/some/path
. It then looks for the configuration file in /some/path, which is wrong - and in my case led pytest to find a broken configuration file in that directory and using it.The ideal fix would be for
get_dirs_from_args()
to be called after the command line is parsed and the non-positional arguments are removed. I don't know if we can do this, or we have a chicken and egg problem of what gets read first.Another possible fix is perhaps to first just look for a
conftest.py
(as we already do in_pytest/config/__init__.py
), and if we find one (in my example, it's in in /some/directory/my, not /some/path) also look for pytest.ini in the same directory - NOT look again at all the directories in command line. In other words, it doesn't make too much sense (I think) to pick up conftest.py from one directory, but tox.ini from a different one, and since conftest.py is more important and more pytest-specific, it should be discovered first.By the way, there is a workaround to solving my problem without fixing pytest at all - instead of running
The user just needs to run
With an equals sign instead of a space. This works well because now the findpaths.py code skips the entire option, not just half of it. But I still think this needs a better fix, because a user might not remember to use an equals sign instead of a space, and if they do use a space, the resulting error message is very unhelpful (I got strange warnings coming from the definitions in a wrong tox.ini file, but without telling me which ini file this is coming from, or why this ini file was chosen).
The text was updated successfully, but these errors were encountered: