44
55> Pytest plugin for Strawberry GraphQL
66
7- This repository currently provides the package and release infrastructure for
8- ` pytest-strawberry ` . The installed package is discovered automatically by
9- pytest, but it does not expose fixtures, hooks, or command-line options yet .
7+ ` pytest-strawberry ` measures which Strawberry GraphQL schema fields are reached
8+ while your pytest suite runs. It reports schema field coverage independently of
9+ Python source coverage .
1010
1111## Installation
1212
@@ -16,6 +16,83 @@ pip install pytest-strawberry
1616
1717Pytest loads the plugin automatically through its ` pytest11 ` entry point.
1818
19+ ## Field coverage
20+
21+ Enable coverage on the pytest command line:
22+
23+ ``` shell
24+ pytest --strawberry-coverage
25+ ```
26+
27+ By default, the report covers fields with explicit Strawberry resolvers or
28+ custom field resolution supplied by integrations such as Strawberry Django. It
29+ excludes fields handled only by Strawberry's ordinary attribute lookup:
30+
31+ ``` text
32+ ============================= Strawberry coverage =============================
33+ Subscriptions: excluded (requires graphql-core 3.3+); graphql-core 3.2.11
34+
35+ Schema b6016cac
36+ ┌───────────────────┬────────┬──────┬─────────┬───────────────────────┐
37+ │ Python type │ Fields │ Miss │ Cover │ Missing fields │
38+ ├───────────────────┼────────┼──────┼─────────┼───────────────────────┤
39+ │ QueryRoot [Query] │ 2 │ 0 │ 100.00% │ │
40+ │ UserModel [User] │ 3 │ 1 │ 66.67% │ email_address [email] │
41+ ├───────────────────┼────────┼──────┼─────────┼───────────────────────┤
42+ │ All types │ 5 │ 1 │ 80.00% │ │
43+ └───────────────────┴────────┴──────┴─────────┴───────────────────────┘
44+
45+ Overall coverage: 80.00% (4/5 fields, 1 missing)
46+ ```
47+
48+ The table uses Python class and field names so uncovered resolvers are directly
49+ searchable in the codebase. When ` name= ` explicitly changes a GraphQL name, the
50+ GraphQL alias is shown in brackets. Automatic camel-casing is not repeated.
51+
52+ Use ` all ` mode to include Strawberry fields that use default attribute
53+ resolution:
54+
55+ ``` shell
56+ pytest --strawberry-coverage --strawberry-coverage-mode=all
57+ ```
58+
59+ A field counts as covered when GraphQL execution reaches its resolver or
60+ default lookup. A resolver that raises still counts. Fields skipped by a
61+ directive, omitted from the operation, or bypassed by null propagation do not.
62+ Aliases and fragments do not create additional field coordinates.
63+
64+ You can enforce a minimum combined percentage:
65+
66+ ``` shell
67+ pytest --strawberry-coverage --strawberry-coverage-fail-under=90
68+ ```
69+
70+ The threshold is compared with the displayed percentage rounded to two decimal
71+ places. Mode and threshold options require ` --strawberry-coverage ` .
72+
73+ Schema executions with different eligible fields or Python mappings receive
74+ separate fingerprinted tables. Executions with the same field set are combined.
75+ The final threshold uses their combined field and hit totals. Coverage from
76+ pytest-xdist workers is merged automatically.
77+
78+ A runnable [ Strawberry Django example] ( ./examples/strawberry_django ) shows how
79+ generated model fields participate in resolver coverage without adding a
80+ runtime dependency on Strawberry Django.
81+
82+ ### Subscriptions
83+
84+ Subscription resolver extensions are supported by graphql-core 3.3 and newer.
85+ With that capability available, subscription source and payload fields are
86+ included normally. graphql-core 3.2 does not run resolver extensions for
87+ subscriptions, so subscription-only fields are excluded and the plugin emits
88+ one warning if a subscription executes. Query and mutation coverage remains
89+ available in the same run.
90+
91+ Only schemas used to execute an operation during the pytest session are
92+ reported. An observed schema with no eligible fields is 100% covered; a session
93+ that observes no schemas is 0% covered. Reporting and threshold enforcement are
94+ disabled under ` --collect-only ` .
95+
1996## Development
2097
2198Install the project and its development dependencies with
@@ -35,6 +112,13 @@ uv run mypy
35112uv build
36113```
37114
115+ Run the Strawberry, graphql-core, and Strawberry Django compatibility checks
116+ on Python 3.14 with:
117+
118+ ``` shell
119+ uv run nox --tags compatibility
120+ ```
121+
38122## Releases
39123
40124Release changes are proposed through pull requests containing a ` RELEASE.md ` .
0 commit comments