|
| 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()`. |
0 commit comments