Skip to content

Commit 26915d5

Browse files
committed
Use regex to auto-detect QA Sphere markers in test names
Replace hardcoded name mapping with a regex that extracts the project code and sequence from function names like test_bd023_foo -> BD-023:test_bd023_foo. Project code is a single configurable constant.
1 parent 3bdfe2c commit 26915d5

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

tests/conftest.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,32 @@
66

77
load_dotenv()
88

9-
# Map test function names to QA Sphere-friendly display names with BD-XXX markers.
10-
# qas-cli matches markers in the format PROJECT-SEQUENCE (e.g. BD-023).
11-
_QAS_NAMES: dict[str, str] = {
12-
"test_bd023_cart_operations": "BD-023: Cart operations",
13-
"test_bd022_order_with_cash_payment": "BD-022: Order with cash payment",
14-
"test_bd055_about_page_content": "BD-055: About page content",
15-
"test_bd026_navbar_navigation_state": "BD-026: Navbar navigation state",
16-
"test_bd038_menu_tabs_and_products": "BD-038: Menu tabs and products",
17-
"test_bd052_welcome_banner": "BD-052: Welcome banner",
18-
}
9+
# QA Sphere project code. Used to detect test case markers in function names.
10+
# e.g. test_bd023_cart_operations -> BD-023:test_bd023_cart_operations
11+
QAS_PROJECT_CODE = "BD"
12+
13+
_QAS_MARKER_RE = re.compile(rf"test_({QAS_PROJECT_CODE})(\d+)_", re.IGNORECASE)
14+
15+
16+
def _rewrite_qas_name(name: str) -> str:
17+
"""Rewrite test name to include QA Sphere marker prefix.
18+
19+
test_bd023_cart_operations[chromium] -> BD-023:test_bd023_cart_operations[chromium]
20+
"""
21+
m = _QAS_MARKER_RE.match(name)
22+
if not m:
23+
return name
24+
code = m.group(1).upper()
25+
seq = m.group(2)
26+
return f"{code}-{seq}:{name}"
1927

2028

2129
def pytest_itemcollected(item: pytest.Item) -> None:
22-
"""Rewrite test node IDs so JUnit XML contains QA Sphere markers (BD-XXX)."""
23-
# item.name looks like "test_bd023_cart_operations[chromium]"
24-
base_name = re.sub(r"\[.*\]$", "", item.name)
25-
if base_name in _QAS_NAMES:
26-
suffix = item.name[len(base_name) :] # e.g. "[chromium]"
27-
item._nodeid = item._nodeid.replace(item.name, _QAS_NAMES[base_name] + suffix)
28-
item.name = _QAS_NAMES[base_name] + suffix
30+
"""Rewrite test node IDs so JUnit XML contains QA Sphere markers."""
31+
new_name = _rewrite_qas_name(item.name)
32+
if new_name != item.name:
33+
item._nodeid = item._nodeid.replace(item.name, new_name)
34+
item.name = new_name
2935

3036

3137
@pytest.fixture(scope="session")

0 commit comments

Comments
 (0)