相关疑难解决方法(0)

为什么python模拟补丁不起作用?

我有两个文件

spike.py

class T1(object):
    def foo(self, afd):
        return "foo"

    def get_foo(self):
        return self.foo(1)


def bar():
    return "bar"
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

当我运行时python test_spike.py,第一个测试用例会通过,但第二个测试用例会失败.然后我切换到使用nosetests test_spike.py,然后两个都失败了.

我不明白这是怎么发生的?这些案件应该通过所有.

python unit-testing mocking nose

20
推荐指数
3
解决办法
3万
查看次数

标签 统计

mocking ×1

nose ×1

python ×1

unit-testing ×1