using mocker to patch with pytest
up vote
0
down vote
favorite
I installed pytest-mock and am trying to try using mocker to function as patch does, but I get "Type Error: Need a valid target to patch. You supplied 'return a + b'"
# test_capitalize.py
import time
def sum( a, b):
time.sleep(10)
return a + b
def test_sum(mocker):
mocker.patch('return a + b');
assertEqual(sum(2,3), 9)
python mocking pytest
add a comment |
up vote
0
down vote
favorite
I installed pytest-mock and am trying to try using mocker to function as patch does, but I get "Type Error: Need a valid target to patch. You supplied 'return a + b'"
# test_capitalize.py
import time
def sum( a, b):
time.sleep(10)
return a + b
def test_sum(mocker):
mocker.patch('return a + b');
assertEqual(sum(2,3), 9)
python mocking pytest
You can't patch a string. I assume you want to patch thesum
function?mocker.patch(__name__ + '.sum', return_value=9)
or, if you want to patch with a custom function, useside_effect
:mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b)
etc.
– hoefling
Nov 22 at 16:47
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I installed pytest-mock and am trying to try using mocker to function as patch does, but I get "Type Error: Need a valid target to patch. You supplied 'return a + b'"
# test_capitalize.py
import time
def sum( a, b):
time.sleep(10)
return a + b
def test_sum(mocker):
mocker.patch('return a + b');
assertEqual(sum(2,3), 9)
python mocking pytest
I installed pytest-mock and am trying to try using mocker to function as patch does, but I get "Type Error: Need a valid target to patch. You supplied 'return a + b'"
# test_capitalize.py
import time
def sum( a, b):
time.sleep(10)
return a + b
def test_sum(mocker):
mocker.patch('return a + b');
assertEqual(sum(2,3), 9)
python mocking pytest
python mocking pytest
edited Nov 23 at 15:54
seron
1,93632036
1,93632036
asked Nov 22 at 16:25
Kevin
5918
5918
You can't patch a string. I assume you want to patch thesum
function?mocker.patch(__name__ + '.sum', return_value=9)
or, if you want to patch with a custom function, useside_effect
:mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b)
etc.
– hoefling
Nov 22 at 16:47
add a comment |
You can't patch a string. I assume you want to patch thesum
function?mocker.patch(__name__ + '.sum', return_value=9)
or, if you want to patch with a custom function, useside_effect
:mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b)
etc.
– hoefling
Nov 22 at 16:47
You can't patch a string. I assume you want to patch the
sum
function? mocker.patch(__name__ + '.sum', return_value=9)
or, if you want to patch with a custom function, use side_effect
: mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b)
etc.– hoefling
Nov 22 at 16:47
You can't patch a string. I assume you want to patch the
sum
function? mocker.patch(__name__ + '.sum', return_value=9)
or, if you want to patch with a custom function, use side_effect
: mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b)
etc.– hoefling
Nov 22 at 16:47
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
patch
requires a path to the function being patched. You could do something like this:
import pytest
def sum(a, b):
return a + b
def test_sum1(mocker):
mocker.patch(__name__ + ".sum", return_value=9)
assert sum(2, 3) == 9
def test_sum2(mocker):
def crazy_sum(a, b):
return b + b
mocker.patch(__name__ + ".sum", side_effect=crazy_sum)
assert sum(2, 3) == 6
Result:
$ pytest -v patch_test.py
============= test session starts ==============
platform cygwin -- Python 3.6.4, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/xyz/temp, inifile:
plugins: mock-1.10.0, cov-2.6.0
collected 2 items
patch_test.py::test_sum1 PASSED [ 50%]
patch_test.py::test_sum2 PASSED [100%]
=========== 2 passed in 0.02 seconds ===========
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
accepted
patch
requires a path to the function being patched. You could do something like this:
import pytest
def sum(a, b):
return a + b
def test_sum1(mocker):
mocker.patch(__name__ + ".sum", return_value=9)
assert sum(2, 3) == 9
def test_sum2(mocker):
def crazy_sum(a, b):
return b + b
mocker.patch(__name__ + ".sum", side_effect=crazy_sum)
assert sum(2, 3) == 6
Result:
$ pytest -v patch_test.py
============= test session starts ==============
platform cygwin -- Python 3.6.4, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/xyz/temp, inifile:
plugins: mock-1.10.0, cov-2.6.0
collected 2 items
patch_test.py::test_sum1 PASSED [ 50%]
patch_test.py::test_sum2 PASSED [100%]
=========== 2 passed in 0.02 seconds ===========
add a comment |
up vote
1
down vote
accepted
patch
requires a path to the function being patched. You could do something like this:
import pytest
def sum(a, b):
return a + b
def test_sum1(mocker):
mocker.patch(__name__ + ".sum", return_value=9)
assert sum(2, 3) == 9
def test_sum2(mocker):
def crazy_sum(a, b):
return b + b
mocker.patch(__name__ + ".sum", side_effect=crazy_sum)
assert sum(2, 3) == 6
Result:
$ pytest -v patch_test.py
============= test session starts ==============
platform cygwin -- Python 3.6.4, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/xyz/temp, inifile:
plugins: mock-1.10.0, cov-2.6.0
collected 2 items
patch_test.py::test_sum1 PASSED [ 50%]
patch_test.py::test_sum2 PASSED [100%]
=========== 2 passed in 0.02 seconds ===========
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
patch
requires a path to the function being patched. You could do something like this:
import pytest
def sum(a, b):
return a + b
def test_sum1(mocker):
mocker.patch(__name__ + ".sum", return_value=9)
assert sum(2, 3) == 9
def test_sum2(mocker):
def crazy_sum(a, b):
return b + b
mocker.patch(__name__ + ".sum", side_effect=crazy_sum)
assert sum(2, 3) == 6
Result:
$ pytest -v patch_test.py
============= test session starts ==============
platform cygwin -- Python 3.6.4, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/xyz/temp, inifile:
plugins: mock-1.10.0, cov-2.6.0
collected 2 items
patch_test.py::test_sum1 PASSED [ 50%]
patch_test.py::test_sum2 PASSED [100%]
=========== 2 passed in 0.02 seconds ===========
patch
requires a path to the function being patched. You could do something like this:
import pytest
def sum(a, b):
return a + b
def test_sum1(mocker):
mocker.patch(__name__ + ".sum", return_value=9)
assert sum(2, 3) == 9
def test_sum2(mocker):
def crazy_sum(a, b):
return b + b
mocker.patch(__name__ + ".sum", side_effect=crazy_sum)
assert sum(2, 3) == 6
Result:
$ pytest -v patch_test.py
============= test session starts ==============
platform cygwin -- Python 3.6.4, pytest-3.10.1, py-1.7.0, pluggy-0.8.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/xyz/temp, inifile:
plugins: mock-1.10.0, cov-2.6.0
collected 2 items
patch_test.py::test_sum1 PASSED [ 50%]
patch_test.py::test_sum2 PASSED [100%]
=========== 2 passed in 0.02 seconds ===========
edited Nov 23 at 14:22
answered Nov 23 at 14:16
seron
1,93632036
1,93632036
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53434986%2fusing-mocker-to-patch-with-pytest%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
You can't patch a string. I assume you want to patch the
sum
function?mocker.patch(__name__ + '.sum', return_value=9)
or, if you want to patch with a custom function, useside_effect
:mocker.patch(__name__ + '.sum', side_effect=lambda a, b: a - b)
etc.– hoefling
Nov 22 at 16:47