Questions about the difference between mock.patch () and mock.patch.object ()

Hello, everyone!

this may be a question about the difference between unittest.mock.patch () and unittest.mock.patch.object () . The following code works normally when using mock.patch.object () . I don"t understand why an error ModuleNotFoundError: No module named "Person" will be reported when using mock.patch () . Is it absolutely impossible to use mock.patch () ?

-sharp 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:
                -sharp 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()
Mar.03,2021

python/1281" rel=" nofollow noreferrer "> use of python mock patch


when querying or using mock.patch () , look for the problem with module, modify it, and now work properly.

Code structure:

  • py_unittest.py
  • person (directory)

    • _ _ init__.py
    • person.py

person.py Code

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 Code

__all__ = ['Person',]

from .person import Person

py_unittest.py Code

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:
                -sharp 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()
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9a3ea-17ba.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9a3ea-17ba.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?