【发布时间】:2016-10-08 08:16:33
【问题描述】:
我希望 choice 在我的单元测试中每次都返回相同的值 1000。以下代码不起作用。
import unittest
from random import choice
from mock import mock
def a():
return choice([1, 2, 3])
class mockobj(object):
@classmethod
def choice(cls, li):
return 1000
class testMock(unittest.TestCase):
def test1(self):
with mock.patch('random.choice', mockobj.choice):
self.assertEqual(a(), 1000)
报错信息如下:
Failure
Traceback (most recent call last):
File "test.py", line 15, in test1
self.assertEqual(a(), 1000)
AssertionError: 3 != 1000
我应该如何修改它以使其工作?我正在使用python2.7
【问题讨论】:
-
那是哪个python测试框架?我不知道“mockobj”。这段代码怎么不起作用 - 它有什么作用?
-
@DannyStaple 很抱歉给您带来不便。
mockobj只是我创建的一个类,我已将其添加到代码中,因此您可以运行它来重现问题。
标签: python unit-testing mocking