Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify tests for variants #13150

Open
1 task done
tiangolo opened this issue Jan 3, 2025 · 0 comments
Open
1 task done

Simplify tests for variants #13150

tiangolo opened this issue Jan 3, 2025 · 0 comments
Assignees

Comments

@tiangolo
Copy link
Member

tiangolo commented Jan 3, 2025

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.
  • docs_src/query_params_str_validations/tutorial010_an_py310.py
    • Using Annotated, Python 3.10.
  • 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 like py38).
  • docs_src/query_params_str_validations/tutorial010_py310.py
    • Python 3.10, not using Annotated (as not using Annotated is the oldest form, it just doesn't have the an part in the file name.
  • docs_src/query_params_str_validations/tutorial010.py
    • Not using Annotated, Python 3.8.

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 extra x 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 new mod.app

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants