Python测试和模拟导入的模块

问题描述 投票:1回答:1

我有一个模块导入另一个模块,如下所示:

#main-file.py
import src.FetchFunction.video_service.fields.urls as urls

def some_func():
  return urls.fetch()

现在我想像这样测试这个文件:

import unittest
import src.FetchFunction.video_service.fields.urls as urls
from unittest.mock import MagicMock

class MainFileTest(unittest.TestCase):

    def test_example(self):
      urls.fetch = MagicMock(return_value='mocked_resp')
      assertSomething()

这部分运作良好,做我想要的。但这会影响其他测试文件...我的意思是我有其他测试使用“urls.fetch”,而现在他们获得了上面模拟的响应而不是获得正确的流程。

任何的想法?

  • 很确定它不相关但我使用pytest来运行我的测试
python python-3.x python-2.7 pytest
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.