Skip to content

Commit bf34ca8

Browse files
committed
docs: add guide on in-tree tests for packages with compiled code
1 parent 31bcf20 commit bf34ca8

3 files changed

Lines changed: 123 additions & 0 deletions

File tree

‎package-structure-code/python-package-structure.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ Including the **tests/** directory in your **src/package** directory ensures tha
133133

134134
Be sure to read the [pytest documentation for more about including tests in your package distribution](https://docs.pytest.org/en/7.2.x/explanation/goodpractices.html#choosing-a-test-layout-import-rules).
135135

136+
If you decide to ship tests with your package, see [shipping tests with your package](in-tree-tests) for how to lay them out and run them with `pytest --pyargs`.
137+
136138
:::{admonition} Challenges with including tests and data in a package wheel
137139
:class: tip
138140

‎tests/in-tree-tests.md‎

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
(in-tree-tests)=
2+
# Shipping tests with your package (in-tree tests)
3+
4+
Throughout this guide we recommend keeping your `tests/` directory **outside**
5+
of your package directory (an "out-of-tree" layout). This keeps your wheel
6+
small and, combined with the [src layout](src-layout-test), guarantees that
7+
your tests run against the *installed* package rather than the files in your
8+
working directory.
9+
10+
Some packages, however, benefit from shipping their tests inside the package
11+
("in-tree" tests). This is common in the scientific Python ecosystem: NumPy,
12+
SciPy, and scikit-image all do it.
13+
14+
## When in-tree tests make sense
15+
16+
Consider in-tree tests if your package:
17+
18+
* **Contains compiled code** (C, C++, Cython, Rust, or Fortran). A wheel built
19+
on one platform may import fine but produce wrong results on another.
20+
Letting users run the test suite against the wheel they installed
21+
is the most reliable way to find out.
22+
* **Is built by downstream packagers** such as conda-forge, Debian, or Spack.
23+
Packagers routinely run a package's test suite as part of their build, and
24+
they can only do so if the tests ship with the package.
25+
* Has users who need to **verify an installation** on an HPC cluster or
26+
another locked-down environment where they can't clone the repository.
27+
28+
If your package is pure Python with a small test suite, out-of-tree tests are
29+
simpler, and we recommend sticking with them.
30+
31+
## Layout
32+
33+
Move `tests/` inside your package directory and give it an `__init__.py` so
34+
that it is discovered and installed like any other subpackage:
35+
36+
```bash
37+
src/
38+
mypackage/
39+
__init__.py
40+
core.py
41+
tests/
42+
__init__.py
43+
test_core.py
44+
docs/
45+
pyproject.toml
46+
```
47+
48+
:::{admonition} Make sure the tests ship
49+
:class: tip
50+
51+
Check your build backend's package-discovery settings. If you list packages
52+
explicitly, add `mypackage.tests`. After building, inspect the wheel
53+
(`unzip -l dist/*.whl`) to confirm that the test files are included.
54+
:::
55+
56+
## Run tests against the installed package
57+
58+
With tests inside the package, run them using pytest's `--pyargs` flag. This
59+
tells pytest to import `mypackage` from the active environment, locate its
60+
`tests` subpackage, and run it, regardless of your current working directory:
61+
62+
```bash
63+
pytest --pyargs mypackage
64+
```
65+
66+
Many scientific packages also expose a convenience function so that users
67+
don't need to remember the flag:
68+
69+
```python
70+
# src/mypackage/__init__.py
71+
def test(*args):
72+
"""Run the mypackage test suite."""
73+
import pytest
74+
75+
return pytest.main(["--pyargs", "mypackage", *args])
76+
```
77+
78+
```python
79+
>>> import mypackage
80+
>>> mypackage.test()
81+
```
82+
83+
Because `pytest` is only imported inside the function, it stays an optional
84+
dependency. Add it to a `test` extra in your `pyproject.toml`:
85+
86+
```toml
87+
[project.optional-dependencies]
88+
test = ["pytest"]
89+
```
90+
91+
## Pitfalls
92+
93+
**Running tests on the source tree by accident.** This is the main reason we
94+
recommend out-of-tree tests for beginners. If you run
95+
`pytest src/mypackage/tests` from the repository root, Python may import your
96+
*un-built* source files instead of the installed package. With compiled code,
97+
this surfaces as confusing `ImportError`s about missing extension modules.
98+
Always use `pytest --pyargs mypackage` (or a `nox` or `tox` session that
99+
installs the package first) rather than pointing pytest at a path.
100+
101+
**Test data.** Don't ship large datasets inside your wheel. See
102+
[don't include test suite datasets in your package](src-layout-test)
103+
for alternatives such as Pooch.
104+
105+
**Wheel size.** Every test file is downloaded by every user. Keep the in-tree
106+
suite focused. Very slow or data-heavy tests can stay out of tree and run only
107+
in CI.
108+
109+
For a real-world example, see how
110+
[NumPy](https://github.com/numpy/numpy/tree/main/numpy) organizes
111+
`numpy/tests` and `numpy/<submodule>/tests`, and exposes `numpy.test()`.

‎tests/index.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ Set up continuous integration with GitHub Actions to run
5757
tests across Python versions and operating systems.
5858
::::
5959

60+
::::{grid-item-card} ✨ Shipping tests with your package ✨
61+
:link: in-tree-tests
62+
:link-type: doc
63+
:class-card: left-aligned
64+
65+
Learn when it makes sense to ship tests inside your package (for example,
66+
if it has compiled code) and how users can run them with `pytest --pyargs`.
67+
::::
68+
6069
::::{grid-item-card} ✨ Code coverage ✨
6170
:link: code-cov
6271
:link-type: doc
@@ -86,6 +95,7 @@ Test types <test-types>
8695
Run tests locally <run-tests>
8796
Run tests with Nox <run-tests-nox>
8897
Run tests online (using CI) <tests-ci>
98+
Shipping tests with your package <in-tree-tests>
8999
Code coverage <code-cov>
90100
```
91101

0 commit comments

Comments
 (0)