Skip to content

Commit 7931c87

Browse files
authored
Merge pull request #111 from OpenTestSolar/feat/refactor-reporter-implementation
feat(reporter): refactor reporter implementation to use FileReporter
2 parents 812ee1a + 4bebe65 commit 7931c87

15 files changed

Lines changed: 642 additions & 579 deletions

File tree

‎pytest/.gitignore‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,5 @@ cython_debug/
161161
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162162
.idea/
163163

164-
testdata/allure_results/*
164+
testdata/allure_results/*.vscode/
165+
.vscode/

‎pytest/CHANGELOG.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Changed
11+
- Update file reporting mode in run script
12+
- Refactor reporter implementation to use FileReporter instead of Reporter
13+
14+
### Fixed
15+
- Improve test case collection and execution with better file handling

‎pytest/pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "testsolar-pytestx"
3-
version = "0.1.56"
3+
version = "0.1.58"
44
description = "Pytest tool for TestSolar"
55
authors = [
66
{name = "asiazhang2002", email = "asiazhang2002@gmail.com"},

‎pytest/script/linux/run.sh‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ export PYTHONUNBUFFERED=1
1010
export TESTSOLAR_TTP_LOADINSUBPROC=1 # 隔离环境
1111

1212
/usr/local/bin/testtools_sdk version
13-
/usr/local/bin/testtools_sdk serve --tool pytest
13+
/usr/local/bin/testtools_sdk serve --tool pytest --file-report-mode

‎pytest/src/testsolar_pytestx/collector.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from testsolar_testtool_sdk.model.load import LoadResult, LoadError
1616
from testsolar_testtool_sdk.model.param import EntryParam
1717
from testsolar_testtool_sdk.model.test import TestCase
18-
from testsolar_testtool_sdk.reporter import Reporter
18+
from testsolar_testtool_sdk.reporter import BaseReporter, FileReporter
1919

2020
from .converter import selector_to_pytest, pytest_to_selector, CASE_DRIVE_SEPARATOR
2121
from .filter import filter_invalid_selector_path
@@ -25,10 +25,10 @@
2525

2626

2727
class PytestCollector:
28-
def __init__(self, pipe_io: Optional[BinaryIO] = None):
28+
def __init__(self, report_file_path: Path):
2929
self.collected: List[Item] = []
3030
self.errors: Dict[str, str] = {}
31-
self.reporter: Reporter = Reporter(pipe_io=pipe_io)
31+
self.reporter: BaseReporter = FileReporter(report_file_path)
3232

3333
def pytest_collection_modifyitems(self, items: Sequence[Union[Item, Collector]]) -> None:
3434
for item in items:
@@ -105,7 +105,7 @@ def collect_testcases(
105105

106106
testcase_list = [os.path.join(entry_param.ProjectPath, it) for it in pytest_paths if it]
107107

108-
my_plugin = PytestCollector(pipe_io)
108+
my_plugin = PytestCollector(Path(entry_param.FileReportPath))
109109
args = [
110110
f"--rootdir={entry_param.ProjectPath}",
111111
"--collect-only",
@@ -150,7 +150,7 @@ def collect_testcases(
150150
print(f"[Load] collect testcase count: {len(load_result.Tests)}")
151151
print(f"[Load] collect load error count: {len(load_result.LoadErrors)}")
152152

153-
reporter = Reporter(pipe_io=pipe_io)
153+
reporter = FileReporter(Path(entry_param.FileReportPath))
154154
reporter.report_load_result(load_result)
155155

156156

‎pytest/src/testsolar_pytestx/converter.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def pytest_to_selector(item: Item, project_path: str) -> str:
7878

7979
if hasattr(item, "path") and hasattr(item, "cls") and item.path:
8080
rel_path = os.path.relpath(item.path, project_path)
81+
# 统一使用正斜杠,确保跨平台一致性
82+
rel_path = rel_path.replace(os.sep, "/")
8183
name = item.name
8284
if item.cls:
8385
name = item.cls.__name__ + "/" + name
@@ -87,6 +89,8 @@ def pytest_to_selector(item: Item, project_path: str) -> str:
8789
full_name = normalize_testcase_name(item.nodeid)
8890
else:
8991
rel_path, _, name = item.location
92+
# 统一使用正斜杠,确保跨平台一致性
93+
rel_path = rel_path.replace(os.sep, "/")
9094
name = name.replace(".", "/")
9195
name = decode_datadrive(name)
9296
full_name = f"{rel_path}?{name}"
@@ -140,6 +144,8 @@ def normalize_testcase_name(name: str, sub_case_key: Optional[str] = None) -> st
140144
-> test_directory/test_module.py?TestExampleClass/test_example_function/[datedrive]
141145
"""
142146
assert "::" in name
147+
# 统一使用正斜杠,确保跨平台一致性
148+
name = name.replace(os.sep, "/")
143149
name = name.replace("::", "?", 1).replace( # 第一个分割符是文件,因此替换为?
144150
"::", "/"
145151
) # 后续的分割符是测试用例名称,替换为/

‎pytest/src/testsolar_pytestx/executor.py‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from pathlib import Path
23
import sys
34
from datetime import datetime, timedelta
45
from typing import BinaryIO, Optional, Dict, Any, List, Callable
@@ -18,7 +19,7 @@
1819
ResultType,
1920
TestCaseStep,
2021
)
21-
from testsolar_testtool_sdk.reporter import Reporter
22+
from testsolar_testtool_sdk.reporter import BaseReporter, FileReporter
2223
from enum import Enum
2324

2425
from .case_log import gen_logs
@@ -46,11 +47,11 @@ class RunMode(Enum):
4647
class PytestExecutor:
4748
def __init__(
4849
self,
49-
reporter: Reporter,
50+
reporter: BaseReporter,
5051
comment_fields: Optional[List[str]] = None,
5152
data_drive_key: Optional[str] = None,
5253
) -> None:
53-
self.reporter: Reporter = reporter
54+
self.reporter: BaseReporter = reporter
5455
self.testcase_count = 0
5556
self.testdata: Dict[str, TestResult] = {}
5657
self.skipped_testcase: Dict[str, str] = {}
@@ -265,7 +266,7 @@ def run_testcases(
265266

266267
append_extra_args(args)
267268

268-
reporter: Reporter = Reporter(pipe_io=pipe_io)
269+
reporter: BaseReporter = FileReporter(report_path=Path(entry.FileReportPath))
269270
exit_code = 0
270271
captured_stderr = ""
271272
if run_mode == RunMode.SINGLE:

‎pytest/src/testsolar_pytestx/parser.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def parse_case_attributes(item: Item, comment_fields: Optional[List[str]] = None
4646
for data_name in mark.args[0]:
4747
if data_name == case_data_name:
4848
attributes["coding_testcase_id"] = mark.args[0][data_name]
49-
49+
5050
attributes["tags"] = json.dumps(tags)
5151
return attributes
5252

0 commit comments

Comments
 (0)