129 lines
4.0 KiB
Markdown
129 lines
4.0 KiB
Markdown
|
|
# 测试结果显示逻辑修复
|
|||
|
|
|
|||
|
|
## 问题描述
|
|||
|
|
|
|||
|
|
在运行中出现矛盾现象:
|
|||
|
|
- **表面状态**:测试项目列表中所有项都显示为"通过"
|
|||
|
|
- **实际状态**:连接测试口失败,测试应该停止
|
|||
|
|
|
|||
|
|
## 根本原因
|
|||
|
|
|
|||
|
|
### 问题1:前向状态更新错误
|
|||
|
|
在 `on_progress_update` 中:
|
|||
|
|
```python
|
|||
|
|
if current > 1:
|
|||
|
|
prev_row = current - 2
|
|||
|
|
if prev_row < self.result_table.rowCount():
|
|||
|
|
self.result_table.update_status(prev_row, 'passed', '--') # ❌ 假设为passed
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
这段代码直接假设前一个步骤通过了,而不是检查实际的测试结果。当某个步骤失败时,这会导致该步骤被标记为passed。
|
|||
|
|
|
|||
|
|
### 问题2:测试完成后的状态覆盖
|
|||
|
|
在 `on_test_finished` 中:
|
|||
|
|
```python
|
|||
|
|
if summary['failed'] == 0:
|
|||
|
|
# ...
|
|||
|
|
for row in range(self.result_table.rowCount()):
|
|||
|
|
item = self.result_table.item(row, 2)
|
|||
|
|
if item and item.data(Qt.UserRole) in ['pending', 'running']:
|
|||
|
|
self.result_table.update_status(row, 'passed', '--') # ❌ 将未测试项标记为passed
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
当 `summary['failed'] == 0` 时(可能因为早期步骤失败且results为空),代码会将所有未完成的项标记为通过。
|
|||
|
|
|
|||
|
|
## 解决方案
|
|||
|
|
|
|||
|
|
### 修复1:基于实际结果更新前向步骤
|
|||
|
|
|
|||
|
|
**修改前**:
|
|||
|
|
```python
|
|||
|
|
if current > 1:
|
|||
|
|
prev_row = current - 2
|
|||
|
|
if prev_row < self.result_table.rowCount():
|
|||
|
|
self.result_table.update_status(prev_row, 'passed', '--')
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**修改后**:
|
|||
|
|
```python
|
|||
|
|
if current > 1:
|
|||
|
|
prev_row = current - 2
|
|||
|
|
if prev_row < len(self.test_engine.results) and prev_row < self.result_table.rowCount():
|
|||
|
|
result = self.test_engine.results[prev_row]
|
|||
|
|
status = result.status.value if hasattr(result.status, 'value') else str(result.status)
|
|||
|
|
self.result_table.update_status(prev_row, status, f"{result.execution_time:.1f}s")
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**改进**:
|
|||
|
|
- 检查 `self.test_engine.results` 中是否有对应的结果
|
|||
|
|
- 获取实际的测试结果状态(PASSED、FAILED等)
|
|||
|
|
- 显示实际的执行时间
|
|||
|
|
|
|||
|
|
### 修复2:测试完成时基于实际结果更新所有项
|
|||
|
|
|
|||
|
|
**修改前**:
|
|||
|
|
```python
|
|||
|
|
if summary['failed'] == 0:
|
|||
|
|
# ... 将pending/running标记为passed
|
|||
|
|
else:
|
|||
|
|
# ... 只更新failed的项
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**修改后**:
|
|||
|
|
```python
|
|||
|
|
# 根据实际的测试结果更新表格,而不是假设所有未完成的项都通过了
|
|||
|
|
for i, result in enumerate(self.test_engine.results):
|
|||
|
|
if i < self.result_table.rowCount():
|
|||
|
|
status = result.status.value if hasattr(result.status, 'value') else str(result.status)
|
|||
|
|
self.result_table.update_status(i, status, f"{result.execution_time:.1f}s")
|
|||
|
|
|
|||
|
|
# 标记未测试的项为跳过
|
|||
|
|
for row in range(len(self.test_engine.results), self.result_table.rowCount()):
|
|||
|
|
self.result_table.update_status(row, 'skipped', '--')
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**改进**:
|
|||
|
|
- 遍历所有实际的测试结果,使用其真实状态
|
|||
|
|
- 对于未执行的项,明确标记为'skipped'
|
|||
|
|
- 不再有假设或条件判断,完全基于事实
|
|||
|
|
|
|||
|
|
## 修改前后对比
|
|||
|
|
|
|||
|
|
### 场景:串口连接失败
|
|||
|
|
|
|||
|
|
**修改前**:
|
|||
|
|
1. 串口连接步骤失败
|
|||
|
|
2. 测试停止
|
|||
|
|
3. `on_progress_update` 被调用最后一次
|
|||
|
|
4. 由于 `summary['failed'] == 0` 的错误逻辑
|
|||
|
|
5. **所有项都被标记为'passed'** ❌
|
|||
|
|
|
|||
|
|
**修改后**:
|
|||
|
|
1. 串口连接步骤失败
|
|||
|
|
2. 测试停止
|
|||
|
|
3. `on_test_finished` 更新表格
|
|||
|
|
4. 遍历 `results` 获取实际状态
|
|||
|
|
5. 串口连接显示'failed',其他项显示'skipped' ✅
|
|||
|
|
|
|||
|
|
## 验证清单
|
|||
|
|
|
|||
|
|
✅ 编译检查:无语法错误
|
|||
|
|
✅ 逻辑检查:基于实际结果更新状态
|
|||
|
|
✅ 完整性检查:所有项都有明确的状态
|
|||
|
|
|
|||
|
|
## 预期改进
|
|||
|
|
|
|||
|
|
1. **准确的状态反映** - UI显示与实际测试结果一致
|
|||
|
|
2. **清晰的失败信息** - 失败项明确标记为'failed'
|
|||
|
|
3. **清晰的跳过信息** - 未执行的项标记为'skipped'
|
|||
|
|
4. **无误导信息** - 不会虚假显示全部通过
|
|||
|
|
|
|||
|
|
## 测试建议
|
|||
|
|
|
|||
|
|
1. 运行程序
|
|||
|
|
2. 开始测试但串口未连接
|
|||
|
|
3. 观察表格结果:
|
|||
|
|
- 第一项(串口连接)应显示'failed'
|
|||
|
|
- 其他项应显示'skipped'
|
|||
|
|
- **不应该**有任何项显示为'passed'
|