-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
pytest_addoption - Command-line options not added #78
Comments
Original comment by Jason R. Coombs (BitBucket: jaraco, GitHub: jaraco): I'm confused. In every project I use, pytest does discover conftest.py files in subdirectories (and has for a long time). Our tests wouldn't run if it didn't find conftest.py in subdirectories. The two example tests are running from the same directory (the parent of the project). I extracted the zip file to my desktop and here is the full, unedited transcript showing what happens with and without command-line options supplied. {{{ =================================== ERRORS ====================================
Options: reporting: collection: test session debugging and configuration: [pytest] ini-options in the next pytest.ini|tox.ini|setup.cfg file: addopts (args) extra command line options |
Original comment by holger krekel (BitBucket: hpk42, GitHub: hpk42): Here is what happens: During "py.test" the sub directories are traversed for collecting test files. When a conftest.py files is encountered its addoption and configure hooks are executed. This makes default option values available to any test in a subdirectory. However, during a "py.test --help" no sub directories are traversed and no tests are collected. Making "py.test --help" traverse sub directories is not a good idea at least for large projects. Maybe it makes sense to introduce a mechanism for preloading conftest.py files using the config file mechanism. It already mostly works if you put this into a pytest.ini (or tox.ini or setup.cfg): {{{ This means that in your project you get the command line options everywhere. Note that you need to also add a myproj/test/init.py file for this to work. Maybe we could allow filenames so that this requirement is not there. What do you think? |
Original comment by Jason R. Coombs (BitBucket: jaraco, GitHub: jaraco): Oh. That's very interesting. I understand better how it works, and that all makes sense. I think I was assuming that since the add_option was not being called during --help that pytest would never have included that option. Are you saying that pytest will honor those command-line arguments if they're passed, even though they don't appear in --help? Or are you saying that they will be defined their defaults, but to supply those arguments, the tests need to be run from that directory? In my findings, it's the latter. I could not find any way to get the command-line options to be included unless they were processed as the top-level config. I tried using {{{-p myproj.test.conftest}}} (after touching myproj/test/init.py), but that gave me an error: {{{ the above command produces no output because it succeeds.}}} Am I not using -p correctly? Apparently if I set {{{PYTHONPATH='.'}}}, it works as prescribed. After going through all of this, my feeling is it would be sufficient to simply document that command-line options should be defined at the level where it's expected py.test will be run or explicitly loaded using -p. Whether -p supports file-based loading is perhaps the subject for another ticket (though I don't feel strongly enough to create one). Now that I understand better, I'm going to downgrade the priority of this ticket. The explanation was most helpful, but I would suggest adding a note in the docs. Thanks! |
The docs now contain this explanation, so closing this. |
Fixes pytest-dev#78
Originally reported by: Jason R. Coombs (BitBucket: jaraco, GitHub: jaraco)
I created a sample minimal project (attached). In myproj/test/conftest.py you find:
The ValueError was added to quickly determine if pytest is processing the options. Run pytest on the project and it quickly returns:
However, if one passes a command-line argument (even --help), the pytest_addoption is never executed:
Preferably, pytest would process pytest_addoption even when options are supplied. If it can't do this for some reason, it should at least be documented where pytest_addoption is valid.
This behavior was observed on pytest 2.0.3 and 2.1.2 on both Ubuntu Lucid and Windows 7 on Python 2.7 64-bit.
The text was updated successfully, but these errors were encountered: