Skip to content

Commit 9cde949

Browse files
petercrockerclaude
andcommitted
Improve integration test robustness and fix asyncio warnings
- Add pytest-dependency and pytest-order for explicit test ordering - Add wait_for_condition async helper to reduce polling boilerplate - Add workflow_state fixture to share IDs between dependent tests - Add cleanup_on_failure fixture to clean branch on test failure - Fix authentication by adding API token to execute_command - Move asyncio marker from class level to individual async tests - Improve assertion messages with detailed context - Extract timeout/polling values into named constants 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3b53473 commit 9cde949

File tree

4 files changed

+468
-127
lines changed

4 files changed

+468
-127
lines changed

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ dependencies = [
1515
"mypy>=1.15.0",
1616
"pytest>=8.3.5,<9.0.0",
1717
"pytest-asyncio>=0.26.0",
18+
"pytest-dependency>=0.6.0",
19+
"pytest-order>=1.3.0",
1820
"rich>=13.9.4",
1921
"ruff>=0.14.1",
2022
"types-pyyaml>=6.0.12.20250809",
@@ -42,7 +44,7 @@ follow_untyped_imports = true
4244
[tool.pytest.ini_options]
4345
asyncio_mode = "auto"
4446
testpaths = ["tests"]
45-
asyncio_default_fixture_loop_scope = "function"
47+
asyncio_default_fixture_loop_scope = "class"
4648
filterwarnings = [
4749
"ignore:`background_execution` is deprecated:DeprecationWarning:infrahub_sdk.branch",
4850
]

tests/integration/conftest.py

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import os
44
import subprocess
5+
from collections.abc import Generator
56
from pathlib import Path
67

78
import pytest
89
from infrahub_sdk import Config, InfrahubClient, InfrahubClientSync
10+
from infrahub_testcontainers.container import PROJECT_ENV_VARIABLES
911
from infrahub_testcontainers.helpers import TestInfrahubDocker
1012

1113
TEST_DIRECTORY = Path(__file__).parent
@@ -16,28 +18,48 @@ class TestInfrahubDockerWithClient(TestInfrahubDocker):
1618
"""Base test class with Infrahub Docker container and clients."""
1719

1820
@pytest.fixture(scope="class")
19-
def async_client_main(self, infrahub_port: int) -> InfrahubClient:
20-
"""Async Infrahub client on main branch."""
21+
def async_client_main(
22+
self, infrahub_port: int
23+
) -> Generator[InfrahubClient, None, None]:
24+
"""Async Infrahub client on main branch.
25+
26+
Yields:
27+
InfrahubClient configured for the test container.
28+
"""
2129
client = InfrahubClient(
2230
config=Config(
2331
address=f"http://localhost:{infrahub_port}",
2432
)
2533
)
26-
return client
34+
yield client
2735

2836
@pytest.fixture(scope="class")
29-
def client_main(self, infrahub_port: int) -> InfrahubClientSync:
30-
"""Sync Infrahub client on main branch."""
37+
def client_main(
38+
self, infrahub_port: int
39+
) -> Generator[InfrahubClientSync, None, None]:
40+
"""Sync Infrahub client on main branch.
41+
42+
Yields:
43+
InfrahubClientSync configured for the test container.
44+
"""
3145
client = InfrahubClientSync(
3246
config=Config(
3347
address=f"http://localhost:{infrahub_port}",
3448
)
3549
)
36-
return client
50+
yield client
3751

3852
@pytest.fixture(scope="class")
39-
def client(self, infrahub_port: int, default_branch: str) -> InfrahubClientSync:
40-
"""Sync Infrahub client on the default test branch."""
53+
def client(
54+
self, infrahub_port: int, default_branch: str
55+
) -> Generator[InfrahubClientSync, None, None]:
56+
"""Sync Infrahub client on the default test branch.
57+
58+
Creates the branch if it doesn't exist and sets it as default.
59+
60+
Yields:
61+
InfrahubClientSync configured for the test branch.
62+
"""
4163
client = InfrahubClientSync(
4264
config=Config(
4365
address=f"http://localhost:{infrahub_port}",
@@ -48,13 +70,26 @@ def client(self, infrahub_port: int, default_branch: str) -> InfrahubClientSync:
4870
if client.default_branch != default_branch:
4971
client.default_branch = default_branch
5072

51-
return client
73+
yield client
5274

5375
@staticmethod
54-
def execute_command(command: str, address: str) -> subprocess.CompletedProcess[str]:
55-
"""Execute a shell command with Infrahub environment variables."""
76+
def execute_command(
77+
command: str, address: str
78+
) -> subprocess.CompletedProcess[str]:
79+
"""Execute a shell command with Infrahub environment variables.
80+
81+
Args:
82+
command: The shell command to execute.
83+
address: The Infrahub server address.
84+
85+
Returns:
86+
CompletedProcess with stdout, stderr, and returncode.
87+
"""
5688
env = os.environ.copy()
5789
env["INFRAHUB_ADDRESS"] = address
90+
env["INFRAHUB_API_TOKEN"] = PROJECT_ENV_VARIABLES[
91+
"INFRAHUB_TESTING_INITIAL_ADMIN_TOKEN"
92+
]
5893
env["INFRAHUB_MAX_CONCURRENT_EXECUTION"] = "10"
5994

6095
result = subprocess.run(

0 commit comments

Comments
 (0)