Skip to content

Commit 970bcec

Browse files
authored
Enforce shared assertions in e2e tests (#3108)
* Enforce shared assertions in e2e tests * Use e2e fixtures for shared assertions * Ignore expected fixtures in code scanning * Share HTTP mocks in e2e tests * Cover HTTP assertion helper branch * Address review feedback on assert helpers * Cover async assertion guard branch * Refine shared e2e assertion helpers * Tighten assert helper usage guard * Bind HTTP mocks to expected URLs * Use expected fixture for collapsed external model * Tighten HTTP request assertions * Use shared HTTP mock fixture * Use shared HTTP mock in JSON URL test * Tighten assert guard coverage * Use output fixtures for e2e asserts * Type HTTP mock helpers * Avoid CodeQL protocol body noise * Address review assertion helper feedback * Restore native deferred expected output * Limit HTTP mock assertions to requested options * Document e2e assertion helper policy * Update llms docs for assertion policy * Clarify e2e snapshot update workflow
1 parent f00eaab commit 970bcec

119 files changed

Lines changed: 7578 additions & 1130 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/codeql.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ jobs:
3838
with:
3939
languages: ${{ matrix.language }}
4040
queries: +security-and-quality
41+
config: |
42+
paths-ignore:
43+
- tests/data/expected/**
4144
4245
- name: Autobuild
4346
uses: github/codeql-action/autobuild@ce64ddcb0d8d890d2df4a9d1c04ff297367dea2a # v3

docs/development-contributing.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,39 @@ $ tox run -e cli-docs
5151
## 11. Commit and Push...
5252
```
5353

54+
## 🧪 E2E test assertions
55+
56+
E2E tests that validate generated output must use the shared assertion helpers
57+
instead of direct `assert` statements. The helpers compare complete generated
58+
files, generated modules, warnings, errors, and HTTP request behavior with
59+
consistent diffs and update hints.
60+
61+
Use helpers such as `run_main_and_assert`, `run_main_url_and_assert`,
62+
`create_assert_file_content`, `assert_output`, `assert_directory_content`,
63+
`assert_generated_modules_output`, `assert_generated_file_matches_output`,
64+
`assert_httpx_get_kwargs`, and `assert_warnings_contain` where they fit. Prefer
65+
full expected-file or inline snapshot comparisons for generated output over
66+
substring checks.
67+
68+
The file-output helpers use
69+
[`inline-snapshot`](https://15r10nk.github.io/inline-snapshot/latest/) and
70+
`external_file()` internally. When an expected output file is missing, the
71+
failure message includes the command to create it, such as
72+
`tox run -e <version> -- --inline-snapshot=create`. When an existing expected
73+
file differs from the generated output, the failure includes a diff and the
74+
command to update it, such as
75+
`tox run -e <version> -- --inline-snapshot=fix`. Review the generated files and
76+
the resulting `git diff` before committing. See the inline-snapshot
77+
[`--inline-snapshot` pytest options](https://15r10nk.github.io/inline-snapshot/latest/pytest/#-inline-snapshotcreatefixtrimupdate)
78+
for the meaning of `create` and `fix`.
79+
80+
Direct `assert` statements are blocked in the guarded E2E test modules by
81+
`tests/test_assert_helper_usage.py`. If you add a new E2E test file that checks
82+
generated output, add it to `E2E_TEST_PATHS` in that guard test so new direct
83+
assertions are also rejected. Only use `@pytest.mark.allow_direct_assert` for
84+
cases that cannot reasonably be expressed with the shared helpers, such as
85+
mock setup internals or narrow intermediate-state checks.
86+
5487
## ➕ Adding a New CLI Option
5588

5689
When adding a new CLI option to `datamodel-code-generator`, follow these steps:

docs/llms-full.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30847,6 +30847,39 @@ $ tox run -e cli-docs
3084730847
## 11. Commit and Push...
3084830848
```
3084930849

30850+
## 🧪 E2E test assertions
30851+
30852+
E2E tests that validate generated output must use the shared assertion helpers
30853+
instead of direct `assert` statements. The helpers compare complete generated
30854+
files, generated modules, warnings, errors, and HTTP request behavior with
30855+
consistent diffs and update hints.
30856+
30857+
Use helpers such as `run_main_and_assert`, `run_main_url_and_assert`,
30858+
`create_assert_file_content`, `assert_output`, `assert_directory_content`,
30859+
`assert_generated_modules_output`, `assert_generated_file_matches_output`,
30860+
`assert_httpx_get_kwargs`, and `assert_warnings_contain` where they fit. Prefer
30861+
full expected-file or inline snapshot comparisons for generated output over
30862+
substring checks.
30863+
30864+
The file-output helpers use
30865+
[`inline-snapshot`](https://15r10nk.github.io/inline-snapshot/latest/) and
30866+
`external_file()` internally. When an expected output file is missing, the
30867+
failure message includes the command to create it, such as
30868+
`tox run -e <version> -- --inline-snapshot=create`. When an existing expected
30869+
file differs from the generated output, the failure includes a diff and the
30870+
command to update it, such as
30871+
`tox run -e <version> -- --inline-snapshot=fix`. Review the generated files and
30872+
the resulting `git diff` before committing. See the inline-snapshot
30873+
[`--inline-snapshot` pytest options](https://15r10nk.github.io/inline-snapshot/latest/pytest/#-inline-snapshotcreatefixtrimupdate)
30874+
for the meaning of `create` and `fix`.
30875+
30876+
Direct `assert` statements are blocked in the guarded E2E test modules by
30877+
`tests/test_assert_helper_usage.py`. If you add a new E2E test file that checks
30878+
generated output, add it to `E2E_TEST_PATHS` in that guard test so new direct
30879+
assertions are also rejected. Only use `@pytest.mark.allow_direct_assert` for
30880+
cases that cannot reasonably be expressed with the shared helpers, such as
30881+
mock setup internals or narrow intermediate-state checks.
30882+
3085030883
## ➕ Adding a New CLI Option
3085130884

3085230885
When adding a new CLI option to `datamodel-code-generator`, follow these steps:

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ verbosity_assertions = 2
232232
markers = [
233233
"perf: marks tests as performance tests (excluded from CI benchmarks)",
234234
"benchmark: marks tests as benchmark tests",
235+
"allow_direct_assert: marks e2e tests where direct assert statements are intentionally allowed",
235236
]
236237

237238
[tool.coverage]

0 commit comments

Comments
 (0)