Debian Codegraph @

docs/develop/testing.md

raw
1 ---
2 title: Testing
3 description: How to run the test harness, generate coverage reports, and other considerations while writing unit tests.
4 ---
5
6 # Testing
7
8 The full contributor workflow, branch policy, PR expectations, and AI/LLM policy are documented in the root [`CONTRIBUTING.md`](https://github.com/django-guardian/django-guardian/blob/main/CONTRIBUTING.md).
9
10 This page focuses specifically on running the test and verification tooling used by this repository.
11
12 `django-guardian` extends Django's authorization framework, so regressions can have security implications. Changes in permission logic should be treated as security-sensitive and verified thoroughly.
13
14 !!! danger "Security Risks"
15 If you spot a *security risk* or a bug that might affect security of systems that use `django-guardian`,
16
17 **DO NOT create a public issue**.
18
19 Instead, follow the process in the project's [security policy](https://github.com/django-guardian/django-guardian/blob/main/SECURITY.md)
20 and use the repository's [private vulnerability reporting flow](https://github.com/django-guardian/django-guardian/security).
21
22 If you find a non-security related bug in this project,
23 please take a minute and file a ticket in our
24 [issue tracker](https://github.com/django-guardian/django-guardian/issues).
25
26 ## Local setup
27
28 Install [`uv`](https://docs.astral.sh/uv/getting-started/installation/) and sync the development environment:
29
30 ```shell
31 uv sync --group dev
32 ```
33
34 If you need the optional database dependencies used by the broader CI matrix, sync the `ci` dependency group instead:
35
36 ```shell
37 uv sync --group ci
38 ```
39
40 ## Running tests
41
42 The fastest local test command is the main `pytest` suite with coverage:
43
44 ```shell
45 uv run pytest --cov=guardian --cov-report=xml --cov-report=term
46 ```
47
48 Makefile alias:
49
50 ```shell
51 make test
52 ```
53
54 If you want full matrix parity with the environments defined in `tox.ini`, run:
55
56 ```shell
57 uv run tox run
58 ```
59
60 To run a specific environment only:
61
62 ```shell
63 uv run tox run -e core-py313-django51
64 uv run tox run -e type-py313
65 uv run tox run -e docs-py313-django51
66 ```
67
68 Makefile alias for the full matrix:
69
70 ```shell
71 make test-all
72 ```
73
74 ## Coverage support
75
76 [Coverage](http://nedbatchelder.com/code/coverage/) is a tool for
77 measuring code coverage of Python programs. It is great for tests and we
78 use it as a backup - we try to cover 100% of the code used by
79 `django-guardian`. This of course does *NOT* mean that if all of the
80 codebase is covered by tests we can be sure there is no bug (as
81 specification of almost all applications requires some unique scenarios
82 to be tested). On the other hand it definitely helps to track missing
83 parts.
84
85 The repository's standard coverage command is already included in the default pytest invocation:
86
87 ```shell
88 uv run pytest --cov=guardian --cov-report=xml --cov-report=term
89 ```
90
91 This produces:
92
93 - terminal coverage output for quick review
94 - `coverage.xml` for CI/reporting integrations
95
96 ## Tox
97
98 `tox` is the canonical way to exercise the supported Python/Django combinations,
99 type-checking environment, docs build environment, and example project checks.
100
101 ```shell
102 uv run tox run
103 ```
104
105 Useful examples:
106
107 ```shell
108 uv run tox run -e core-py314-django52
109 uv run tox run -e core-py312-djangomain
110 uv run tox run -e example-py313-django51
111 uv run tox run -e examplegroup-py313-django51
112 ```
113
114 If you change models or permission schema state, also run the migration sanity check used by the Django tox environments:
115
116 ```shell
117 uv run python manage.py makemigrations --check --dry-run
118 ```
119
120 ## Linting, typing, and docs verification
121
122 Repository changes should usually be verified with more than just tests.
123
124 ```shell
125 uv run ruff check .
126 uv run mypy ./guardian
127 uv run mkdocs build
128 ```
129
130 Makefile aliases:
131
132 ```shell
133 make lint
134 make docs
135 ```
136
137 ## GitHub Actions
138
139 !!! abstract "Added in version 2.4.0"
140
141 We have support for [GitHub Actions](https://github.com/django-guardian/django-guardian/actions)
142 to run the same general checks in CI. Local verification with `pytest`, `tox`, `ruff`, `mypy`, and `mkdocs` helps catch problems before you open or update a pull request.