Skip to content

Commit 21cf1c2

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

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

.github/workflows/cicd.yaml

+1-1
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

+3-1
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

+1
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

+15-4
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
@@ -119,19 +120,29 @@ def source_api():
119120

120121

121122
@pytest.fixture(scope="session")
122-
def source_api_server(source_api):
123+
def free_port():
124+
"""Get a free port."""
125+
sock = socket.socket()
126+
# Needed for Github Actions, https://stackoverflow.com/a/4466035
127+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
128+
sock.bind(("", 0))
129+
return sock.getsockname()[1]
130+
131+
132+
@pytest.fixture(scope="session")
133+
def source_api_server(source_api, free_port):
123134
"""Run the source API in a background thread."""
124-
host, port = "127.0.0.1", 9119
135+
host = "127.0.0.1"
125136
server = uvicorn.Server(
126137
uvicorn.Config(
127138
source_api,
128139
host=host,
129-
port=port,
140+
port=free_port,
130141
)
131142
)
132143
thread = threading.Thread(target=server.run)
133144
thread.start()
134-
yield f"http://{host}:{port}"
145+
yield f"http://{host}:{free_port}"
135146
server.should_exit = True
136147
thread.join()
137148

uv.lock

+24
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)