Open Korijn opened 8 years ago
Thanks for the report!
By default pytest_addoption
is not called on all conftest.py
files, only at the root.
From pytest_addoption:
Note This function should be implemented only in plugins or conftest.py files situated at the tests root directory due to how py.test discovers plugins during startup.
I think this logic would have to be extended to also consider conftest.py
files at the root of packages when using the --pyargs
option.
Right, I thought that included the root of packages. Guess not. :)
Just FYI, I found this workaround which works - https://stackoverflow.com/a/43747114/2149641
However it basically makes your tests a callable module instead of using --pyargs
.
I showed up here to file exactly this bug but I see someone has beat me to it. I want to be able to execute tests from an installed location, where my "root" conftest.py
is located in the directory corresponding to mypackage.tests
. I was hoping that pytest --pyargs mypackage.tests
would locate it correctly.
Any progress on fixing this issue? It has recently cropped up in statsmodels.
Also bitten by this while running the statsmodels tests... @al1190
Hey, hello 2017 larsks! I just into the same problem again and forgot that I had previously filed this bug. @nicoddemus, is this a particularly intractable problem, or just not an interesting one?
Hey @larsks,
Now that the dust has settled a bit with pytest 5.0, I will see if I can get a shot at this. Thanks for the ping. 👍
Hi all! I just ran into the same issue, does anyone know a work around?
I couldn't find a way to run the tests within an installed package with custom options, apart from:
$ pytest myenv/lib/python3.7/site-packages/mypackage/tests --custom-option foo
pytest_addoption
to a separate plugin
module and provide it to pytest through -p
:
$ pytest -p mypackage.tests.plugin --pyargs mypackage.tests --custom-option foo
pytest_addoption
to a separate plugin
module and register it using setuptools (it isn't great because this specific module gets loaded at every pytest invocations but it only makes sense for the tests in this particular package)Oh, and thank you for your work :)
EDIT: there was already an answer mentioning a workaround, I should have paid more attention.
I use a pattern where I define a test function in my projects __init__
that looks like:
def test(
extra_args: Optional[Union[str, List[str]]] = None,
exit: bool = True,
append: bool = True,
):
import sys
try:
import pytest
except ImportError:
raise ImportError("Need pytest to run tests")
cmd = ["--tb=auto"]
if extra_args:
if not isinstance(extra_args, list):
pytest_args = [extra_args]
else:
pytest_args = extra_args
if append:
cmd += pytest_args[:]
else:
cmd = pytest_args
pkg = os.path.dirname(__file__)
cmd = [pkg] + cmd
print("running: pytest {}".format(" ".join(cmd)))
status = pytest.main(cmd)
if exit:
sys.exit(status)
This lets me test from an install with
python -c "import pkg;pkg.test()
or python -c "import pkg;pkg.test(['-r a','--skip-slow','-n auto',...)
@bashtage Oh thanks, I didn't notice a similar workaround had already been shared earlier in the thread.
Sorry for the noise :sweat_smile:
How feasible would it be to allow defining options in a conftest.py
within the tests path?
That would mean we didn't need to put the conftest.py
file in the root, and so it wouldn't get missed by python's packaging (I think).
Now that the dust has settled a bit with pytest 5.0, I will see if I can get a shot at this. Thanks for the ping.
We stumbled upon this issue in dandi project as well. Please let us know if dust conditions improved. Cheers.
As part of our build process, we run the test suite after creating the packaging to check that the installed package works as intended. We recently added an option to py.test for some conditional testing, by placing a function
pytest_addoption
in theconftest.py
file that is in the root of the package (so next tomy_pkg/__init__.py
). When calling py.test on the source, this works as intended. Tests are skipped when we omit the option, and they are executed when we pass the option.my_pkg/conftest.py
:However, when we run
py.test --pyargs my_pkg
in an environment where the built package is installed, we see the following error message (even though the test suite is run):All other tests, including those depending on fixtures in the same
conftest.py
file, simply pass.We're running on Windows and Linux, and both on dev machines as on CI (AppVeyor and Codeship), this scenario occurs. We're using the following related packages:
Running with
--traceconfig
does show that theconftest.py
file is registered:PLUGIN registered: <module 'my_pkg.conftest' from 'C:\\Users\\Korijn\\Miniconda3\\envs\\_test\\lib\\site-packages\\my_pkg\\conftest.py'>
full output:
I tried adding a default value:
This avoids the exception, but I still can't pass the option: