Skip to content

Commit da706e7

Browse files
authored
test: use pytest fixture and simplify test code (laike9m#50)
1 parent 153acc6 commit da706e7

File tree

6 files changed

+39
-33
lines changed

6 files changed

+39
-33
lines changed

tests/conftest.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
In `conftest.py` we define fixtures that are used in multiple tests. pytest will automatically
3+
inject them into the test functions that need them by signature matching.
4+
"""
5+
6+
7+
from pathlib import Path
8+
9+
import pytest
10+
from flask.testing import FlaskClient
11+
12+
from app import app
13+
14+
CHALLENGES_DIR = Path(__file__).parent.parent / "challenges"
15+
ALL_QUESTIONS = list(CHALLENGES_DIR.glob("**/question.py"))
16+
ALL_SOLUTIONS = list(CHALLENGES_DIR.glob("**/solution.py"))
17+
18+
19+
@pytest.fixture
20+
def test_client() -> FlaskClient:
21+
return app.test_client()
22+
23+
24+
@pytest.fixture(params=ALL_QUESTIONS, ids=lambda x: x.parent.name)
25+
def question_file(request):
26+
return request.param
27+
28+
29+
@pytest.fixture(params=ALL_SOLUTIONS, ids=lambda x: x.parent.name)
30+
def solution_file(request):
31+
return request.param

tests/fixture.py

-10
This file was deleted.

tests/test_api.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
from pathlib import Path
22

3-
import pytest
4-
from werkzeug.test import TestResponse
5-
from app import app
6-
from .fixture import ALL_QUESTIONS
3+
from flask.testing import FlaskClient
74

85

9-
@pytest.mark.parametrize("question_file", ALL_QUESTIONS, ids=lambda x: x.parent.name)
10-
def test_get_challenge(question_file: Path):
6+
def test_get_challenge(test_client: FlaskClient, question_file: Path):
117
level, challenge_name = question_file.parent.name.split("-", maxsplit=1)
12-
response = app.test_client().get(f"/{level}/{challenge_name}")
8+
response = test_client.get(f"/{level}/{challenge_name}")
139

1410
# Verify the returned HTML contains a challenge's source code
1511
assert "TODO" in response.text
1612

1713

18-
@pytest.mark.parametrize("question_file", ALL_QUESTIONS, ids=lambda x: x.parent.name)
19-
def test_run_challenge(question_file: Path):
14+
def test_run_challenge(test_client: FlaskClient, question_file: Path):
2015
level, challenge_name = question_file.parent.name.split("-", maxsplit=1)
21-
response = app.test_client().post(f"run/{level}/{challenge_name}", data="")
16+
response = test_client.post(f"run/{level}/{challenge_name}", data="")
2217

2318
# Verify backend can run type check and return results
2419
assert response.json is not None
2520

2621

27-
def test_invalid_challenge_redirect_to_homepage():
28-
response = app.test_client().get("/foo/bar")
22+
def test_invalid_challenge_redirect_to_homepage(test_client: FlaskClient):
23+
response = test_client.get("/foo/bar")
2924
assert response.status_code == 302
3025
assert response.location == "/"

tests/test_identical.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44

55
from pathlib import Path
66

7-
import pytest
8-
97
from views.challenge import Challenge, Level
10-
from .fixture import ALL_SOLUTIONS, QUESTION
118

129

13-
@pytest.mark.parametrize("solution_file", ALL_SOLUTIONS, ids=lambda x: x.parent.name)
1410
def test_identical(solution_file: Path):
1511
level, challenge_name = solution_file.parent.name.split("-", maxsplit=1)
1612
with solution_file.open() as f:
@@ -19,7 +15,7 @@ def test_identical(solution_file: Path):
1915
name=challenge_name, level=Level(level), code=solution_code
2016
).test_code
2117

22-
question_file = solution_file.parent / QUESTION
18+
question_file = solution_file.parent / "question.py"
2319
with question_file.open() as f:
2420
question_code = f.read()
2521
question_test = Challenge(

tests/test_questions.py

-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
import pytest
88

99
from views.challenge import ChallengeManager
10-
from .fixture import ALL_QUESTIONS
1110

1211

13-
@pytest.mark.parametrize("question_file", ALL_QUESTIONS, ids=lambda x: x.parent.name)
1412
def test_solution_valid(question_file: Path):
1513
# Skip the challenges whose question can't fail type check no matter how many tests are added.
1614
if question_file.parent.name == "basic-any":

tests/test_solutions.py

-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,9 @@
44

55
from pathlib import Path
66

7-
import pytest
8-
97
from views.challenge import ChallengeManager
10-
from .fixture import ALL_SOLUTIONS
118

129

13-
@pytest.mark.parametrize("solution_file", ALL_SOLUTIONS, ids=lambda x: x.parent.name)
1410
def test_solution_valid(solution_file: Path):
1511
with solution_file.open() as f:
1612
code = f.read()

0 commit comments

Comments
 (0)