Skip to content

Commit 9ca1d05

Browse files
committed
ci: split unit and integration tests
Closes #1402
1 parent fcfa22e commit 9ca1d05

File tree

4 files changed

+79
-21
lines changed

4 files changed

+79
-21
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Shared setup backend
2+
runs:
3+
using: "composite"
4+
steps:
5+
- name: Set up Python
6+
uses: actions/setup-python@v4
7+
with:
8+
python-version: '3.12'
9+
10+
- name: Install Poetry
11+
run: |
12+
python -m pip install --upgrade pip
13+
pip install poetry
14+
shell: bash
15+
16+
- name: Install dependencies
17+
working-directory: ./backend
18+
run: poetry install
19+
shell: bash

.github/workflows/ci.yaml

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -104,27 +104,15 @@ jobs:
104104
run: pnpm build
105105
working-directory: ./dashboard
106106

107-
build-django:
107+
lint-and-unit-test-django:
108108
if: github.event.pull_request.draft != true
109109
runs-on: ubuntu-latest
110-
timeout-minutes: 10
110+
timeout-minutes: 3
111111
steps:
112-
- name: Checkout code
113-
uses: actions/checkout@v4
114-
115-
- name: Set up Python
116-
uses: actions/setup-python@v4
117-
with:
118-
python-version: '3.12'
119-
120-
- name: Install Poetry
121-
run: |
122-
python -m pip install --upgrade pip
123-
pip install poetry
112+
- uses: actions/checkout@v4
124113

125-
- name: Install dependencies
126-
run: poetry install
127-
working-directory: ./backend
114+
- name: Setup Backend
115+
uses: ./.github/actions/setup-backend
128116

129117
- name: Lint
130118
run: poetry run flake8
@@ -134,6 +122,20 @@ jobs:
134122
run: poetry run black --check .
135123
working-directory: ./backend
136124

125+
- name: Run unit tests
126+
run: poetry run pytest -m unit
127+
working-directory: ./backend
128+
129+
integration-test-django:
130+
if: github.event.pull_request.draft != true
131+
runs-on: ubuntu-latest
132+
timeout-minutes: 10
133+
steps:
134+
- uses: actions/checkout@v4
135+
136+
- name: Setup Backend
137+
uses: ./.github/actions/setup-backend
138+
137139
- name: Configure credentials variables
138140
run: |
139141
echo -n "${{ secrets.PG_JSON }}" | base64 --decode > application_default_credentials.json
@@ -160,11 +162,11 @@ jobs:
160162
break
161163
fi
162164
echo "Waiting for backend to be ready... $i"
163-
sleep 10
165+
sleep 5
164166
done
165167
166-
- name: Run tests
167-
run: poetry run pytest --run-all
168+
- name: Run integration tests
169+
run: poetry run pytest -m "integration" --run-all
168170
working-directory: ./backend
169171

170172
- name: Clean containers
@@ -176,7 +178,8 @@ jobs:
176178
needs:
177179
- lint-js
178180
- build-front
179-
- build-django
181+
- lint-and-unit-test-django
182+
- integration-test-django
180183
steps:
181184
- name: Configure staging host authenticity
182185
run: |

backend/conftest.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
1+
from pytest import Item
2+
3+
TEST_MARKERS = ["unit", "integration"]
4+
5+
16
def pytest_addoption(parser):
27
parser.addoption(
38
"--run-all", action="store_true", default=False, help="run all test cases"
49
)
10+
11+
12+
def pytest_collection_modifyitems(items: list[Item]):
13+
for item in items:
14+
parent_folder = item.path.parent.name.lower()
15+
16+
matched_markers = [marker for marker in TEST_MARKERS if marker in parent_folder]
17+
match len(matched_markers):
18+
case 0:
19+
raise Exception(
20+
"""Test %s must have a type.
21+
Place the test in a folder whose name contains 'unit' or 'integration'."""
22+
% item.name
23+
)
24+
case 1:
25+
item.add_marker(matched_markers[0])
26+
case _:
27+
raise Exception(
28+
"""Test %s can only be of a single type (one of %s),
29+
but found multiple markers (%s) at folder %s"""
30+
% (
31+
item.name,
32+
TEST_MARKERS,
33+
matched_markers,
34+
parent_folder,
35+
)
36+
)

backend/pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ pytest-rerunfailures = "^15.0"
3636
[tool.pytest.ini_options]
3737
DJANGO_SETTINGS_MODULE = "kernelCI.settings"
3838
addopts = "-n 4 --dist loadscope --reruns 4 -x"
39+
markers = [
40+
"unit: marks tests as unit tests (fast, no external dependencies)",
41+
"integration: marks tests as integration tests (slow, connects to the database)"
42+
]
3943

4044
[build-system]
4145
requires = ["poetry-core"]

0 commit comments

Comments
 (0)