Different @patch behavior between Python 2.7 and 3.6 (using mock)
up vote
2
down vote
favorite
@patch
does not seem to produce the same behavior under 2.7 and 3.6.
Here is my project structure:
project/
foo.py
bar.py
lol.py
tests/
test_project.py
foo.py:
class Foo:
pass
bar.py (imports Foo):
from project.foo import Foo
class Bar:
def __init__(self):
f = Foo()
lol.py (imports Bar):
from bar import Bar
class Lol:
def __init__(self):
b = Bar()
Since bar.py
imports Foo using from project.foo import Foo
, I am patching bar.Foo
(according to where to patch docs):
test_bar.py:
from project import lol
from project import bar
@patch('bar.Foo') # Works in 3.6, fails with 2.7
def test_lol(mock_Foo):
l = lol.Lol()
mock_Foo.assert_called()
This setup runs correctly in Python 3.6 but fails in 2.7 (Foo does not get patched).
However, if I switch my setup to:
test_bar.py:
from project import lol
# from project import bar # No need to import bar anymore
@patch('project.bar.Foo') # Works in 2.7, fails with 3.6
def test_lol(mock_Foo):
l = lol.Lol()
mock_Foo.assert_called()
It works in 2.7 but fails in 3.6.
What is a recommended way to use @patch
to make it produce results consistent between python versions?
Note: This problem only appears when I test lol.py
. If I call bar.py
from the unit test, I get consistent results using second setup @patch('cookie_test.bar.Foo')
and it works in both 2.7 and 3.6.
python python-3.x python-2.7 mocking patch
New contributor
add a comment |
up vote
2
down vote
favorite
@patch
does not seem to produce the same behavior under 2.7 and 3.6.
Here is my project structure:
project/
foo.py
bar.py
lol.py
tests/
test_project.py
foo.py:
class Foo:
pass
bar.py (imports Foo):
from project.foo import Foo
class Bar:
def __init__(self):
f = Foo()
lol.py (imports Bar):
from bar import Bar
class Lol:
def __init__(self):
b = Bar()
Since bar.py
imports Foo using from project.foo import Foo
, I am patching bar.Foo
(according to where to patch docs):
test_bar.py:
from project import lol
from project import bar
@patch('bar.Foo') # Works in 3.6, fails with 2.7
def test_lol(mock_Foo):
l = lol.Lol()
mock_Foo.assert_called()
This setup runs correctly in Python 3.6 but fails in 2.7 (Foo does not get patched).
However, if I switch my setup to:
test_bar.py:
from project import lol
# from project import bar # No need to import bar anymore
@patch('project.bar.Foo') # Works in 2.7, fails with 3.6
def test_lol(mock_Foo):
l = lol.Lol()
mock_Foo.assert_called()
It works in 2.7 but fails in 3.6.
What is a recommended way to use @patch
to make it produce results consistent between python versions?
Note: This problem only appears when I test lol.py
. If I call bar.py
from the unit test, I get consistent results using second setup @patch('cookie_test.bar.Foo')
and it works in both 2.7 and 3.6.
python python-3.x python-2.7 mocking patch
New contributor
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
@patch
does not seem to produce the same behavior under 2.7 and 3.6.
Here is my project structure:
project/
foo.py
bar.py
lol.py
tests/
test_project.py
foo.py:
class Foo:
pass
bar.py (imports Foo):
from project.foo import Foo
class Bar:
def __init__(self):
f = Foo()
lol.py (imports Bar):
from bar import Bar
class Lol:
def __init__(self):
b = Bar()
Since bar.py
imports Foo using from project.foo import Foo
, I am patching bar.Foo
(according to where to patch docs):
test_bar.py:
from project import lol
from project import bar
@patch('bar.Foo') # Works in 3.6, fails with 2.7
def test_lol(mock_Foo):
l = lol.Lol()
mock_Foo.assert_called()
This setup runs correctly in Python 3.6 but fails in 2.7 (Foo does not get patched).
However, if I switch my setup to:
test_bar.py:
from project import lol
# from project import bar # No need to import bar anymore
@patch('project.bar.Foo') # Works in 2.7, fails with 3.6
def test_lol(mock_Foo):
l = lol.Lol()
mock_Foo.assert_called()
It works in 2.7 but fails in 3.6.
What is a recommended way to use @patch
to make it produce results consistent between python versions?
Note: This problem only appears when I test lol.py
. If I call bar.py
from the unit test, I get consistent results using second setup @patch('cookie_test.bar.Foo')
and it works in both 2.7 and 3.6.
python python-3.x python-2.7 mocking patch
New contributor
@patch
does not seem to produce the same behavior under 2.7 and 3.6.
Here is my project structure:
project/
foo.py
bar.py
lol.py
tests/
test_project.py
foo.py:
class Foo:
pass
bar.py (imports Foo):
from project.foo import Foo
class Bar:
def __init__(self):
f = Foo()
lol.py (imports Bar):
from bar import Bar
class Lol:
def __init__(self):
b = Bar()
Since bar.py
imports Foo using from project.foo import Foo
, I am patching bar.Foo
(according to where to patch docs):
test_bar.py:
from project import lol
from project import bar
@patch('bar.Foo') # Works in 3.6, fails with 2.7
def test_lol(mock_Foo):
l = lol.Lol()
mock_Foo.assert_called()
This setup runs correctly in Python 3.6 but fails in 2.7 (Foo does not get patched).
However, if I switch my setup to:
test_bar.py:
from project import lol
# from project import bar # No need to import bar anymore
@patch('project.bar.Foo') # Works in 2.7, fails with 3.6
def test_lol(mock_Foo):
l = lol.Lol()
mock_Foo.assert_called()
It works in 2.7 but fails in 3.6.
What is a recommended way to use @patch
to make it produce results consistent between python versions?
Note: This problem only appears when I test lol.py
. If I call bar.py
from the unit test, I get consistent results using second setup @patch('cookie_test.bar.Foo')
and it works in both 2.7 and 3.6.
python python-3.x python-2.7 mocking patch
python python-3.x python-2.7 mocking patch
New contributor
New contributor
edited Nov 21 at 1:48
New contributor
asked Nov 21 at 1:36
Leonid Umanskiy
112
112
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
I cannot replicate the difference using 2.7 vs 3.6 upon adding __init__.py
files to your project
directory, and changing the import of Bar
in lol.py
:
from project.bar import Bar
In either case, you should not need to import bar
in your test - mock is taking care of finding bar
by parsing the string passed to the mock
decorator.
I suspect the error you're seeing is due to the fact that Python 3 uses absolute imports (https://www.python.org/dev/peps/pep-0328/)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I cannot replicate the difference using 2.7 vs 3.6 upon adding __init__.py
files to your project
directory, and changing the import of Bar
in lol.py
:
from project.bar import Bar
In either case, you should not need to import bar
in your test - mock is taking care of finding bar
by parsing the string passed to the mock
decorator.
I suspect the error you're seeing is due to the fact that Python 3 uses absolute imports (https://www.python.org/dev/peps/pep-0328/)
add a comment |
up vote
1
down vote
I cannot replicate the difference using 2.7 vs 3.6 upon adding __init__.py
files to your project
directory, and changing the import of Bar
in lol.py
:
from project.bar import Bar
In either case, you should not need to import bar
in your test - mock is taking care of finding bar
by parsing the string passed to the mock
decorator.
I suspect the error you're seeing is due to the fact that Python 3 uses absolute imports (https://www.python.org/dev/peps/pep-0328/)
add a comment |
up vote
1
down vote
up vote
1
down vote
I cannot replicate the difference using 2.7 vs 3.6 upon adding __init__.py
files to your project
directory, and changing the import of Bar
in lol.py
:
from project.bar import Bar
In either case, you should not need to import bar
in your test - mock is taking care of finding bar
by parsing the string passed to the mock
decorator.
I suspect the error you're seeing is due to the fact that Python 3 uses absolute imports (https://www.python.org/dev/peps/pep-0328/)
I cannot replicate the difference using 2.7 vs 3.6 upon adding __init__.py
files to your project
directory, and changing the import of Bar
in lol.py
:
from project.bar import Bar
In either case, you should not need to import bar
in your test - mock is taking care of finding bar
by parsing the string passed to the mock
decorator.
I suspect the error you're seeing is due to the fact that Python 3 uses absolute imports (https://www.python.org/dev/peps/pep-0328/)
edited Nov 21 at 2:26
answered Nov 21 at 2:20
Wes Doyle
6051619
6051619
add a comment |
add a comment |
Leonid Umanskiy is a new contributor. Be nice, and check out our Code of Conduct.
Leonid Umanskiy is a new contributor. Be nice, and check out our Code of Conduct.
Leonid Umanskiy is a new contributor. Be nice, and check out our Code of Conduct.
Leonid Umanskiy is a new contributor. Be nice, and check out our Code of Conduct.
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%2f53404113%2fdifferent-patch-behavior-between-python-2-7-and-3-6-using-mock%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