moz-testrail-client 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: moz-testrail-client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Small shared TestRail API client (httpx-based)
|
|
5
|
+
Requires-Python: >=3.14
|
|
6
|
+
Requires-Dist: httpx>=0.26.0
|
|
7
|
+
Requires-Dist: pydantic-settings>=2.1.0
|
|
8
|
+
Provides-Extra: dev
|
|
9
|
+
Requires-Dist: pytest-asyncio>=0.23.0; extra == 'dev'
|
|
10
|
+
Requires-Dist: pytest>=8.0.0; extra == 'dev'
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
testrail_client/__init__.py,sha256=2NbgKwN_4eQlbzvZ04wvEzbKhqP_SdRJbmjcQ-1R57A,472
|
|
2
|
+
testrail_client/client.py,sha256=enxnt4okbGEkZJYZjgmBVK-YNhO8O7S5xrVbLFfJ0XE,4513
|
|
3
|
+
testrail_client/config.py,sha256=7y73ugjPVTLCF3244xkmHp-_mm6Uqg4FmfDXil2wVSo,651
|
|
4
|
+
testrail_client/models.py,sha256=7kAbrvm3fyCZfT1I004LFXBDIplypkf7iHRw38LyAK8,1436
|
|
5
|
+
moz_testrail_client-0.1.0.dist-info/METADATA,sha256=l1xN2JM9oHGydwawhAunjN5rB_pp22iKTWKNlY6gGms,331
|
|
6
|
+
moz_testrail_client-0.1.0.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
|
|
7
|
+
moz_testrail_client-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from testrail_client.client import TestRailClient
|
|
2
|
+
from testrail_client.config import TestRailSettings
|
|
3
|
+
from testrail_client.models import (
|
|
4
|
+
Case,
|
|
5
|
+
CaseResult,
|
|
6
|
+
CaseStep,
|
|
7
|
+
CaseType,
|
|
8
|
+
Result,
|
|
9
|
+
Run,
|
|
10
|
+
Section,
|
|
11
|
+
Status,
|
|
12
|
+
Suite,
|
|
13
|
+
Template,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"Case",
|
|
18
|
+
"CaseResult",
|
|
19
|
+
"CaseStep",
|
|
20
|
+
"CaseType",
|
|
21
|
+
"Result",
|
|
22
|
+
"Run",
|
|
23
|
+
"Section",
|
|
24
|
+
"Status",
|
|
25
|
+
"Suite",
|
|
26
|
+
"Template",
|
|
27
|
+
"TestRailClient",
|
|
28
|
+
"TestRailSettings",
|
|
29
|
+
]
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
"""Small shared TestRail API client."""
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from testrail_client.config import TestRailSettings
|
|
8
|
+
from testrail_client.models import (
|
|
9
|
+
Case,
|
|
10
|
+
CaseResult,
|
|
11
|
+
CaseStep,
|
|
12
|
+
CaseType,
|
|
13
|
+
Result,
|
|
14
|
+
Run,
|
|
15
|
+
Section,
|
|
16
|
+
Status,
|
|
17
|
+
Suite,
|
|
18
|
+
Template,
|
|
19
|
+
_CaseTypesResponse,
|
|
20
|
+
_ResultsResponse,
|
|
21
|
+
_StatusesResponse,
|
|
22
|
+
_TemplatesResponse,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class TestRailClient:
|
|
27
|
+
def __init__(self, settings: TestRailSettings | None = None) -> None:
|
|
28
|
+
self.settings = settings or TestRailSettings.from_env()
|
|
29
|
+
|
|
30
|
+
@property
|
|
31
|
+
def base_url(self) -> str:
|
|
32
|
+
return self.settings.url.rstrip("/")
|
|
33
|
+
|
|
34
|
+
def suite_url(self, suite_id: int) -> str:
|
|
35
|
+
return f"{self.base_url}/index.php?/suites/view/{suite_id}"
|
|
36
|
+
|
|
37
|
+
def _api_url(self, endpoint: str) -> str:
|
|
38
|
+
return f"{self.base_url}/index.php?/api/v2/{endpoint.lstrip('/')}"
|
|
39
|
+
|
|
40
|
+
async def request(
|
|
41
|
+
self, method: str, endpoint: str, data: dict[str, Any] | None = None
|
|
42
|
+
) -> dict[str, Any] | list[Any]:
|
|
43
|
+
async with httpx.AsyncClient(timeout=self.settings.timeout_seconds) as client:
|
|
44
|
+
response = await client.request(
|
|
45
|
+
method,
|
|
46
|
+
self._api_url(endpoint),
|
|
47
|
+
auth=(self.settings.username, self.settings.api_key),
|
|
48
|
+
json=data,
|
|
49
|
+
headers={"Content-Type": "application/json"},
|
|
50
|
+
)
|
|
51
|
+
response.raise_for_status()
|
|
52
|
+
if not response.content:
|
|
53
|
+
return {}
|
|
54
|
+
result = response.json()
|
|
55
|
+
if not isinstance(result, (dict, list)):
|
|
56
|
+
raise RuntimeError("TestRail returned an unexpected response")
|
|
57
|
+
return result
|
|
58
|
+
|
|
59
|
+
async def get_case_types(self) -> list[CaseType]:
|
|
60
|
+
response = await self.request("GET", "get_case_types")
|
|
61
|
+
return _CaseTypesResponse.model_validate(response).root
|
|
62
|
+
|
|
63
|
+
async def get_templates(self) -> list[Template]:
|
|
64
|
+
response = await self.request(
|
|
65
|
+
"GET", f"get_templates/{self.settings.project_id}"
|
|
66
|
+
)
|
|
67
|
+
return _TemplatesResponse.model_validate(response).root
|
|
68
|
+
|
|
69
|
+
async def get_statuses(self) -> list[Status]:
|
|
70
|
+
response = await self.request("GET", "get_statuses")
|
|
71
|
+
return _StatusesResponse.model_validate(response).root
|
|
72
|
+
|
|
73
|
+
async def add_suite(self, name: str) -> Suite:
|
|
74
|
+
response = await self.request(
|
|
75
|
+
"POST",
|
|
76
|
+
f"add_suite/{self.settings.project_id}",
|
|
77
|
+
{"name": name},
|
|
78
|
+
)
|
|
79
|
+
return Suite.model_validate(response)
|
|
80
|
+
|
|
81
|
+
async def add_section(self, suite_id: int, name: str) -> Section:
|
|
82
|
+
response = await self.request(
|
|
83
|
+
"POST",
|
|
84
|
+
f"add_section/{self.settings.project_id}",
|
|
85
|
+
{"suite_id": suite_id, "name": name},
|
|
86
|
+
)
|
|
87
|
+
return Section.model_validate(response)
|
|
88
|
+
|
|
89
|
+
async def add_case(
|
|
90
|
+
self,
|
|
91
|
+
section_id: int,
|
|
92
|
+
*,
|
|
93
|
+
title: str,
|
|
94
|
+
type_id: int,
|
|
95
|
+
template_id: int,
|
|
96
|
+
steps: list[CaseStep],
|
|
97
|
+
preconditions: str | None = None,
|
|
98
|
+
labels: list[str] | None = None,
|
|
99
|
+
) -> Case:
|
|
100
|
+
payload: dict[str, Any] = {
|
|
101
|
+
"title": title,
|
|
102
|
+
"type_id": type_id,
|
|
103
|
+
"template_id": template_id,
|
|
104
|
+
"custom_steps_separated": [step.model_dump() for step in steps],
|
|
105
|
+
}
|
|
106
|
+
if preconditions:
|
|
107
|
+
payload["custom_preconds"] = preconditions
|
|
108
|
+
if labels:
|
|
109
|
+
payload["labels"] = labels
|
|
110
|
+
response = await self.request("POST", f"add_case/{section_id}", payload)
|
|
111
|
+
return Case.model_validate(response)
|
|
112
|
+
|
|
113
|
+
async def add_run(
|
|
114
|
+
self,
|
|
115
|
+
*,
|
|
116
|
+
suite_id: int,
|
|
117
|
+
case_ids: list[int],
|
|
118
|
+
name: str,
|
|
119
|
+
description: str | None = None,
|
|
120
|
+
) -> Run:
|
|
121
|
+
payload: dict[str, Any] = {
|
|
122
|
+
"name": name,
|
|
123
|
+
"suite_id": suite_id,
|
|
124
|
+
"include_all": False,
|
|
125
|
+
"case_ids": case_ids,
|
|
126
|
+
}
|
|
127
|
+
if description:
|
|
128
|
+
payload["description"] = description
|
|
129
|
+
response = await self.request(
|
|
130
|
+
"POST",
|
|
131
|
+
f"add_run/{self.settings.project_id}",
|
|
132
|
+
payload,
|
|
133
|
+
)
|
|
134
|
+
return Run.model_validate(response)
|
|
135
|
+
|
|
136
|
+
async def add_results_for_cases(
|
|
137
|
+
self, run_id: int, results: list[CaseResult]
|
|
138
|
+
) -> list[Result]:
|
|
139
|
+
response = await self.request(
|
|
140
|
+
"POST",
|
|
141
|
+
f"add_results_for_cases/{run_id}",
|
|
142
|
+
{"results": [result.model_dump() for result in results]},
|
|
143
|
+
)
|
|
144
|
+
return _ResultsResponse.model_validate(response).root
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Configuration for :class:`TestRailClient`."""
|
|
2
|
+
|
|
3
|
+
from typing import Annotated
|
|
4
|
+
|
|
5
|
+
from pydantic import BaseModel, Field
|
|
6
|
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TestRailSettings(BaseModel):
|
|
10
|
+
username: Annotated[str, Field(min_length=1)]
|
|
11
|
+
api_key: Annotated[str, Field(min_length=1)]
|
|
12
|
+
project_id: int
|
|
13
|
+
url: str = "https://mozilla.testrail.io"
|
|
14
|
+
timeout_seconds: int = 30
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def from_env(cls) -> TestRailSettings:
|
|
18
|
+
return _TestRailEnvSettings()
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class _TestRailEnvSettings(TestRailSettings, BaseSettings):
|
|
22
|
+
model_config = SettingsConfigDict(env_prefix="TESTRAIL_", extra="ignore")
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"""Typed TestRail domain models used by the client."""
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel, ConfigDict, RootModel
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class _Model(BaseModel):
|
|
7
|
+
model_config = ConfigDict(extra="ignore")
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class CaseType(_Model):
|
|
11
|
+
"""A test case type configured in TestRail."""
|
|
12
|
+
|
|
13
|
+
id: int
|
|
14
|
+
name: str
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Template(_Model):
|
|
18
|
+
"""A test case template configured in TestRail."""
|
|
19
|
+
|
|
20
|
+
id: int
|
|
21
|
+
name: str
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class Status(_Model):
|
|
25
|
+
"""A test result status configured in TestRail."""
|
|
26
|
+
|
|
27
|
+
id: int
|
|
28
|
+
name: str
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class Suite(_Model):
|
|
32
|
+
"""A test suite created in TestRail."""
|
|
33
|
+
|
|
34
|
+
id: int
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class Section(_Model):
|
|
38
|
+
"""A section created inside a TestRail test suite."""
|
|
39
|
+
|
|
40
|
+
id: int
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Case(_Model):
|
|
44
|
+
"""A test case created in TestRail."""
|
|
45
|
+
|
|
46
|
+
id: int
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class Run(_Model):
|
|
50
|
+
"""A test run created in TestRail."""
|
|
51
|
+
|
|
52
|
+
id: int
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class Result(_Model):
|
|
56
|
+
"""A test result recorded in TestRail."""
|
|
57
|
+
|
|
58
|
+
id: int
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class CaseStep(_Model):
|
|
62
|
+
"""One step of a test case, with its expected outcome."""
|
|
63
|
+
|
|
64
|
+
content: str
|
|
65
|
+
expected: str = ""
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
class CaseResult(_Model):
|
|
69
|
+
"""A result to record for one test case in a run."""
|
|
70
|
+
|
|
71
|
+
case_id: int
|
|
72
|
+
status_id: int
|
|
73
|
+
comment: str = ""
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class _CaseTypesResponse(RootModel[list[CaseType]]):
|
|
77
|
+
pass
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class _TemplatesResponse(RootModel[list[Template]]):
|
|
81
|
+
pass
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
class _StatusesResponse(RootModel[list[Status]]):
|
|
85
|
+
pass
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class _ResultsResponse(RootModel[list[Result]]):
|
|
89
|
+
pass
|