The question is in context of unit testing.
I have created an instance of the class I am testing, and trying to test one of its methods. The method uses data it gets from different class defined in separate module. I am going to mock that module.
How I can access my instance’s name space – I have to do it before running the method I am testing, to mock module which contain definition of the class my method is getting data from?
The question is in context of unit testing. I have created an instance of
Share
I am going to create an example here which I think parallels what you are trying to do.
Say you have some class that we’ll call
Datathat is defined in the modulefoo. Thefoomodule importsbarand a method offoo.Datacallsbar.get_data()to populate itself.You want to create a module
testthat will create an instance offoo.Data, but instead of using the actual modulebaryou want that instance to use a mocked version of this.You can set this up by importing
foofrom your test module, and then rebindingfoo.barto your mocked version of the module.Here is an example of how this might look:
bar.py:
foo.py:
test.py:
Although this will get you by for a simple test case, you are probably better off looking into a mocking library to handle this for you.