简体   繁体   中英

How to mock using sys.modules and with mock.patch (Python interference on static functions)

So I've this code that mocks two times, the first time by mocking imports with:

sys.modules['random'] = MagicMock()

The second time happens inside the unittest of a function that used that import, for example a function that used random

The tests. py is:

import sys
import unittest
from unittest import mock
from unittest.mock import MagicMock
import foo

sys.modules['random'] = MagicMock()

class test_foo(unittest.TestCase):

    def test_method(self):
        with mock.patch('random.choice', return_value = 2):
            object = foo.FooClass(3)
            self.assertEqual(2, object.method(), 'Should be 2')

    def test_staticmethod(self):
        with mock.patch('random.choice', return_value = 2):
            object = foo.FooClass(3)
            self.assertEqual(2, object.method(), 'should be 2')

The original file Foo.py is:

import random

class FooClass:

    def __init__(self,arg):
        self.arg = arg

    def method(self):
        print(random.choice)
        return random.choice([1,2,3])

    @staticmethod
    def staticmethod():
        print(random.choice)
        random.choice([1,2,3])

The two mocks contrarrest each other, and the mocking of random doesn't happen. When it prints random it actually prints:

<<bound method Random.choice of <random.Random object at 0x7fe688028018>>

I want that to print a MagicMock. Can someone help me understand what's happening? Why are they contrarresting each other?

You don't need to update the module source with sys.modules['random'] = MagicMock() without this line it works fine <MagicMock name='choice' id='...'> . patch already does all the work for the isolated temporary updating the method. See more explanation in the docs - Where to patch

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM