import unittest from unittest.mock import patch, MagicMock from cloud import chuli # 替换为实际模块名 class TestChuliFunction(unittest.TestCase): @patch('wordcloud.WordCloud') @patch('matplotlib.pyplot.imshow') @patch('matplotlib.pyplot.show') def test_chuli(self, mock_plt_show, mock_plt_imshow, MockWordCloud): # 创建一个模拟的 WordCloud 对象 mock_wc = MockWordCloud.return_value mock_wc.generate.return_value = None # 输入测试数据 test_input = ['测试', '词云', '功能', '正常'] # 调用 chuli 函数 chuli(test_input) # 验证 WordCloud 的构造函数是否被正确调用 MockWordCloud.assert_called_once_with( background_color='white', height=1000, width=1000, font_path='simsun.ttc' ) # 验证 WordCloud 的 generate 方法是否被正确调用 mock_wc.generate.assert_called_once_with('测试 词云 功能 正常') # 验证 plt.imshow 是否被调用 mock_plt_imshow.assert_called_once_with(mock_wc) # 验证 plt.show 是否被调用 mock_plt_show.assert_called_once() if __name__ == '__main__': unittest.main()