本习题是习题46 的拓展,实际的测试了一个简单的类Room。
下面是本习题的目录框架:
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLwkERPpXV61UMNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL4AjM2UzNyETMzIDNwEjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
上面的.pyc后缀都是运行自动化测试之后,产生的文件。
setup.py:
try
from setuptools import setup
except ImportError:
from distutils.core import setup
config = {
'description' : 'My Project',
'author' : 'Gabe',
'author_email' : '[email protected]',
'version' : '0.1',
'install_requires' : ['nose'],
'packages' :['ex47'],
'scripts' : [],
'name' : 'black_hole'
}
setup(**config)
game.py:
class Room(object):
def __init__(self, name, description):
self.name = name
self.description = description
self.paths = {}#This is a dict
def go(self, direction):
return self.paths.get(direction,None)
def add_paths(self, paths):
self.paths.update(paths)
ex47_tests.py
from nose.tools import *
from game import Room #从顶层目录导入模块时候,不用加ex47
def test_room():
gold = Room("GoldRoom",
"""This room has gold in it you can grab. There's a
door to the north.""")
assert_equal(gold.name, "GoldRoom")
assert_equal(gold.paths, {})
def test_room_paths():
center = Room("Center", "Test room in the center.")
north = Room("North", "Test room in the north.")
south = Room("South","Test room in the south.")
center.add_paths({'north' : north, 'south' : south})
assert_equal(center.go('north'), north)
assert_equal(center.go('south'), south)
def test_map():
start = Room("Start", "You can go west and down a hole.")
west = Room("Trees", "There are trees here, you can go east.")
down = Room("Dungeon", "It's dark down here, you can go up.")
start.add_paths({'west' : west, 'down' : down})
west.add_paths({'east' : start})
down.add_paths({'up' : start})
assert_equal(start.go('west'), west)
assert_equal(start.go('west').go('east'),start)
assert_equal(start.go('down').go('up'),start)
最后的运行结果:
用到的py字典方面的语法:
dict.get(key, default = None)
#key值,字典中要查找的键,None是找不到键的时候,返回的默认的值。
dict.update(dict2)
#添加到指定字典dict里的字典dict2,该方法没有返回值。
断言:
总结:算是对自动化测试目录的一个简单应用。