-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_task6.py
More file actions
105 lines (88 loc) · 2.98 KB
/
test_task6.py
File metadata and controls
105 lines (88 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Task 6: Final Integration Test
验证所有错误处理符合规范
"""
def test_all_error_formats():
"""测试所有错误输出以 Error: 开头"""
print("=" * 70)
print("TASK 6: ERROR FORMAT VERIFICATION")
print("=" * 70)
# 重新加载模块以获取最新代码
import importlib
import main
importlib.reload(main)
from main import (
state,
close_port,
start_capture,
stop_capture,
send_command,
send_raw_string,
)
# 重置状态
state.serial_conn = None
state.is_capturing = False
state.capture_thread = None
with state.buffer_lock:
state.data_buffer.clear()
all_passed = True
# Test 1: close_port without open port
print("\n1. Testing close_port() without open port...")
result = close_port()
expected_prefix = "Error:"
if result.startswith(expected_prefix):
print(f" ✓ PASS: {result}")
else:
print(f" ✗ FAIL: Expected 'Error:' prefix, got: {result}")
all_passed = False
# Test 2: start_capture without open port
print("\n2. Testing start_capture() without open port...")
result = start_capture()
if result.startswith(expected_prefix):
print(f" ✓ PASS: {result}")
else:
print(f" ✗ FAIL: Expected 'Error:' prefix, got: {result}")
all_passed = False
# Test 3: stop_capture without running
print("\n3. Testing stop_capture() without running capture...")
result = stop_capture()
if result.startswith(expected_prefix):
print(f" ✓ PASS: {result}")
else:
print(f" ✗ FAIL: Expected 'Error:' prefix, got: {result}")
all_passed = False
# Test 4: send_command without open port
print("\n4. Testing send_command() without open port...")
result = send_command("test")
if result.startswith(expected_prefix):
print(f" ✓ PASS: {result}")
else:
print(f" ✗ FAIL: Expected 'Error:' prefix, got: {result}")
all_passed = False
# Test 5: send_raw_string without open port
print("\n5. Testing send_raw_string() without open port...")
result = send_raw_string("test")
if result.startswith(expected_prefix):
print(f" ✓ PASS: {result}")
else:
print(f" ✗ FAIL: Expected 'Error:' prefix, got: {result}")
all_passed = False
# Test 6: open invalid port (should return Error:)
print("\n6. Testing open_port() with invalid port...")
from main import open_port
result = open_port("COM999")
if result.startswith(expected_prefix):
print(f" ✓ PASS: {result}")
else:
print(f" ✗ FAIL: Expected 'Error:' prefix, got: {result}")
all_passed = False
print("\n" + "=" * 70)
if all_passed:
print("ALL ERROR FORMAT TESTS PASSED!")
else:
print("SOME TESTS FAILED!")
print("=" * 70)
return all_passed
if __name__ == "__main__":
success = test_all_error_formats()
exit(0 if success else 1)