Skip to content

Commit b75cf76

Browse files
alejsdevpre-commit-ci[bot]
authored andcommitted
✅ Simplify tests for schema_extra_example (fastapi#13197)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent df0a1a5 commit b75cf76

14 files changed

+65
-1658
lines changed

tests/test_tutorial/test_schema_extra_example/test_tutorial001.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
import importlib
2+
13
import pytest
24
from fastapi.testclient import TestClient
35

4-
from ...utils import needs_pydanticv2
6+
from ...utils import needs_py310, needs_pydanticv2
57

68

7-
@pytest.fixture(name="client")
8-
def get_client():
9-
from docs_src.schema_extra_example.tutorial001 import app
9+
@pytest.fixture(
10+
name="client",
11+
params=[
12+
"tutorial001",
13+
pytest.param("tutorial001_py310", marks=needs_py310),
14+
],
15+
)
16+
def get_client(request: pytest.FixtureRequest):
17+
mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}")
1018

11-
client = TestClient(app)
19+
client = TestClient(mod.app)
1220
return client
1321

1422

tests/test_tutorial/test_schema_extra_example/test_tutorial001_pv1.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
1+
import importlib
2+
13
import pytest
24
from fastapi.testclient import TestClient
35

4-
from ...utils import needs_pydanticv1
6+
from ...utils import needs_py310, needs_pydanticv1
57

68

7-
@pytest.fixture(name="client")
8-
def get_client():
9-
from docs_src.schema_extra_example.tutorial001_pv1 import app
9+
@pytest.fixture(
10+
name="client",
11+
params=[
12+
"tutorial001_pv1",
13+
pytest.param("tutorial001_pv1_py310", marks=needs_py310),
14+
],
15+
)
16+
def get_client(request: pytest.FixtureRequest):
17+
mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}")
1018

11-
client = TestClient(app)
19+
client = TestClient(mod.app)
1220
return client
1321

1422

tests/test_tutorial/test_schema_extra_example/test_tutorial001_pv1_py310.py

Lines changed: 0 additions & 129 deletions
This file was deleted.

tests/test_tutorial/test_schema_extra_example/test_tutorial001_py310.py

Lines changed: 0 additions & 135 deletions
This file was deleted.

tests/test_tutorial/test_schema_extra_example/test_tutorial004.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,30 @@
1+
import importlib
2+
3+
import pytest
14
from dirty_equals import IsDict
25
from fastapi.testclient import TestClient
36

4-
from docs_src.schema_extra_example.tutorial004 import app
7+
from ...utils import needs_py39, needs_py310
8+
9+
10+
@pytest.fixture(
11+
name="client",
12+
params=[
13+
"tutorial004",
14+
pytest.param("tutorial004_py310", marks=needs_py310),
15+
"tutorial004_an",
16+
pytest.param("tutorial004_an_py39", marks=needs_py39),
17+
pytest.param("tutorial004_an_py310", marks=needs_py310),
18+
],
19+
)
20+
def get_client(request: pytest.FixtureRequest):
21+
mod = importlib.import_module(f"docs_src.schema_extra_example.{request.param}")
522

6-
client = TestClient(app)
23+
client = TestClient(mod.app)
24+
return client
725

826

9-
# Test required and embedded body parameters with no bodies sent
10-
def test_post_body_example():
27+
def test_post_body_example(client: TestClient):
1128
response = client.put(
1229
"/items/5",
1330
json={
@@ -20,7 +37,7 @@ def test_post_body_example():
2037
assert response.status_code == 200
2138

2239

23-
def test_openapi_schema():
40+
def test_openapi_schema(client: TestClient):
2441
response = client.get("/openapi.json")
2542
assert response.status_code == 200, response.text
2643
assert response.json() == {

0 commit comments

Comments
 (0)