You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何测试Fixture中实例化类内的os.makedirs方法?

如何测试Fixture中实例化类的os.makedirs方法?

你遇到的ModuleNotFoundError: No module named 'f_get_images'是因为两个核心问题:patch的目标路径错误,以及fixture的调用方式不对。下面是问题分析和修正方案:

错误原因

  1. Patch路径错误:@patch('f_get_images.exists')里的f_get_images是fixture名称,不是os模块的引用路径。Images类中调用的os.path.exists和os.makedirs,实际是调用该类所在模块(src.get_images)中的os引用,所以patch目标必须指向这个路径。
  2. Fixture调用错误:继承unittest.TestCase的测试类中,通过@pytest.mark.usefixtures注入的fixture,不能直接用名称调用,需要通过参数传入或self访问。
  3. 断言参数缺失:mock_make_dirs.assert_called_with()没有传入预期的路径参数,无法验证调用是否正确。

修正后的测试代码

方案一:纯Pytest测试类(推荐)

放弃unittest.TestCase,用Pytest原生的类测试风格,更简洁直观:

import pytest
from unittest.mock import patch
from src.get_images import Images

@pytest.fixture(scope="class")
def images_instance():
    return Images("test")

class TestImagesCreateFolder:
    def test_folder_exists_skips_makedirs(self, images_instance, mocker):
        # 模拟文件夹已存在
        mocker.patch('src.get_images.os.path.exists', return_value=True)
        mock_makedirs = mocker.patch('src.get_images.os.makedirs')
        
        images_instance.create_folder()
        
        # 验证makedirs未被调用
        mock_makedirs.assert_not_called()

    def test_folder_not_exists_calls_makedirs(self, images_instance, mocker):
        expected_path = r"C:\Users\test\Pictures\WindowsSpotlight"
        # 模拟文件夹不存在
        mocker.patch('src.get_images.os.path.exists', return_value=False)
        mock_makedirs = mocker.patch('src.get_images.os.makedirs')
        
        images_instance.create_folder()
        
        # 验证makedirs以正确路径被调用一次
        mock_makedirs.assert_called_once_with(expected_path)

方案二:结合Unittest.TestCase

如果需要保留unittest.TestCase,可以通过参数传入fixture:

import pytest
from unittest.mock import patch
import unittest
from src.get_images import Images

@pytest.fixture(scope="class")
def images_instance():
    return Images("test")

@pytest.mark.usefixtures("images_instance")
class TestImagesCreateFolder(unittest.TestCase):
    @patch('src.get_images.os.path.exists')
    @patch('src.get_images.os.makedirs')
    def test_folder_exists_skips_makedirs(self, mock_makedirs, mock_exists, images_instance):
        mock_exists.return_value = True
        
        images_instance.create_folder()
        
        mock_makedirs.assert_not_called()

    @patch('src.get_images.os.path.exists')
    @patch('src.get_images.os.makedirs')
    def test_folder_not_exists_calls_makedirs(self, mock_makedirs, mock_exists, images_instance):
        expected_path = r"C:\Users\test\Pictures\WindowsSpotlight"
        mock_exists.return_value = False
        
        images_instance.create_folder()
        
        mock_makedirs.assert_called_once_with(expected_path)

关键注意点

  • Patch路径规则:Python mock遵循"在哪里调用就patch哪里"的原则,所以必须patchsrc.get_images.os(即Images类所在模块中的os引用),而非全局的os模块。
  • 覆盖两种场景:测试要覆盖文件夹存在和不存在两种情况,确保逻辑的完整性。
  • Fixture命名:避免用f_xxx这类容易和模块名混淆的命名,改用更清晰的名称如images_instance。

内容的提问来源于stack exchange,提问作者Omega

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 19:23:14