Skip to content

Commit 7d9134a

Browse files
authored
Merge pull request #109 from OpenTestSolar/bugfix/pytest-correct-result-type-before-report
上报前校正用例结果
2 parents 5407209 + 5cb6032 commit 7d9134a

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

‎pytest/src/testsolar_pytestx/executor.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
133133
logger.info(f"Skipped {file}:{line}: {reason}")
134134
test_result.Message = reason[:1000]
135135

136+
logger.info(f"{report.nodeid} setup result type: {result_type}")
137+
136138
elif report.when == "call":
137139
test_result.Steps.append(
138140
TestCaseStep(
@@ -150,6 +152,8 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
150152

151153
test_result.ResultType = result_type
152154

155+
logger.info(f"{report.nodeid} call result type: {result_type}")
156+
153157
elif report.when == "teardown":
154158
test_result.Steps.append(
155159
TestCaseStep(
@@ -163,8 +167,24 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
163167
if not test_result.is_final():
164168
test_result.ResultType = result_type
165169

170+
logger.info(f"{report.nodeid} teardown result type: {result_type}")
171+
166172
logger.info(f"E {report.nodeid} log report")
167173

174+
def _correct_result_type(
175+
self, steps: List[TestCaseStep], original_result_type: ResultType, testcase_name: str
176+
) -> ResultType:
177+
"""校正测试结果类型"""
178+
result_type = original_result_type
179+
for step in steps:
180+
if step.ResultType == ResultType.FAILED and result_type == ResultType.SUCCEED:
181+
logger.info(
182+
f"{testcase_name} result type corrected to FAILED due to failed step: {step.Title}"
183+
)
184+
result_type = ResultType.FAILED
185+
break
186+
return result_type
187+
168188
def pytest_runtest_logfinish(self, nodeid: str, location: Any) -> None:
169189
"""
170190
Called at the end of running the runtest protocol for a single item.
@@ -174,6 +194,10 @@ def pytest_runtest_logfinish(self, nodeid: str, location: Any) -> None:
174194

175195
test_result = self.testdata[testcase_name]
176196
test_result.EndTime = datetime.utcnow()
197+
# 上报前预先校正一遍测试结果
198+
test_result.ResultType = self._correct_result_type(
199+
test_result.Steps, test_result.ResultType, testcase_name
200+
)
177201
self.testcase_count += 1
178202
logger.info(
179203
f"Testcase {nodeid} finished with result type {test_result.ResultType}, total {self.testcase_count} testcases complete"

0 commit comments

Comments
 (0)