From 8a89fa5a9dbacbce3a237d0e8ad91f0ffcb490bd Mon Sep 17 00:00:00 2001 From: Michał Górny Date: Sat, 5 Aug 2023 07:12:39 +0200 Subject: Update Guide to 8e5a339 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michał Górny --- guide/test.html | 52 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) (limited to 'guide/test.html') diff --git a/guide/test.html b/guide/test.html index 08b95de..0090c43 100644 --- a/guide/test.html +++ b/guide/test.html @@ -358,6 +358,14 @@ in the staging area or build directory. In fact, this is often necessary to prevent import collisions — e.g. when modules would be loaded first from the staging area due to explicit imports then again from the current directory due to test discovery.

+
+

Warning

+

Not all packages fail explicitly like that. In particular, +if the C extensions are optional, the package may either skip tests +requiring them or silently fall back to testing the pure Python +variant. Special caution is advised when packaging software using +C extensions with top-level source layout.

+

Unfortunately, this does not work correctly if C extensions are built as part of these packages. Since the package imported relatively does not include the necessary extensions, the imports fail, e.g.:

@@ -387,23 +395,49 @@ does not include the necessary extensions, the imports fail, e.g.:

E ImportError: cannot import name 'login' from 'systemd' (/tmp/portage/dev-python/python-systemd-234-r2/work/python-systemd-234/systemd/__init__.py) -

The preferred solution is to change the working directory before running -the tests. If tests are installed as a part of the package, they can -be discovered through package search e.g. using pytest:

+
+

Note

+

Historically, the primary recommendation was to change directory +prior to running the test suite. However, this was change since +the rm approach is easier in PEP 517 mode, and less likely +to trigger other issues (e.g. pytest missing its configuration, +tests relying on relative paths).

+
+

The modern preference is to remove the package directory prior to +running the test suite. In PEP 517 mode, the build system is only run +as part of src_compile, and therefore the original sources are not +needed afterwards. For example, when the tests are in a separate +directory:

+
python_test() {
+    rm -rf frozendict || die
+    epytest
+}
+
+
+

When the tests are installed as a part of the installed package, +--pyargs option can be used to find them via package lookup:

python_test() {
-    cd "${T}" || die
-    epytest --pyargs systemd
+    rm -rf numpy || die
+    epytest --pyargs numpy
 }
 
-

If the tests are in a separate directory, an absolute path can be used -instead:

+

The other possible solution is to change the working directory prior +to running the test suite, either into an arbitrary directory that +does not feature the collision, or into the install directory. +The latter could possible be necessary if the tests are installed +as part of the package, and assume paths relative to the source +directory:

python_test() {
-    cd "${T}" || die
-    epytest "${S}"/test
+    cd "${BUILD_DIR}/install$(python_get_sitedir)" || die
+    epytest
 }
 
+

However, please note that changing the working directory leads to pytest +missing its configuration (pytest.ini, setup.cfg, +pyproject.toml) which in turn could lead to warnings about missing +marks or misleading test suite problems.

Checklist for dealing with test failures

-- cgit v1.2.3-65-gdbad