Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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_Unit Testing_Tdd - Fatal编程技术网

Python 硬时间调试属性错误:模块';游戏';没有属性';获取玩家姓名';

Python 硬时间调试属性错误:模块';游戏';没有属性';获取玩家姓名';,python,unit-testing,tdd,Python,Unit Testing,Tdd,我正在努力在测试驱动的基本骰子游戏中实现模拟对象。然而,当我运行我的测试(见下文)时,它显示“AttributeError”,我只是不明白为什么 这是我的单元测试(test\u game.py)实现: from unittest import TestCase, mock import game class GameTest(TestCase): def test_get_player_names(self): """Players can enter their

我正在努力在测试驱动的基本骰子游戏中实现模拟对象。然而,当我运行我的测试(见下文)时,它显示“AttributeError”,我只是不明白为什么

这是我的单元测试(
test\u game.py
)实现:

from unittest import TestCase, mock

import game

class GameTest(TestCase):

    def test_get_player_names(self):
        """Players can enter their names"""

        fake_input = mock.Mock(side_effect=['A', 'M', 'Z', ''])

        with mock.patch('builtins.input', fake_input):
            names = game.get_player_names()

        self.assertEqual(names, ['A', 'M', 'Z'])

    def test_get_player_names_stdout(self):
        """Check the prompts for player names"""

        with mock.patch('builtins.input', side_effect=['A', 'B', '']) as fake:
            game.get_player_names()

        fake.assert_has_calls([
            mock.call("Player 1's name: "),
            mock.call("Player 2's name: "),
            mock.call("Player 3's name: ")
        ])
这是我用Python编写的实际代码(
game.py
):

class Dice:

    def __init__(self, *players):
        self.players = players

    def get_players(self):
        """Return a tuple of all players"""
        return self.players

    def get_player_names():
        """Prompt for player names"""
        names = []

        while True:
            value = input("Player {}'s name: ".format(len(names) + 1))
            if not value:
                break

            names.append(value)

        return names
测试的错误显示(PowerShell):

PS C:\Users\Seun\desktop\dice>python-m unittest ---------------------------------------------------------------------- 在0.000秒内运行了0个测试 好啊 PS C:\Users\Seun\desktop\dice>python3-m单元测试 嗯。 ====================================================================== 错误:test\u get\u player\u name(test\u game.GameTest) 玩家可以输入他们的名字 ---------------------------------------------------------------------- 回溯(最近一次呼叫最后一次): 文件“C:\Users\Seun\desktop\dice\test\u game.py”,第19行,在test\u get\u player\u name中 名称=游戏。获取玩家名称() AttributeError:模块“游戏”没有属性“获取玩家姓名” ====================================================================== 错误:test\u get\u player\u names\u stdout(test\u game.GameTest) 检查提示中的球员姓名 ---------------------------------------------------------------------- 回溯(最近一次呼叫最后一次): 文件“C:\Users\Seun\desktop\dice\test\u game.py”,第27行,在test\u get\u player\u names\u stdout中 游戏。获取玩家姓名() AttributeError:模块“游戏”没有属性“获取玩家姓名” ---------------------------------------------------------------------- 在0.009秒内运行了3次测试
由于
game而失败(错误=2)。在测试类中获取玩家姓名
您指向名为“game”模块中名为“获取玩家姓名”的方法

但是
game.py
公开了一个
Dice
类,该类公开了
get\u player\u name
。因此,您必须导入模块,实例化一个骰子实例,然后从该实例调用
get\u player\u names

from game import Dice
class GameTest(TestCase):
    # [ ... your code ...]
    game = Dice('player1', 'player2')
    game.get_player_names() # <== works.
从游戏导入骰子
类GameTest(测试用例):
#[…您的代码…]
游戏=骰子(‘玩家1’、‘玩家2’)

game.get_player_names()#通过
game.get_player_names
在测试类中,您指向名为“game”模块中名为“get_player_names”的方法

但是
game.py
公开了一个
Dice
类,该类公开了
get\u player\u name
。因此,您必须导入模块,实例化一个骰子实例,然后从该实例调用
get\u player\u names

from game import Dice
class GameTest(TestCase):
    # [ ... your code ...]
    game = Dice('player1', 'player2')
    game.get_player_names() # <== works.
从游戏导入骰子
类GameTest(测试用例):
#[…您的代码…]
游戏=骰子(‘玩家1’、‘玩家2’)

game.get_player_names()#您的测试正在导入名为“game”的
模块。
在这个模块中,您有一个
(“骰子”),带有“get\u player\u names”
方法

从错误消息中,您试图从错误的位置访问if。 您需要首先实例化Dice类以访问其方法

例如,在GameTest中,您可以:

def test_get_player_names(self):
    """Players can enter their names"""

    fake_input = mock.Mock(side_effect=['A', 'M', 'Z', ''])

    # Create a Dice Instance
    dice_game = game.Dice()

    with mock.patch('builtins.input', fake_input):
        names = dice_game.get_player_names()  # Reference to the Dice Instance and not the module

    self.assertEqual(names, ['A', 'M', 'Z'])

您的测试正在导入名为“游戏”的
模块。
在这个模块中,您有一个
(“骰子”),带有“get\u player\u names”
方法

从错误消息中,您试图从错误的位置访问if。 您需要首先实例化Dice类以访问其方法

例如,在GameTest中,您可以:

def test_get_player_names(self):
    """Players can enter their names"""

    fake_input = mock.Mock(side_effect=['A', 'M', 'Z', ''])

    # Create a Dice Instance
    dice_game = game.Dice()

    with mock.patch('builtins.input', fake_input):
        names = dice_game.get_player_names()  # Reference to the Dice Instance and not the module

    self.assertEqual(names, ['A', 'M', 'Z'])

游戏在哪里初始化,您导入游戏但代码在哪里?这是第二个代码集-game.py游戏在哪里初始化,您导入游戏但代码在哪里?这是第二个代码集-game.pyI尝试了此操作,但抛出了一个类型错误:get_player_names()接受0个位置参数,但给出了1个位置参数当然。。。我没有注意到,您在该定义上缺少了一个
self
参数(您可能希望这样做,以便您也可以在实例中保存输入)。我尝试了这个方法,但它抛出了一个类型错误:get_player_names()接受0个位置参数,但1被指定了,当然。。。我没有注意到这一点,您在该定义上缺少了一个
self
参数(您可能希望这样做,以便也可以将输入保存在实例中)。