Simplify tests for variants
tiangolo opened this issue · comments
Privileged issue
- I'm @tiangolo or he asked me directly to create an issue here.
Issue Content
Summary
Simplify tests for variants, from multiple test files (one test file per variant) to a single test file with parameters to test each variant.
Background
Currently, we have multiple source example variants for different Python versions:
- Python 3.8
- Python 3.9
- Python 3.10
And we have versions using Annotated
and without using it.
Combining that, for each source app, we end up with different variants.
For example, for docs_src/query_params_str_validations/tutorial010.py
, for this same tutorial010
, we have these variants:
docs_src/query_params_str_validations/tutorial010_an_py39.py
- Using
Annotated
, Python 3.9.
- Using
docs_src/query_params_str_validations/tutorial010_an_py310.py
- Using
Annotated
, Python 3.10.
- Using
docs_src/query_params_str_validations/tutorial010_an.py
- Using
Annotated
, Python 3.8 (as 3.8 is the oldest, this one doesn't have a part in the name likepy38
).
- Using
docs_src/query_params_str_validations/tutorial010_py310.py
- Python 3.10, not using
Annotated
(as not usingAnnotated
is the oldest form, it just doesn't have thean
part in the file name.
- Python 3.10, not using
docs_src/query_params_str_validations/tutorial010.py
- Not using
Annotated
, Python 3.8.
- Not using
Each of these files represent the same FastAPI app, but with the improved syntax for Python 3.9, or 3.10, or using Annotated
, but in the end, the same app.
We want to keep these files like this because they have the different ways to create an app, the different supported syntaxes, including backward-compatible ones. They are shown in the docs and tested on CI.
Then, we have tests for that... currently, we just have a test file per variant file, so, we have:
tests/test_tutorial/test_query_params_str_validations/test_tutorial010_an_py39.py
tests/test_tutorial/test_query_params_str_validations/test_tutorial010_an_py310.py
tests/test_tutorial/test_query_params_str_validations/test_tutorial010_an.py
tests/test_tutorial/test_query_params_str_validations/test_tutorial010_py310.py
tests/test_tutorial/test_query_params_str_validations/test_tutorial010.py
But then, each of the files is almost exactly the same code, only with Pytest "markers" to define that something should only be run on Python 3.10, etc. but apart from that, they have the same code.
The Task
The task is to replace the multiple test files for each variant with a single file that uses Pytest parameters to import each specific app, and that uses Pytest markers for the files that require a specific version of Python.
An example of the result for one of these test variants is here: #13149
Not all tutorial tests have multiple variants, but there are a few that do. This can be done in one PR per tutorial (with the single test for all its variants).
Instructions
These are not strict but they worked for me to simplify the process.
- Take one of the tests that requires a Python version, say Python 3.10, e.g.
docs_src/query_params_str_validations/tutorial010_an_py310.py
, copy it to a new file with a different name (only temporarily), e.g. with an extrax
at the end:docs_src/query_params_str_validations/tutorial010x.py
- Copy the changes visible from the file in https://github.com/fastapi/fastapi/pull/13149/files, mainly:
- The
params=
part - The
request: pytest.FixtureRequest
param - The mod = importlib.import_module(` part
- The client =
TestClient(mod.app)
with the newmod.app
- The
For that tutorial, e.g. tutorial010, there are a few variants, in this case, 5. There should be one param for each of those 5 files.
The ones with a name with a variant part for Python 3.10 (py310
) should have marks=needs_py310
, and the ones for Python 3.9 (py39
) should have marks=needs_py39
.
Once that is done and the tests in that file are passing, remove the other files, and rename that test to remove the extra x
at the end.
@alejsdev: I think I will be able to help. Please let me know which tests I can do.
tests.test_tutorial.tests_query_params
: #13215
test_query_params_str_validations
: #13218
test_testing
: #13220: I'm not sure about that one, and it might be wrong.
test_testing_dependencies
: #13223 I'm not sure about that one, and it might be wrong.
I can help , please let me know the tests to be worked on.