自動化測試
計算機的工作就是為了提高人的效率,所以無聊、冗繁、重復的工作盡量讓它去自動化執(zhí)行,比如代碼測試。
復制上節(jié)中的骨架目錄,將項目名字改為ex47,并用EX47替代所有NAME。
終端進入項目根目錄,運行nosetests命令,如果出現(xiàn)以下提示,說明ex47項目骨架建立成功。
$ nosetests
.
~----------------------------------------------------------------------
Ran 1 test in 0.002sOK
新增一個待測試的源碼文件game.py到EX47文件夾
class Room(object):
def __init__(self, name, description):
self.name = name
self.description = description
self.paths = {}
def go(self, direction):
return self.paths.get(direction, None)
def add_paths(self, paths):
self.paths.update(paths)
字典的get和update函數(shù)
get(key[, default])
Return the value for key if key is in the dictionary, else default. If default is not given, it defaults to None, so that this method never raises a KeyError.
通過鍵取得值
update([other])
Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.
update() accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs: d.update(red=1, blue=2).
使用鍵值對為字典添加或更新元素
編寫測試用例(test case)
編寫測試用例的前提是深刻理解模塊的功能邏輯。
from nose.tools import *
from EX47.game import Room
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)
若出現(xiàn)提示
$ nosetests
.
~----------------------------------------------------------------------
Ran 3 test in 0.003sOK
說明測試功能正常。
assertEqual(first, second, msg=None)
Test that first and second are equal. If the values do not compare equal, the test will fail.
In addition, if first and second are the exact same type and one of list, tuple, dict, set, frozenset or unicode or any type that a subclass registers with addTypeEqualityFunc() the type-specific equality function will be called in order to generate a more useful default error message (see also the list of type-specific methods).
Changed in version 2.7: Added the automatic calling of type-specific equality function.
assert_equal函數(shù)的功能就是比較第一個參數(shù)和第二個參數(shù)是否相等。
| Method | Checks that | New in |
|---|---|---|
| assertEqual(a, b) | a == b | |
| assertNotEqual(a, b) | a != b | |
| assertTrue(x) | bool(x) is True | |
| assertFalse(x) | bool(x) is False | |
| assertIs(a, b) | a is b | 2.7 |
| assertIsNot(a, b) | a is not b | 2.7 |
| assertIsNone(x) | x is None | 2.7 |
| assertIsNotNone(x) | x is not None | 2.7 |
| assertIn(a, b) | a in b | 2.7 |
| assertNotIn(a, b) | a not in b | 2.7 |
| assertIsInstance(a, b) | isinstance(a, b) | 2.7 |
| assertNotIsInstance(a, b) | not isinstance(a, b) | 2.7 |