我从一个简单的游戏练习单元测试和鼻子测试,我有一个游戏部分,有一个我需要测试的random.randint骰子。我一直在关注如何使用mock
测试随机事件http://www.jjinux.com/2014/01/python-lightning-quick-introduction-to.html
我在运行nosetests时遇到此错误
ERROR: tests.ex47_tests.test_dice_roll
----------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/usr/local/lib/python2.7/site-packages/mock/mock.py", line 1297, in patched
arg = patching.__enter__()
File "/usr/local/lib/python2.7/site-packages/mock/mock.py", line 1353, in __enter__
self.target = self.getter()
File "/usr/local/lib/python2.7/site-packages/mock/mock.py", line 1520, in <lambda>
getter = lambda: _importer(target)
File "/usr/local/lib/python2.7/site-packages/mock/mock.py", line 1210, in _importer
thing = _dot_lookup(thing, comp, import_path)
File "/usr/local/lib/python2.7/site-packages/mock/mock.py", line 1199, in _dot_lookup
__import__(import_path)
ImportError: No module named random
我已经随机导入,所以不确定为什么它不起作用或者这与模拟有关吗?
以下是游戏文件和测试文件中的代码部分(我没有把所有代码放在这里,因为我认为不需要)
game.py
class Intro(Rooms):
def enter(self):
print "Intro room"
print "You see the Gothon, you have to fight him!"
def dice_roll_fight(self):
print 'DICE ROLL'
dice_roll = randint(1,6)
print "You rolled a ", dice_roll
if dice_roll <= 2:
return 'death'
elif dice_roll >= 3:
return 'starter'
else:
print "ERROR try again"
ex47_tests.py
from nose.tools import *
from ex47.game import Intro
from mock import mock
import random
@mock.patch('ex47.game.random.randint')
def test_dice_roll(randint_mock):
randint_mock.return_value = 1
assert_equal(game.dice_roll_fight(), 'death')
@mock.patch('ex47.game.random.randint')
def test_dice_roll(randint_mock):
randint_mock.return_value = 2
assert_equal(game.dice_roll_fight(), 'death')
@mock.patch('ex47.game.random.randint')
def test_dice_roll(randint_mock):
randint_mock.return_value = 3
assert_equal(game.dice_roll_fight(), 'starter')
答案 0 :(得分:3)
根据您在rand_int
课程中如何调用Intro
,您可能会以这种方式导入:
from random import randint
在这种情况下,您的修补应该是这样的:
@mock.patch('ex47.game.randint')
你打补丁的方式:
@mock.patch('ex47.game.random.randint')
表示您是这样导入的:
import random
此外,我在测试代码中看到了一些问题。
您在测试中随机导入。你不应该这样做。您正在修补(根据您正在测试的内容进行修补),因此您无需随机导入,因为您将根据game
模块正确修补随机方法。
看起来你试图在没有实际实例化你的类的情况下调用你的方法。如果您还没有,则应将测试设置为:
class ThisIsMyTest(unittest.TestCase):
def setUp(self):
self.game = Intro()
您的测试方法的名称都相同。这将最终只运行一个测试,您将不会收到您想要测试的其他结果。
考虑到上述三个提及,以下代码应足以帮助您进行单元测试:
import unittest
from nose.tools import assert_equal
from game import Intro
from mock import mock
class GameTest(unittest.TestCase):
def setUp(self):
self.game = Intro()
@mock.patch('game.randint')
def test_dice_roll_one(self, randint_mock):
randint_mock.return_value = 1
from nose.tools import assert_equal
assert_equal(self.game.dice_roll_fight(), 'death')
@mock.patch('game.randint')
def test_dice_roll_two(self, randint_mock):
randint_mock.return_value = 2
assert_equal(self.game.dice_roll_fight(), 'death')
@mock.patch('game.randint')
def test_dice_roll_three(self, randint_mock):
randint_mock.return_value = 3
assert_equal(self.game.dice_roll_fight(), 'starter')
if __name__ == '__main__':
unittest.main()