鍍金池/ 問答/Python  測試  網(wǎng)絡安全/ 關于mock.patch()和mock.patch.object()的區(qū)別的問題

關于mock.patch()和mock.patch.object()的區(qū)別的問題

大家好!

這可能是一個關于unittest.mock.patch()unittest.mock.patch.object()的區(qū)別的問題,下面的代碼使用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()
回答
編輯回答
舊時光
2018年8月22日 12:20
編輯回答
萌二代

查詢了下,還是使用mock.patch()的時候,查找module的問題,修改了下,現(xiàn)在可以正常運行了。

代碼結構:

  • 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()
2017年8月29日 04:02