随机不在鼻子测试中使用模拟
Random not working with mock in nosetest
我在一个简单的游戏中练习单元测试和鼻子测试,我有一个游戏部分有 random.randint 的掷骰子,我需要测试它。我一直在关注这篇关于如何使用 mock
测试随机事件的文章
http://www.jjinux.com/2014/01/python-lightning-quick-introduction-to.html
我在 运行 鼻测
时收到此错误
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')
根据您在 Intro
class 中调用 rand_int
的方式,您可能正在以这种方式导入:
from random import randint
在这种情况下,您的补丁应该是这样的:
@mock.patch('ex47.game.randint')
您修补的方式:
@mock.patch('ex47.game.random.randint')
表示您是这样导入的:
import random
此外,我发现您的测试代码中存在一些问题。
您正在测试中导入随机数。您不需要这样做。您正在正确修补(针对您正在测试的内容进行修补),因此您不需要导入 random 因为您将根据您的 game
模块正确修补您的随机方法。
您似乎在尝试调用您的方法而不实际实例化您的 class。如果您还没有,您应该将测试设置为:
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()
我在一个简单的游戏中练习单元测试和鼻子测试,我有一个游戏部分有 random.randint 的掷骰子,我需要测试它。我一直在关注这篇关于如何使用 mock
测试随机事件的文章http://www.jjinux.com/2014/01/python-lightning-quick-introduction-to.html
我在 运行 鼻测
时收到此错误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')
根据您在 Intro
class 中调用 rand_int
的方式,您可能正在以这种方式导入:
from random import randint
在这种情况下,您的补丁应该是这样的:
@mock.patch('ex47.game.randint')
您修补的方式:
@mock.patch('ex47.game.random.randint')
表示您是这样导入的:
import random
此外,我发现您的测试代码中存在一些问题。
您正在测试中导入随机数。您不需要这样做。您正在正确修补(针对您正在测试的内容进行修补),因此您不需要导入 random 因为您将根据您的
game
模块正确修补您的随机方法。您似乎在尝试调用您的方法而不实际实例化您的 class。如果您还没有,您应该将测试设置为:
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()