Skip to content

Commit c1d22e6

Browse files
authored
Merge pull request #10 from fastapi-mvc/improve_tests
Refactor and improve tests suite
2 parents 82234c2 + 7fdf3ea commit c1d22e6

23 files changed

+864
-676
lines changed

template/.coveragerc.jinja

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

template/Makefile.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ integration-test: install ## Run {{project_name}} integration tests
6262
.PHONY: coverage
6363
coverage: install ## Run {{project_name}} tests coverage
6464
echo "[coverage] Run {{project_name}} tests coverage."
65-
${POETRY_BINARY} run pytest --cov-config=.coveragerc --cov={{package_name}} --cov-fail-under=90 --cov-report=xml --cov-report=term-missing tests
65+
${POETRY_BINARY} run pytest --cov={{package_name}} --cov-fail-under=90 --cov-report=xml --cov-report=term-missing tests
6666

6767
.PHONY: test
6868
test: unit-test integration-test ## Run {{project_name}} tests

template/pyproject.toml.jinja

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,14 @@ myst-parser = "~0.18.1"
5656
[build-system]
5757
requires = ["poetry-core>=1.0.0"]
5858
build-backend = "poetry.core.masonry.api"
59+
60+
[tool.coverage.run]
61+
omit = [
62+
"{{package_name}}/config/gunicorn.py",
63+
"{{package_name}}/__main__.py",
64+
]
65+
66+
[tool.coverage.report]
67+
exclude_lines = [
68+
"pass",
69+
]

template/tests/integration/app/__init__.py

Whitespace-only changes.

template/tests/integration/app/controllers/__init__.py

Whitespace-only changes.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from {{package_name}}.config import settings
2+
3+
4+
class TestReadyController:
5+
6+
def test_should_return_ok(self, app_runner):
7+
# given
8+
settings.USE_REDIS = False
9+
10+
# when
11+
response = app_runner.get("/api/ready")
12+
13+
# then
14+
assert response.status_code == 200
15+
assert response.json() == {"status": "ok"}
16+
17+
def test_should_return_not_found_when_invalid_uri(self, app_runner):
18+
# given / when
19+
response = app_runner.get("/api/ready/123")
20+
21+
# then
22+
assert response.status_code == 404
23+
{%- if redis %}
24+
25+
def test_should_return_bad_gateway_when_redis_unavailable(self, app_runner):
26+
# given
27+
settings.USE_REDIS = True
28+
29+
# when
30+
response = app_runner.get("/api/ready")
31+
32+
# then
33+
assert response.status_code == 502
34+
assert response.json() == {
35+
"error": {
36+
"code": 502,
37+
"message": "Could not connect to Redis",
38+
"status": "BAD_GATEWAY",
39+
}
40+
}
41+
{% endif %}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
from fastapi.testclient import TestClient
3+
from {{package_name}}.app import get_application
4+
from {{package_name}}.config import settings
5+
6+
7+
@pytest.fixture
8+
def app_runner():
9+
# Overriding to true in order to initialize redis client on FastAPI event
10+
# startup handler. It'll be needed for unit tests.
11+
settings.USE_REDIS = True
12+
app = get_application()
13+
14+
with TestClient(app) as client:
15+
yield client

template/tests/integration/test_ready_endpoint.py.jinja

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

template/tests/unit/app/conftest.py.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ from {{package_name}}.config import settings
55

66

77
@pytest.fixture
8-
def app():
8+
def app_runner():
99
# Overriding to true in order to initialize redis client on FastAPI event
1010
# startup handler. It'll be needed for unit tests.
1111
settings.USE_REDIS = True
Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,41 @@
11
from {{package_name}}.config import settings
22

33

4-
def test_ready(app):
5-
settings.USE_REDIS = False
6-
response = app.get("/api/ready")
7-
assert response.status_code == 200
8-
assert response.json() == {"status": "ok"}
4+
class TestReadyController:
95

6+
def test_should_return_ok(self, app_runner):
7+
# given
8+
settings.USE_REDIS = False
109

11-
def test_ready_invalid(app):
12-
response = app.get("/api/ready/123")
13-
assert response.status_code == 404
10+
# when
11+
response = app_runner.get("/api/ready")
12+
13+
# then
14+
assert response.status_code == 200
15+
assert response.json() == {"status": "ok"}
16+
17+
def test_should_return_not_found_when_invalid_uri(self, app_runner):
18+
# given / when
19+
response = app_runner.get("/api/ready/123")
20+
21+
# then
22+
assert response.status_code == 404
1423
{%- if redis %}
1524

25+
def test_should_return_bad_gateway_when_redis_unavailable(self, app_runner):
26+
# given
27+
settings.USE_REDIS = True
28+
29+
# when
30+
response = app_runner.get("/api/ready")
1631

17-
def test_ready_invalid_with_redis(app):
18-
settings.USE_REDIS = True
19-
response = app.get("/api/ready")
20-
assert response.status_code == 502
21-
assert response.json() == {
22-
"error": {
23-
"code": 502,
24-
"message": "Could not connect to Redis",
25-
"status": "BAD_GATEWAY",
32+
# then
33+
assert response.status_code == 502
34+
assert response.json() == {
35+
"error": {
36+
"code": 502,
37+
"message": "Could not connect to Redis",
38+
"status": "BAD_GATEWAY",
39+
}
2640
}
27-
}
2841
{% endif %}

0 commit comments

Comments
 (0)