我有两个文件
spike.py
class T1(object):
def foo(self, afd):
return "foo"
def get_foo(self):
return self.foo(1)
def bar():
return "bar"
test_spike.py:
from unittest import TestCase
import unittest
from mock import patch, MagicMock
from spike import T1, bar
class TestStuff(TestCase):
@patch('spike.T1.foo', MagicMock(return_value='patched'))
def test_foo(self):
foo = T1().get_foo()
self.assertEqual('patched', foo)
@patch('spike.bar')
def test_bar(self, mock_obj):
mock_obj.return_value = 'patched'
bar = bar()
self.assertEqual('patched', bar)
if __name__ == "__main__":
unittest.main()
当我运行python test_spike.py
时,第一个测试用例会通过,但第二个测试用例会失败。
然后我切换到使用nosetests test_spike.py
,然后两个都失败了。
我不明白这是怎么发生的?这些案件应该通过所有案件。
答案 0 :(得分:14)
使用bar
访问spike.bar
。导入的bar
不受mock.patch
的影响。
from unittest import TestCase
import unittest
from mock import patch, MagicMock
from spike import T1
import spike # <----
class TestShit(TestCase):
@patch('spike.T1.foo', MagicMock(return_value='patched'))
def test_foo(self):
foo = T1().get_foo()
self.assertEqual('patched', foo)
@patch('spike.bar')
def test_bar(self, mock_obj):
mock_obj.return_value = 'patched'
bar = spike.bar() # <-----
self.assertEqual('patched', bar)
if __name__ == "__main__":
unittest.main()
答案 1 :(得分:11)
对于 test_foo ,您没有正确使用补丁。你应该像这样使用它:
class TestFoo(TestCase):
@patch.object(T1, 'foo', MagicMock(return_value='patched'))
def test_foo(self):
foo = T1().get_foo()
self.assertEqual('patched', foo)
给了我:
nosetests test_spike.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
现在第二个例子不起作用,因为你导入了bar函数(得到它的引用),然后尝试模拟它。当你嘲笑某些东西时,你无法改变变量所持有的东西(参考原始函数)。要解决此问题,您应该使用@falsetru建议的方法,如:
from unittest import TestCase
import unittest
from mock import patch
import spike
class TestFoo(TestCase):
@patch('spike.bar')
def test_bar(self, mock_obj):
mock_obj.return_value = 'patched'
value = spike.bar()
self.assertEqual('patched', value)
if __name__ == "__main__":
unittest.main()
这给了我:
python test_spike.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
但是当我试着用鼻子跑它时,我得到了:
nosetests test_spike.py
F
======================================================================
FAIL: test_bar (src.test_spike.TestFoo)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/zilva/envs/test/local/lib/python2.7/site-packages/mock/mock.py", line 1305, in patched
return func(*args, **keywargs)
File "/home/zilva/git/test/src/test_spike.py", line 11, in test_bar
self.assertEqual('patched', value)
AssertionError: 'patched' != 'bar'
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
这种情况发生的原因是我在正确的位置打补丁。我的目录结构是:
test/
└── src/
├── spike.py
├── test_spike.py
└── __init__.py
我从src目录运行测试,所以我应该使用项目根目录中的路径进行修补,如:
@patch('src.spike.bar')
这会给我:
nosetests test_spike.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
或者如果我在测试目录中:
nosetests src/test_spike.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK