如何用pytest/unittest模拟write_files及内部文件写入函数?
单元测试模拟方案
首先注意你的write_files函数存在几个未定义变量(arg1、format1、format_x),假设arg1是参数a,format1是控制输出格式的参数,format_x是其他格式分支,基于此给出测试方案。
我们用Python标准库的unittest.mock实现模拟,核心是替换实际的文件操作和外部函数调用,验证逻辑分支是否正确执行。
测试步骤与代码示例
1. 基础导入与测试类定义
import unittest from unittest.mock import patch, MagicMock from your_module import write_files # 替换成你的函数所在模块名
2. 模拟os模块与外部函数
用@patch装饰器批量模拟需要替换的对象,针对不同分支编写测试用例:
class TestWriteFiles(unittest.TestCase): @patch("your_module.os.makedirs") @patch("your_module.os.path.dirname") @patch("your_module.get_write_json") @patch("your_module.get_write_txt") @patch("your_module.get_write_xlsx") def test_write_files_xlsx_format(self, mock_xlsx, mock_txt, mock_json, mock_dirname, mock_makedirs): # 模拟os.path.dirname返回非空路径 mock_dirname.return_value = "/test/path" # 调用函数,传入xlsx格式参数 write_files(a="/test/path/file.xlsx", b={"data": "test"}, format1="xlsx", format_x=False) # 验证目录创建函数被正确调用 mock_makedirs.assert_called_once_with("/test/path", exist_ok=True) # 验证xlsx写入函数被调用,参数匹配 mock_xlsx.assert_called_once_with("/test/path/file.xlsx", {"data": "test"}) # 验证其他写入函数未触发 mock_txt.assert_not_called() mock_json.assert_not_called() @patch("your_module.os.makedirs") @patch("your_module.os.path.dirname") @patch("your_module.get_write_json") @patch("your_module.get_write_txt") @patch("your_module.get_write_xlsx") def test_write_files_txt_format(self, mock_xlsx, mock_txt, mock_json, mock_dirname, mock_makedirs): mock_dirname.return_value = "/test/txt/path" # 测试txt格式分支 write_files(a="/test/txt/path/file.txt", b="test content", format1="txt", format_x=False) mock_makedirs.assert_called_once_with("/test/txt/path", exist_ok=True) mock_txt.assert_called_once_with("/test/txt/path/file.txt", "test content") mock_xlsx.assert_not_called() mock_json.assert_not_called() @patch("your_module.os.makedirs") @patch("your_module.os.path.dirname") @patch("your_module.get_write_json") @patch("your_module.get_write_txt") @patch("your_module.get_write_xlsx") def test_write_files_multiple_formats(self, mock_xlsx, mock_txt, mock_json, mock_dirname, mock_makedirs): mock_dirname.return_value = "/test/multi/path" # 测试同时匹配xlsx和json格式的场景 write_files(a="/test/multi/path/file", b={"data": "multi"}, format1="xlsx,json", format_x=False) mock_makedirs.assert_called_once_with("/test/multi/path", exist_ok=True) mock_xlsx.assert_called_once_with("/test/multi/path/file", {"data": "multi"}) mock_json.assert_called_once_with("/test/multi/path/file", {"data": "multi"}) mock_txt.assert_not_called() @patch("your_module.os.makedirs") @patch("your_module.os.path.dirname") @patch("your_module.get_write_json") @patch("your_module.get_write_txt") @patch("your_module.get_write_xlsx") def test_write_files_format_x_branch(self, mock_xlsx, mock_txt, mock_json, mock_dirname, mock_makedirs): mock_dirname.return_value = "/test/other/path" # 测试format_x为True的分支 write_files(a="/test/other/path/file", b="test", format1="csv", format_x=True) mock_makedirs.assert_called_once_with("/test/other/path", exist_ok=True) # 验证所有写入函数都未触发 mock_xlsx.assert_not_called() mock_txt.assert_not_called() mock_json.assert_not_called()
关键说明
- patch路径规则:必须填写
write_files所在模块中被引用对象的路径(比如your_module.get_write_xlsx),而非外部模块路径,这是mock生效的核心前提。 - 调用验证:用
assert_called_once_with验证函数是否按预期调用、参数是否匹配;用assert_not_called验证分支未触发。 - 返回值模拟:通过
mock_dirname.return_value设置返回结果,控制os.makedirs的执行逻辑。
如果你的arg1、format1等变量定义和假设不同,只需调整测试中的参数传递和mock逻辑即可。
内容的提问来源于stack exchange,提问作者Digital_AI
相关产品推荐
相关产品推荐

