Python模拟不返回函数的模拟值

时间:2018-02-02 10:27:53

标签: python unit-testing mocking

我正在尝试使用模拟功能编写单元测试用例来模拟函数。

但该函数返回实际值而不是模拟值。

这是我的示例代码,

def k_urandom(length):
    return 'k' * length

def abck_urandom(length):
    return 'abc' + k_urandom(length)

class TestRandom(unittest.TestCase):

    @mock.patch('demo.k_urandom', return_value='pump')
    def test_urandom(self, k_urandom_function):
        print(abck_urandom(5))
        assert abck_urandom(5) == 'abcpump'

在打印函数时,我输出为abckkkkk,但我需要的是模拟输出abcpump

我正在使用python 3.6。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我错过了包裹名称,

@mock.patch('unit_test_example.demo.k_urandom', return_value='pump')

现在它正在运作。

相关问题