Skip to content

Commit 3a0c36c

Browse files
committed
reorganize
1 parent 87673c8 commit 3a0c36c

16 files changed

+33
-27
lines changed

.changeset/live_tests.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
default: minor
33
---
44

5-
# New categories of end-to-end tests
5+
# Functional tests
66

7-
Automated tests have been extended to include two new types of tests:
7+
Automated tests have been extended to include a new category of "functional" tests, in [`end_to_end_tests/functional_tests`](./end_to_end_tests/functional_tests). These are of two kinds:
88

9-
1. Happy-path tests that run the generator from an inline API document and then actually import and execute the generated code. See [`end_to_end_tests/generated_code_live_tests`](./end_to_end_tests/generated_code_live_tests).
9+
1. Happy-path tests that run the generator from an inline API document and then actually import and execute the generated code.
1010
2. Warning/error condition tests that run the generator from an inline API document that contains something invalid, and make assertions about the generator's output.
1111

12-
These provide more efficient and granular test coverage than the "golden record"-based end-to-end tests, and also replace some tests that were previously being done against low-level implementation details in `tests/unit`.
12+
These provide more efficient and granular test coverage than the "golden record"-based end-to-end tests. Also, the low-level unit tests in `tests`, which are dependent on internal implementation details, can now in many cases be replaced by functional tests.
1313

1414
This does not affect any runtime functionality of openapi-python-client.

CONTRIBUTING.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,20 @@ There are 4 types of snapshots generated right now, you may have to update only
6969
3. `test_custom_templates` are used with `baseline_openapi_3.0.json` to generate `custom-templates-golden-record` for testing custom templates
7070
4. `3.1_specific.openapi.yaml` is used to generate `test-3-1-golden-record` and test 3.1-specific features (things which do not have a 3.0 equivalent)
7171

72-
#### Unit tests of generated code
72+
#### Functional tests
7373

74-
These verify the runtime behavior of the generated code, without making assertions about the exact implementation of the code. For instance, they can verify that JSON data is correctly decoded into model class attributes.
74+
These are black-box tests that verify the runtime behavior of generated code, as well as the generator's validation behavior. For instance, they can verify that JSON data is correctly decoded into model class attributes, or that the generator will emit an appropriate warning for an invalid spec.
7575

76-
The tests run the generator against a small API spec (defined inline for each test class), and then import and execute the generated code. This can sometimes identify issues with validation logic, module imports, etc., that might be harder to diagnose via the snapshot tests, especially during development of a new feature.
76+
This can sometimes identify issues with error handling, validation logic, module imports, etc., that might be harder to diagnose via the snapshot tests, especially during development of a new feature.
7777

78-
See [`end_to_end_tests/generated_code_live_tests`](./end_to_end_tests/generated_code_live_tests).
78+
See [`end_to_end_tests/functional_tests`](./end_to_end_tests/functional_tests).
7979

8080
#### Other unit tests
8181

8282
These include:
8383

8484
* Regular unit tests of basic pieces of fairly self-contained low-level functionality, such as helper functions. These are implemented in the `tests/unit` directory, using the `pytest` framework.
85-
* End-to-end tests of invalid spec conditions, where we run the generator against a small spec with some problem, and expect it to print warnings/errors rather than generating code. These are implemented in `end_to_end_tests/generator_errors_and_warnings`.
86-
* Older-style unit tests of low-level functions like `property_from_data` that have complex behavior. These are brittle and difficult to maintain, and should not be used going forward. Instead, use either unit tests of generated code (to test happy paths), or end-to-end tests of invalid spec conditions (to test for warnings/errors), as described above.
85+
* Older-style unit tests of low-level functions like `property_from_data` that have complex behavior. These are brittle and difficult to maintain, and should not be used going forward. Instead, they should be migrated to functional tests.
8786

8887
### Creating a Pull Request
8988

end_to_end_tests/generated_code_live_tests/README.md renamed to end_to_end_tests/functional_tests/README.md

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1-
## The `generated_code_live_tests` module
1+
## The `functional_tests` module
22

3-
These are end-to-end tests which run the code generator command, but unlike the other tests in `end_to_end_tests`, they are also unit tests _of the behavior of the generated code_.
3+
These are end-to-end tests which run the client generator against many small API documents that are specific to various test cases.
4+
5+
Rather than testing low-level implementation details (like the unit tests in `tests`), or making assertions about the exact content of the generated code (like the "golden record"-based end-to-end tests), these treat both the generator and the generated code as black boxes and make assertions about their behavior.
6+
7+
The tests are in two submodules:
8+
9+
# `generated_code_execution`
10+
11+
These tests use valid API specs, and after running the generator, they _import and execute_ pieces of the generated code to verify that it actually works at runtime.
412

513
Each test class follows this pattern:
614

@@ -33,3 +41,16 @@ class TestSimpleJsonObject:
3341
instance = MyModel(string_prop="abc")
3442
assert instance.to_dict() == {"stringProp": "abc"}
3543
```
44+
45+
# `generator_failure_cases`
46+
47+
These run the generator with an invalid API spec and make assertions about the warning/error output. Some of these invalid conditions are expected to only produce warnings about the affected schemas, while others are expected to produce fatal errors that terminate the generator.
48+
49+
For warning conditions, each test class follows this pattern:
50+
51+
- Call `inline_spec_should_cause_warnings`, providing an inline API spec (JSON or YAML). If there are several test methods in the class using the same spec, use a fixture with scope "class" so the generator is only run once.
52+
- Use `assert_bad_schema_warning` to parse the output and check for a specific warning message for a specific schema name.
53+
54+
Or, for fatal error conditions:
55+
56+
- Call `inline_spec_should_fail`, providing an inline API spec (JSON or YAML).

end_to_end_tests/generator_errors_and_warnings/README.md

-14
This file was deleted.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ re = {composite = ["regen_e2e", "e2e --snapshot-update"]}
119119
regen_e2e = "python -m end_to_end_tests.regen_golden_record"
120120

121121
[tool.pdm.scripts.test]
122-
cmd = "pytest tests end_to_end_tests/test_end_to_end.py end_to_end_tests/generated_code_live_tests end_to_end_tests/generator_errors_and_warnings --basetemp=tests/tmp"
122+
cmd = "pytest tests end_to_end_tests/test_end_to_end.py end_to_end_tests/functional_tests --basetemp=tests/tmp"
123123
[tool.pdm.scripts.test.env]
124124
"TEST_RELATIVE" = "true"
125125

0 commit comments

Comments
 (0)