Mocking Python modules across multiple test scripts
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
In my implementation script I have a line which logs a metric:
from datadog import statsd
def some_function:
statsd.increment('some_metric')
From my test script, I assert that statsd.increment() is called by mocking out the datadog module:
datadog = Mock()
sys.modules['datadog'] = datadog
def test():
some_function()
datadog.statsd.increment.assert_called()
This works fine and passes. But as soon as I add ANOTHER script which calls some_function()
without mocking datadog, that script runs beforehand and loads the real datadog module into the cache. The above test then fails because some_function()
is no longer using the mock datadog, it uses the real (cached) datadog.
How can I address this? Is it possible to remove the module from the cache?
python pytest
add a comment |
In my implementation script I have a line which logs a metric:
from datadog import statsd
def some_function:
statsd.increment('some_metric')
From my test script, I assert that statsd.increment() is called by mocking out the datadog module:
datadog = Mock()
sys.modules['datadog'] = datadog
def test():
some_function()
datadog.statsd.increment.assert_called()
This works fine and passes. But as soon as I add ANOTHER script which calls some_function()
without mocking datadog, that script runs beforehand and loads the real datadog module into the cache. The above test then fails because some_function()
is no longer using the mock datadog, it uses the real (cached) datadog.
How can I address this? Is it possible to remove the module from the cache?
python pytest
Are you saying it's failing because it's getting the "mock datadog" instead of the "real datadog"?
– Matt Messersmith
Nov 15 '18 at 22:03
It passed when getting the mock datadog, now it fails because a previous running test has executed it, giving it the real datadog, which is then cached (so the mock cannot be used later on)
– FBryant87
Nov 16 '18 at 9:49
add a comment |
In my implementation script I have a line which logs a metric:
from datadog import statsd
def some_function:
statsd.increment('some_metric')
From my test script, I assert that statsd.increment() is called by mocking out the datadog module:
datadog = Mock()
sys.modules['datadog'] = datadog
def test():
some_function()
datadog.statsd.increment.assert_called()
This works fine and passes. But as soon as I add ANOTHER script which calls some_function()
without mocking datadog, that script runs beforehand and loads the real datadog module into the cache. The above test then fails because some_function()
is no longer using the mock datadog, it uses the real (cached) datadog.
How can I address this? Is it possible to remove the module from the cache?
python pytest
In my implementation script I have a line which logs a metric:
from datadog import statsd
def some_function:
statsd.increment('some_metric')
From my test script, I assert that statsd.increment() is called by mocking out the datadog module:
datadog = Mock()
sys.modules['datadog'] = datadog
def test():
some_function()
datadog.statsd.increment.assert_called()
This works fine and passes. But as soon as I add ANOTHER script which calls some_function()
without mocking datadog, that script runs beforehand and loads the real datadog module into the cache. The above test then fails because some_function()
is no longer using the mock datadog, it uses the real (cached) datadog.
How can I address this? Is it possible to remove the module from the cache?
python pytest
python pytest
edited Nov 16 '18 at 10:17
FBryant87
asked Nov 15 '18 at 16:22
FBryant87FBryant87
1,79012441
1,79012441
Are you saying it's failing because it's getting the "mock datadog" instead of the "real datadog"?
– Matt Messersmith
Nov 15 '18 at 22:03
It passed when getting the mock datadog, now it fails because a previous running test has executed it, giving it the real datadog, which is then cached (so the mock cannot be used later on)
– FBryant87
Nov 16 '18 at 9:49
add a comment |
Are you saying it's failing because it's getting the "mock datadog" instead of the "real datadog"?
– Matt Messersmith
Nov 15 '18 at 22:03
It passed when getting the mock datadog, now it fails because a previous running test has executed it, giving it the real datadog, which is then cached (so the mock cannot be used later on)
– FBryant87
Nov 16 '18 at 9:49
Are you saying it's failing because it's getting the "mock datadog" instead of the "real datadog"?
– Matt Messersmith
Nov 15 '18 at 22:03
Are you saying it's failing because it's getting the "mock datadog" instead of the "real datadog"?
– Matt Messersmith
Nov 15 '18 at 22:03
It passed when getting the mock datadog, now it fails because a previous running test has executed it, giving it the real datadog, which is then cached (so the mock cannot be used later on)
– FBryant87
Nov 16 '18 at 9:49
It passed when getting the mock datadog, now it fails because a previous running test has executed it, giving it the real datadog, which is then cached (so the mock cannot be used later on)
– FBryant87
Nov 16 '18 at 9:49
add a comment |
2 Answers
2
active
oldest
votes
Have you tried to mock the datalog
module inside your function test
? As long as your other scripts are not running concurrently with your test, this may work. That way the mock itself will be set only when the function is called, instead of being set in your script scope.
The tests aren't running concurrently but it's the ordering that's the problem. A test which doesn't mock the module runs first and caches the module, so that it can't be mocked by any test running after that.
– FBryant87
Nov 16 '18 at 10:10
add a comment |
You could use unittest.mock.patch
. If you are using pytest you can do the same with the monkeypatch
fixture.
from datadog import statsd
from unittest.mock import Mock, patch
def some_function():
statsd.increment()
def test_some_function():
with patch('datadog.statsd', Mock()) as mock_statsd:
some_function()
mock_statsd.increment.assert_called()
test_some_function()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323761%2fmocking-python-modules-across-multiple-test-scripts%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Have you tried to mock the datalog
module inside your function test
? As long as your other scripts are not running concurrently with your test, this may work. That way the mock itself will be set only when the function is called, instead of being set in your script scope.
The tests aren't running concurrently but it's the ordering that's the problem. A test which doesn't mock the module runs first and caches the module, so that it can't be mocked by any test running after that.
– FBryant87
Nov 16 '18 at 10:10
add a comment |
Have you tried to mock the datalog
module inside your function test
? As long as your other scripts are not running concurrently with your test, this may work. That way the mock itself will be set only when the function is called, instead of being set in your script scope.
The tests aren't running concurrently but it's the ordering that's the problem. A test which doesn't mock the module runs first and caches the module, so that it can't be mocked by any test running after that.
– FBryant87
Nov 16 '18 at 10:10
add a comment |
Have you tried to mock the datalog
module inside your function test
? As long as your other scripts are not running concurrently with your test, this may work. That way the mock itself will be set only when the function is called, instead of being set in your script scope.
Have you tried to mock the datalog
module inside your function test
? As long as your other scripts are not running concurrently with your test, this may work. That way the mock itself will be set only when the function is called, instead of being set in your script scope.
answered Nov 15 '18 at 21:58
Aurora WangAurora Wang
843418
843418
The tests aren't running concurrently but it's the ordering that's the problem. A test which doesn't mock the module runs first and caches the module, so that it can't be mocked by any test running after that.
– FBryant87
Nov 16 '18 at 10:10
add a comment |
The tests aren't running concurrently but it's the ordering that's the problem. A test which doesn't mock the module runs first and caches the module, so that it can't be mocked by any test running after that.
– FBryant87
Nov 16 '18 at 10:10
The tests aren't running concurrently but it's the ordering that's the problem. A test which doesn't mock the module runs first and caches the module, so that it can't be mocked by any test running after that.
– FBryant87
Nov 16 '18 at 10:10
The tests aren't running concurrently but it's the ordering that's the problem. A test which doesn't mock the module runs first and caches the module, so that it can't be mocked by any test running after that.
– FBryant87
Nov 16 '18 at 10:10
add a comment |
You could use unittest.mock.patch
. If you are using pytest you can do the same with the monkeypatch
fixture.
from datadog import statsd
from unittest.mock import Mock, patch
def some_function():
statsd.increment()
def test_some_function():
with patch('datadog.statsd', Mock()) as mock_statsd:
some_function()
mock_statsd.increment.assert_called()
test_some_function()
add a comment |
You could use unittest.mock.patch
. If you are using pytest you can do the same with the monkeypatch
fixture.
from datadog import statsd
from unittest.mock import Mock, patch
def some_function():
statsd.increment()
def test_some_function():
with patch('datadog.statsd', Mock()) as mock_statsd:
some_function()
mock_statsd.increment.assert_called()
test_some_function()
add a comment |
You could use unittest.mock.patch
. If you are using pytest you can do the same with the monkeypatch
fixture.
from datadog import statsd
from unittest.mock import Mock, patch
def some_function():
statsd.increment()
def test_some_function():
with patch('datadog.statsd', Mock()) as mock_statsd:
some_function()
mock_statsd.increment.assert_called()
test_some_function()
You could use unittest.mock.patch
. If you are using pytest you can do the same with the monkeypatch
fixture.
from datadog import statsd
from unittest.mock import Mock, patch
def some_function():
statsd.increment()
def test_some_function():
with patch('datadog.statsd', Mock()) as mock_statsd:
some_function()
mock_statsd.increment.assert_called()
test_some_function()
answered Nov 18 '18 at 16:50
soundstripesoundstripe
53038
53038
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53323761%2fmocking-python-modules-across-multiple-test-scripts%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Are you saying it's failing because it's getting the "mock datadog" instead of the "real datadog"?
– Matt Messersmith
Nov 15 '18 at 22:03
It passed when getting the mock datadog, now it fails because a previous running test has executed it, giving it the real datadog, which is then cached (so the mock cannot be used later on)
– FBryant87
Nov 16 '18 at 9:49