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

關(guān)于mock.patch()和mock.patch.object()的區(qū)別的問(wèn)題

大家好!

這可能是一個(gè)關(guān)于unittest.mock.patch()unittest.mock.patch.object()的區(qū)別的問(wèn)題,下面的代碼使用mock.patch.object()時(shí),可以正常運(yùn)行,我不明白為什么使用mock.patch()的時(shí)候,會(huì)報(bào)錯(cuò)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: 如果啟用這段代碼會(huì)報(bào)錯(cuò) 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()
回答
編輯回答
編輯回答
萌二代

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

代碼結(jié)構(gòu):

  • 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