开发者

How can I inject an object into another namespace in python?

开发者 https://www.devze.com 2023-01-12 06:01 出处:网络
I\'m writing some unittests for code written by someone else here at the office. Python is not my strongest language. While I\'ve been successful wi开发者_运维问答th basic unit tests, mocking in pytho

I'm writing some unittests for code written by someone else here at the office. Python is not my strongest language. While I've been successful wi开发者_运维问答th basic unit tests, mocking in python is throwing me for a loop.

What I need to do is override a call to ConfigObj and inject my own mock config/fixture into any ConfigObj call.

settings.py

from configobj import ConfigObj
config = ConfigObj('/etc/myapp/config')

utils.py

from settings import config
"""lots of stuff methods using various config values."""

What I would like to do is, in my unittests for utils.py, inject myself either for ANY call to ConfigObj or settings.py itself.

Many of the mocking libraries expect me to Mock my own classes but in the case of this app, it doesn't have any explicit classes.

Can it be done or are the python namespace restrictions too strict that I can't intervene in what a module that I'm importing imports itself?

Side note: running 2.7 so I can't do any of the tricks I've read about from 2.5.


If the tests are in a separate file from from settings.py and utils.py you can create a file mock.py

import configobj

class MockConfigObj(object):
     #mock whatever you wan

configobj.ConfigObj = MockConfigObj

and then import mock before importing (from) any module that itself imports settings. This will ensure that settings.config is created with MockConfigObj. If you want a uniform global mocking, import it before any file that imports configobj.

This works because python will store configobj in sys.modules and check that before actually reading from a file on subsequent imports. in mock.py, the identifier ConfigObj is just a reference to the entry in sys.modules so that any changes that you make will be globally visible.

This strikes me as a little hacky though but it's the best that I can think of.


Python namespaces are not strict at all within the same scope. Just override the variable name containing your object (or the class itself and provided it) within the same scope you'd be expecting the original and that is good enough.

Now, whether or not what you're replacing it with behaves the same is up to you...


Couldn't you just overwrite the original function with another one?

There are no constants in Python, you can change everything, you could even do True = False.


I faced a similar situation before. Here is how I would go about addressing your problem.

Consider a test case for a function from utils.py.

import utils, unittest

class FooFunctionTests(unittest.TestCase):
    def setUp(self):
        utils._old_config = utils.config
        utils.config = MockClass()

    def tearDown(self):
        utils.config = utils._old_config
        del utils._old_config

    def test_foo_function_returns_correct_value(self):        
        self.assertEqual("success!", utils.foo())


The following page is a good one on mocking and import

http://www.relaxdiego.com/2014/04/mocking-objects-in-python.html

Say you have a file named my_package1.py with the following code:

class A(object): def init(self):

and you then import that in my_package2.py with the code

from my_package1 import A class A(object): def init(self):

The first line of my_package2.py creates a variable under the my_package2 namespace called A. Now you have two variables my_package1.A and my_package2.A that both point to the same class in memory. If you want the code in my_package2.py to use a mocked up class A, then you will need to mock my_package2.A not my_package1.A

0

精彩评论

暂无评论...
验证码 换一张
取 消