|
| 1 | +# Test suite conventions |
| 2 | + |
| 3 | +This document describes how tests are organised in AHC. It is the reference for adding, moving |
| 4 | +or reviewing tests. |
| 5 | + |
| 6 | +## Layout |
| 7 | + |
| 8 | +Tests live in a `tests/` package inside the package they test. The layout of `tests/` mirrors |
| 9 | +the production code, so finding the tests for a module is a path lookup rather than a search. |
| 10 | + |
| 11 | +| Production code | Tests | |
| 12 | +|--------------------------------|---------------------------------------------| |
| 13 | +| `{app}/models.py` | `{app}/tests/test_models.py` | |
| 14 | +| `{app}/services.py` | `{app}/tests/test_services.py` | |
| 15 | +| `{app}/services/exporter.py` | `{app}/tests/services/test_exporter.py` | |
| 16 | +| `{app}/utils_owner/views.py` | `{app}/tests/utils_owner/test_views.py` | |
| 17 | +| `ahc/views.py`, `ahc/settings.py` | `src/ahc/tests/test_views.py`, `test_csp.py` | |
| 18 | + |
| 19 | +Rules: |
| 20 | + |
| 21 | +- A module `x.py` maps to `test_x.py`; a package `x/` maps to a directory `tests/x/`. |
| 22 | +- A large module may map to a `tests/<module>/` directory split by area |
| 23 | + (for example `animals/tests/views/test_profile.py`, `views/test_stable.py`). |
| 24 | +- A test of a business invariant that crosses several layers stays together in |
| 25 | + `tests/test_<feature>.py` (for example `medical_notes/tests/test_deceased_write_blocking.py`). |
| 26 | + Reading the whole scenario in one place matters more than splitting it per layer. |
| 27 | +- Do not create empty placeholder test files for untested modules. |
| 28 | +- Every test directory contains an `__init__.py`. Basenames such as `test_views.py` repeat across |
| 29 | + apps, and without packages pytest fails with "import file mismatch". |
| 30 | +- Never keep a `tests.py` module next to a `tests/` package: the package shadows the module. |
| 31 | +- Test files are named `test_*.py`. Test classes are named `Test*` and do not subclass |
| 32 | + `django.test.TestCase`; write plain pytest classes and functions. |
| 33 | + |
| 34 | +## Markers |
| 35 | + |
| 36 | +Every test carries exactly one kind marker: `unit` or `integration`. A collection hook in the |
| 37 | +root `conftest.py` rejects tests with neither, with both, or with `unit` combined with database |
| 38 | +or Django client access. |
| 39 | + |
| 40 | +| Marker | Meaning | |
| 41 | +|---------------|---------| |
| 42 | +| `unit` | An isolated component. No database, no Django request/client, no external services. It may use hermetic local resources such as `tmp_path` when they are part of the component's direct contract (for example a file sweeper tested on a temporary directory). | |
| 43 | +| `integration` | Several layers or adapters working together, or real infrastructure: ORM/database, Django client/URLconf/middleware, signals with the ORM, management commands, Celery/Redis/CouchDB. | |
| 44 | +| `regression` | Orthogonal, added on top of `unit` or `integration`. Guards a specific fixed bug; the test docstring names the PR or issue. | |
| 45 | +| `slow` | Excluded from CI runs. | |
| 46 | +| `django_db` | Technical pytest-django marker granting database access. It is not a test category. | |
| 47 | + |
| 48 | +`regression` rules: |
| 49 | + |
| 50 | +- Put it on the test method that guards the fixed bug. Put it on a class only when every test |
| 51 | + in the class guards the same bug. |
| 52 | +- A test merely added in a `fix(...)` commit is not a regression test by that fact alone; it has |
| 53 | + to protect against the bug that commit fixed. |
| 54 | + |
| 55 | +## Fixtures |
| 56 | + |
| 57 | +Fixtures live at the lowest level that serves every user. |
| 58 | + |
| 59 | +| Level | What belongs there | |
| 60 | +|--------------------------------|--------------------| |
| 61 | +| Root `conftest.py` | Fixtures used by several apps: `user_profile`, `second_user_profile`, `logged_in_client`, `png_upload`, and the marker guard. | |
| 62 | +| `{app}/tests/conftest.py` | Domain fixtures used by several files of one app (for example `animal`, `snapshot_dir`). | |
| 63 | +| A subdirectory `conftest.py` | Fixtures used only by one area of an app. | |
| 64 | +| The test module or class | Fixtures used by a single file or class. | |
| 65 | + |
| 66 | +- There is no central `fixtures.py`. |
| 67 | +- Plain helper functions shared by several files of one app go to `{app}/tests/helpers.py` and |
| 68 | + are imported relatively (`from ..helpers import _query`). Do not import test code through an |
| 69 | + absolute `ahc.apps...tests` path: `src/__init__.py` makes pytest import test modules as |
| 70 | + `src.ahc.apps...`, and an absolute import would load a second copy of the module. |
| 71 | +- A class-level fixture with the same name as a conftest fixture overrides it for that class. |
| 72 | + Keep the override when the test asserts on the overridden data. |
| 73 | + |
| 74 | +## Parametrization |
| 75 | + |
| 76 | +Parametrize only when the behaviour under test is genuinely the same and only the input differs. |
| 77 | +Separate behaviours get separate tests, even when their bodies look similar. |
| 78 | + |
| 79 | +## Running tests |
| 80 | + |
| 81 | +```bash |
| 82 | +uv run pytest # whole suite |
| 83 | +uv run pytest -m unit |
| 84 | +uv run pytest -m integration |
| 85 | +uv run pytest -m regression |
| 86 | + |
| 87 | +just test |
| 88 | +just test-unit |
| 89 | +just test-integration |
| 90 | +just test-regression |
| 91 | +``` |
| 92 | + |
| 93 | +CI runs `unit` and `integration` in separate parallel jobs. Because every test is exactly one of |
| 94 | +the two, together they cover the whole suite once. The test database is SQLite |
| 95 | +(set in `settings.py` when pytest is invoked). |
0 commit comments