试图编写一个简单的python冒险游戏的单元测试

时间:2019-02-11 15:38:25

标签: python

我是python的新手,我只需要一些有关单元测试的帮助。这是我要测试的代码

rooms = {
'Walls': {
    'name': 'an empty room',
    'east': 'bedroom',
    'north': 'temple',
    'text': 'The stone floors and walls and cold and damp.'
},
'temple': {
    'name': 'a small temple',
    'east': 'torture',
    'south': 'empty',
    'text': 'There are three rows of benches facing a small statue.'
},
'torture': {
    'name':
    'a torture chamber',
    'west':
    'temple',
    'south':
    'bedroom',
    'text':
    'There is a rack and an iron maiden against the wall\nand some chains and thumbscrews on the floor.'
},
'bedroom': {
    'name': 'a bedroom',
    'north': 'torture',
    'west': 'empty',
    'text': 'There is a large bed with black, silk sheets on it.'
}
}

directions = ['north', 'south', 'east', 'west']
current_room = rooms['Walls']

# game loop

while True:
    # display current location
    print()
    print(directions)
    print()
    print('You are in {}.'.format(current_room['name']))
    print(current_room['text'])
    # get user input
    command = input('\nWhat do you do? ').strip()
    # movement
    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]
        else:
            # bad movement
            print("You can’t move further in this direction.")
    # quit game
    elif command.lower() in ('q', 'quit'):
        break
    # bad command
    else:
        print("Invalid command")

1 个答案:

答案 0 :(得分:0)

使用功能将代码包装起来。 (我们称其为“ adv_game()”)。 游戏/功能不应打印任何内容,应返回状态代码。 示例:

  1. 状态1 ==>您可以朝这个方向移动
  2. 状态2 ==>无效命令

您将拥有另一个代码,该代码将使用“ rooms”数据结构和当前用户房间调用此“ adv_game()”函数。调用者代码将根据adv_game()的返回值与用户进行交互。
UnitTest将使用输入的组合来提供“ adv_game()”,并将结果与​​期望值相匹配