Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 随机不使用模拟鼻测试_Python_Mocking_Nose_Monkeypatching - Fatal编程技术网

Python 随机不使用模拟鼻测试

Python 随机不使用模拟鼻测试,python,mocking,nose,monkeypatching,Python,Mocking,Nose,Monkeypatching,我在一个简单的游戏中练习单元测试和鼻子测试,游戏中有一部分我需要测试带有random.randint的骰子。我一直在关注这篇关于如何使用mock测试随机事件的文章 当我运行测试时,我得到了这个错误 ERROR: tests.ex47_tests.test_dice_roll ---------------------------------------------------------------------- Traceback (most recent call last): Fil

我在一个简单的游戏中练习单元测试和鼻子测试,游戏中有一部分我需要测试带有random.randint的骰子。我一直在关注这篇关于如何使用mock测试随机事件的文章

当我运行测试时,我得到了这个错误

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

根据您在
Intro
类中调用
rand\u int
的方式,您可能是通过以下方式导入的:

from random import randint
在这种情况下,您的修补程序应如下所示:

@mock.patch('ex47.game.randint')
您正在修补的方式:

@mock.patch('ex47.game.random.randint')
表示您正在按如下方式导入:

import random
此外,我在测试代码中看到了一些问题

  • 您正在测试中导入随机项。你不应该这样做。您正在正确地进行修补(针对您正在测试的内容进行修补),因此您不需要导入random,因为您将针对您的
    游戏
    模块正确地修补您的random方法

  • 似乎您试图调用您的方法,而没有实际实例化您的类。如果尚未设置,则应将测试设置为:

    class ThisIsMyTest(unittest.TestCase):
    def设置(自):
    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()
    

    能否在
    game.py
    中显示
    random
    的导入语句?
    import random
    
    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()