Is it possible to mock a module in python using unittest.mock
? I have a module named config
, while running tests I want to mock it by another module test_config
. how can I do that ? Thanks.
config.py:
CONF_VAR1 = "VAR1"
CONF_VAR2 = "VAR2"
test_config.py:
CONF_VAR1 = "test_VAR1"
CONF_VAR2 = "test_VAR2"
All other modules read config variables from the config
module. While running tests I want them to read config variables from test_config
module instead.
mock is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and make assertions about how they have been used. unittest. mock provides a core Mock class removing the need to create a host of stubs throughout your test suite.
You could mock out the entire DataFrame class using mock. patch("pandas. DataFrame", ...) . Note: it's not pd regardless of how (or even whether) you imported pandas in the current module.
In pytest , mocking can replace the return value of a function within a function. This is useful for testing the desired function and replacing the return value of a nested function within that desired function we are testing.
If you're always accessing the variables in config.py like this:
import config
...
config.VAR1
You can replace the config
module imported by whatever module you're actually trying to test. So, if you're testing a module called foo
, and it imports and uses config
, you can say:
from mock import patch
import foo
import config_test
....
with patch('foo.config', new=config_test):
foo.whatever()
But this isn't actually replacing the module globally, it's only replacing it within the foo
module's namespace. So you would need to patch it everywhere it's imported. It also wouldn't work if foo
does this instead of import config
:
from config import VAR1
You can also mess with sys.modules
to do this:
import config_test
import sys
sys.modules["config"] = config_test
# import modules that uses "import config" here, and they'll actually get config_test
But generally it's not a good idea to mess with sys.modules
, and I don't think this case is any different. I would favor all of the other suggestions made over it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With