关于mock.patch()和mock.patch.object()的区别的问题

大家好!

这可能是一个关于unittest.mock.patch()unittest.mock.patch.object()的区别的问题,下面的代码使用mock.patch.object()时,可以正常运行,我不明白为什么使用mock.patch()的时候,会报错ModuleNotFoundError: No module named 'Person',这种情况是一定不能用mock.patch()吗?

# py_unittest.py

from unittest import TestCase
from unittest.mock import patch
from unittest import main
     
     
class Person(object):
    def __init__(self, name):
        self.name = name
        
    def print_name(self):
        print('My name is ' + self.name)
        
    def print_parents(self):
        mother = input("Enter mother's name: ")
        father = input("Enter father's name: ")
     
        print("{}'s parents are {} and {}.".format(self.name, mother, father))
        self.fake_func()

    def fake_func(self):
        pass

class FuncTest(TestCase):
    def test_print_parents(self):
        john = Person('John')
             
        with patch('builtins.input') as mocked_input:
            mocked_input.side_effect = ('Jo', 'Lee')
            with patch('builtins.print') as mocked_print:
                with patch.object(Person, "fake_func") as mocked_fake_func:
                # with patch('Person.fake_func') as mocked_fake_func: 如果启用这段代码会报错 ModuleNotFoundError: No module named 'Person'
                    john.print_parents()
                    mocked_print.assert_called_with("John's parents are Jo and Lee.")
                    mocked_fake_func.assert_called_once()
 
if __name__ == '__main__':
    main()
阅读 10.3k
2 个回答

查询了下,还是使用mock.patch()的时候,查找module的问题,修改了下,现在可以正常运行了。

代码结构:

  • py_unittest.py
  • person(目录)

    • __init__.py
    • person.py

person.py的代码

class Person(object):
    def __init__(self, name):
        self.name = name
    
    def print_name(self):
        print('My name is ' + self.name)
    
    def print_parents(self):
        mother = input("Enter mother's name: ")
        father = input("Enter father's name: ")

        print("{}'s parents are {} and {}.".format(self.name, mother, father))
        self.fake_func()

    def fake_func(self):
        pass

__init__.py的代码

__all__ = ['Person',]

from .person import Person

py_unittest.py的代码

from unittest import TestCase
from unittest.mock import patch
from unittest import main
from person import Person

class FuncTest(TestCase):
    def test_print_parents(self):
        john = Person('John')
             
        with patch('builtins.input') as mocked_input:
            mocked_input.side_effect = ('Jo', 'Lee')
            with patch('builtins.print') as mocked_print:
                # with patch.object(person.Person, "fake_func") as mocked_fake_func:
                with patch('person.Person.fake_func') as mocked_fake_func:
                    john.print_parents()
                    mocked_print.assert_called_with("John's parents are Jo and Lee.")
                    mocked_fake_func.assert_called_once()

if __name__ == '__main__':
    main()
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏