Sorry for only providing simple code here, I just translate my complex code into simple code for you to know my point.
For example:
main.py
from lib import testlib
print testlib.func1()
lib/testlib.py
def func1():
return 'unexpected result'
test_mock.py
from lib import testlib
from mock import Mock
class TestCase(unittest.TestCase):
def test_mock(self):
testlib.func1 = Mock(return_value=('expected result')
test_patch.py
from lib import testlib
from mock import patch
class TestCase(unittest.TestCase):
def test_patch(self):
with patch('lib.testlib.func1') as p:
p.return_value = ('expected result')
In case test_mock.py, below commands will get different result
First is wrong, the function Mock don't change the function return value correctly. I also use pdb to check, only before debugger enter into main function, all the things will work well.
python test_mock.py
Second is correct
py.test test_mock.py
However in case test_patch.py, both commands python and py.test will get same and correct result. I finally select the patch solution.
I hope those clues are enough for you. I need someone explain the reason.
IMO, I guess the flow of py.test and python is different.
Aucun commentaire:
Enregistrer un commentaire