Skip to content

Commit bd369c5

Browse files
committed
chore(tests): increase speed by running tests in parallel
1 parent 40c509b commit bd369c5

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

.github/workflows/cicd.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
- uses: astral-sh/setup-uv@v4
2929
with:
3030
enable-cache: true
31-
- run: uv run pytest
31+
- run: uv run pytest -n auto

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"python.testing.pytestArgs": [
3-
"tests"
3+
"tests",
4+
"-n",
5+
"auto"
46
],
57
"python.testing.unittestEnabled": false,
68
"python.testing.pytestEnabled": true

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ dev = [
4545
"pre-commit>=3.5.0",
4646
"pytest-asyncio>=0.25.1",
4747
"pytest-cov>=5.0.0",
48+
"pytest-xdist>=3.6.1",
4849
"pytest>=8.3.3",
4950
"starlette-cramjam>=0.4.0",
5051
]

tests/conftest.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Pytest fixtures."""
22

33
import os
4+
import socket
45
import threading
56
from typing import Any, AsyncGenerator
67
from unittest.mock import DEFAULT, AsyncMock, MagicMock, patch
@@ -118,22 +119,35 @@ def source_api():
118119
return app
119120

120121

121-
@pytest.fixture(scope="session")
122+
@pytest.fixture()
122123
def source_api_server(source_api):
123124
"""Run the source API in a background thread."""
124-
host, port = "127.0.0.1", 9119
125-
server = uvicorn.Server(
126-
uvicorn.Config(
127-
source_api,
128-
host=host,
129-
port=port,
125+
sock = socket.socket()
126+
try:
127+
sock.bind(("", 0))
128+
port = sock.getsockname()[1]
129+
host = "127.0.0.1"
130+
server = uvicorn.Server(
131+
uvicorn.Config(
132+
source_api,
133+
host=host,
134+
port=port,
135+
)
130136
)
131-
)
132-
thread = threading.Thread(target=server.run)
133-
thread.start()
134-
yield f"http://{host}:{port}"
135-
server.should_exit = True
136-
thread.join()
137+
thread = threading.Thread(target=server.run)
138+
thread.daemon = True # Make thread daemon so it exits when main thread exits
139+
thread.start()
140+
141+
# Give the server a moment to start
142+
import time
143+
144+
time.sleep(0.1)
145+
146+
yield f"http://{host}:{port}"
147+
finally:
148+
server.should_exit = True
149+
thread.join(timeout=1.0) # Add timeout to prevent hanging
150+
sock.close()
137151

138152

139153
@pytest.fixture(autouse=True, scope="session")

uv.lock

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)