-
Notifications
You must be signed in to change notification settings - Fork 66
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
Document how to use pytest in packages using meson-python #642
Comments
That does seem like a good idea - this causes confusion regularly. Related: I happen to have just written some docs on this topic for SciPy: http://scipy.github.io/devdocs/building/redistributable_binaries.html#stripping-the-test-suite-from-a-wheel-or-installed-package. |
I was honestly expecting someone to tell me that the issue is me being thick, not a limitation of pytest. The
works without problems... One more reason why I'm not a big fan of pytest. |
I'm not familiar with the internal implementation details of pytest or unittest. Purely looking at how the imports work, given a mymodule/ directory and a tests/ directory, you just need them to be usable independently in any way shape or form, which is always the case so no problems. If you use mymodule/tests/ then the individual files inside mymodule/tests/*.py could have things like IMO it's a really bad idea to have the test import names be "mymodule.tests" and a testsuite loader should otherwise be as unopinionated as it can get away with. If you do the bad idea though, then you need the entire built/installed/editable version of the project to also build/install/editable-install the tests. A common issue there is that setuptools copies the entire installable project to build/ (and then copies it again into the wheel) but if the tests aren't installable then they don't get copied -- hence adding the build/lib*/ directory to the import path means that that copy is preferred for importing, but cannot import the tests. Mix that with compiled extensions and you cannot use the build/ version (no tests), nor the original sources (no compiled modules). And thus people started using |
I don't want to defend the practice of having tests in the same directory as the Python source. I'm quite indifferent about it: I see the value of having the tests next to the code, and I see how segregating the tests in their own top level directory makes things easier to keep organized. However, there is not |
Fascinating. Since foo_test.py doesn't depend on the structure (I daresay it doesn't do Maybe unittest tries to assume the test files are individual/standalone but pytest doesn't. |
Is it possible to add an editable-install style loader that tricks pytest into seeing them all as importable using a merged structure? |
I think it would be possible, but it is much easier to just install the tests alongside the code, or to move them in a different directory structure. |
I believe that pytest loads test code in the way it does to allow some of its "magic" functions. The most likely culprit is the auto loading of |
There are two common directory layouts used by Python packages: tests source code alongside the Python source code, or tests source code in a dedicated package top-level directory. Some packages install the tests and some do not. AFAIU pytest 8.0 and later do not allow to have the tests files alongside the Python source code and execute them without installing them.
Example, given a package source code structure looking like:
The package needs to be installed to build the Cython extension module:
However, if
foo_test.py
is not installed, runningpytest package/
results in tests loading errors because pytest importspacakage.fo_test
, which puts thepackage
folder in the source directory in the modules cache, and it does not contain the_foo
extension module. With pytest version previous to the 8.0 release, using the--import-mode=importlib
option solved the issue. However, this is not true for pytest 8.0 and later.The only solution I've found is to install the tests alongside the package. Then
pytest package/
works as expected.If a project does not want to have the tests as part of the distributed wheels, it needs either to add a meson build option that disables installing the tests and set it when building wheels for redistribution and not for development builds, or it needs to use install tags to filter the tests out and use
-Cinstall-args=--tags=python-runtime,tests
in development builds. I don't like the latter solution much, because it requires listing all the tags to be installed.The pytest issue is not meson-python specific, it is more specific to packages using extension modules, but it is complex enough that it may be worth a mention somewhere in the documentation. Maybe we need a Development with meson-python section in the documentation.
The text was updated successfully, but these errors were encountered: