forked from aimacode/aima-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverify_mock.py
More file actions
178 lines (139 loc) · 4.87 KB
/
Copy pathverify_mock.py
File metadata and controls
178 lines (139 loc) · 4.87 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env python
"""
簡單的驗證腳本,用於測試 matplotlib 模擬功能。
這個腳本不需要 pytest 就能運行。
"""
import sys
import os
# 添加父目錄到路徑
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# 設定非互動式後端
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from unittest.mock import patch, MagicMock
import numpy as np
print("=" * 60)
print("測試 1: 基本繪圖(使用 Agg 後端)")
print("=" * 60)
try:
fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
with patch('matplotlib.pyplot.show'):
plt.show() # 被模擬,不會顯示
plt.close()
print("✓ 基本繪圖測試通過")
except Exception as e:
print(f"✗ 基本繪圖測試失敗: {e}")
print("\n" + "=" * 60)
print("測試 2: 模擬 plt.show()")
print("=" * 60)
try:
show_called = False
def mock_show():
global show_called
show_called = True
with patch('matplotlib.pyplot.show', side_effect=mock_show):
plt.plot([1, 2, 3])
plt.show()
assert show_called, "show() 應該被調用"
print("✓ plt.show() 模擬測試通過")
except Exception as e:
print(f"✗ plt.show() 模擬測試失敗: {e}")
print("\n" + "=" * 60)
print("測試 3: 模擬 Figure 物件")
print("=" * 60)
try:
mock_fig = MagicMock(spec=plt.Figure)
mock_ax = MagicMock()
mock_fig.add_subplot.return_value = mock_ax
with patch('matplotlib.pyplot.figure', return_value=mock_fig):
fig = plt.figure(figsize=(10, 5))
ax = fig.add_subplot(111)
ax.plot([1, 2, 3])
assert mock_fig.add_subplot.called, "add_subplot 應該被調用"
assert ax.plot.called, "plot 應該被調用"
print("✓ Figure 物件模擬測試通過")
except Exception as e:
print(f"✗ Figure 物件模擬測試失敗: {e}")
print("\n" + "=" * 60)
print("測試 4: 捕獲繪圖調用")
print("=" * 60)
try:
calls = []
def track_plot(*args, **kwargs):
calls.append(('plot', args, kwargs))
return []
def track_show(*args, **kwargs):
calls.append(('show', args, kwargs))
with patch('matplotlib.pyplot.plot', side_effect=track_plot), \
patch('matplotlib.pyplot.show', side_effect=track_show):
plt.plot([1, 2, 3], [4, 5, 6])
plt.show()
assert len(calls) == 2, f"應該有 2 個調用,但有 {len(calls)} 個"
assert calls[0][0] == 'plot', "第一個調用應該是 plot"
assert calls[1][0] == 'show', "第二個調用應該是 show"
print(f"✓ 捕獲到 {len(calls)} 個調用:")
for call_name, args, kwargs in calls:
print(f" - {call_name}() 被調用")
except Exception as e:
print(f"✗ 捕獲繪圖調用測試失敗: {e}")
print("\n" + "=" * 60)
print("測試 5: 複雜繪圖場景")
print("=" * 60)
try:
with patch('matplotlib.pyplot.show'):
# 創建多個圖表
fig1, ax1 = plt.subplots()
ax1.plot([1, 2, 3], [1, 4, 9])
ax1.set_title('圖表 1')
fig2, (ax2, ax3) = plt.subplots(1, 2, figsize=(10, 5))
ax2.scatter([1, 2, 3], [3, 2, 1])
ax3.bar([1, 2, 3], [3, 5, 2])
plt.show()
plt.close('all')
print("✓ 複雜繪圖場景測試通過")
except Exception as e:
print(f"✗ 複雜繪圖場景測試失敗: {e}")
print("\n" + "=" * 60)
print("測試 6: 熱圖繪製")
print("=" * 60)
try:
imshow_called = False
def track_imshow(*args, **kwargs):
global imshow_called
imshow_called = True
return None
with patch('matplotlib.pyplot.imshow', side_effect=track_imshow), \
patch('matplotlib.pyplot.show'):
data = np.random.rand(5, 5)
plt.imshow(data, cmap='viridis')
plt.show()
assert imshow_called, "imshow 應該被調用"
print("✓ 熱圖繪製測試通過")
except Exception as e:
print(f"✗ 熱圖繪製測試失敗: {e}")
print("\n" + "=" * 60)
print("測試 7: conftest.py 導入測試")
print("=" * 60)
try:
# 嘗試導入 conftest 模組
from conftest import mock_matplotlib_show
print("✓ conftest.py 成功導入")
print(f" 可用的 fixtures:")
# 列出可用的 fixtures
import conftest
fixtures = [name for name in dir(conftest) if not name.startswith('_')]
for fixture in fixtures:
obj = getattr(conftest, fixture)
if hasattr(obj, '_pytestfixturefunction'):
print(f" - {fixture}")
except Exception as e:
print(f"✗ conftest.py 導入測試失敗: {e}")
print("\n" + "=" * 60)
print("所有測試完成!")
print("=" * 60)
print("\n建議:")
print("1. 安裝 pytest 以運行完整測試套件: pip install pytest pytest-cov")
print("2. 運行完整測試: pytest tests/test_mock_figures.py -v")
print("3. 查看文檔: tests/MOCK_FIGURES_README.md")