為了確認(rèn)游戲的功能是否正常,你需要一遍一遍地在你的游戲中輸入命令。這個(gè)過程是很枯燥無味的。如果能寫一小段代碼用來測試你的代碼豈不是更好?然后只要你對程序做了任何修改,或者添加了什么新東西,你只要“跑一下你的測試”,而這些測試能確認(rèn)程序依然能正確運(yùn)行。這些自動測試不會抓到所有的 bug,但可以讓你無需重復(fù)輸入命令運(yùn)行你的代碼,從而為你節(jié)約很多時(shí)間。
從這一章開始,以后的練習(xí)將不會有“你應(yīng)該看到的結(jié)果”這一節(jié),取而代之的是一個(gè)“你應(yīng)該測試的東西”一節(jié)。從現(xiàn)在開始,你需要為自己寫的所有代碼寫自動化測試,而這將讓你成為一個(gè)更好的程序員。
我不會試圖解釋為什么你需要寫自動化測試。我要告訴你的是,你想要成為一個(gè)程序員,而程序的作用是讓無聊冗繁的工作自動化,測試軟件毫無疑問是無聊冗繁的,所以你還是寫點(diǎn)代碼讓它為你測試的更好。
這應(yīng)該是你需要的所有的解釋了。因?yàn)槟銓憜卧獪y試的原因是讓你的大腦更加強(qiáng)健。你讀了這本書,寫了很多代碼讓它們實(shí)現(xiàn)一些事情?,F(xiàn)在你將更進(jìn)一步,寫出懂得你寫的其他代碼的代碼。這個(gè)寫代碼測試你寫的其他代碼的過程將強(qiáng)迫你清楚的理解你之前寫的代碼。這會讓你更清晰地了解你寫的代碼實(shí)現(xiàn)的功能及其原理,而且讓你對細(xì)節(jié)的注意更上一個(gè)臺階。
我們將拿一段非常簡單的代碼為例,寫一個(gè)簡單的測試,這個(gè)測試將建立在上節(jié)我們創(chuàng)建的項(xiàng)目骨架上面。
首先從你的項(xiàng)目骨架創(chuàng)建一個(gè)叫做 ex47 的項(xiàng)目。確認(rèn)該改名稱的地方都有改過,尤其是 tests/ex47_tests.py 這處不要寫錯(cuò),另外運(yùn)行 nosetest 確認(rèn)一下沒有錯(cuò)誤信息。檢查一下 tests/skel_tests.pyc 這個(gè)文件,有的話就把它刪掉,這一點(diǎn)需要尤其注意。
接下來創(chuàng)建一個(gè)簡單的 ex47/game.py 文件,里邊放一些用來被測試的代碼。我們現(xiàn)在放一個(gè)傻乎乎的小 class 進(jìn)去,用來作為我們的測試對象:
1 2 3 4 5 6 7 8 9 10 11 12 | 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)
|
準(zhǔn)備好了這個(gè)文件,接下來把測試骨架改成這樣子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | 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)
|
這個(gè)文件 import 了你在 ex47.game 創(chuàng)建的 Room 這個(gè)類,接下來我們要做的就是測試它。于是我們看到一系列的以 test_ 開頭的測試函數(shù),它們就是所謂的“測試用例(test case)”,每一個(gè)測試用例里面都有一小段代碼,它們會創(chuàng)建一個(gè)或者一些房間,然后去確認(rèn)房間的功能和你期望的是否一樣。它測試了基本的房間功能,然后測試了路徑,最后測試了整個(gè)地圖。
這里最重要的函數(shù)時(shí) assert_equal,它保證了你設(shè)置的變量,以及你在 Room 里設(shè)置的路徑和你的期望相符。如果你得到錯(cuò)誤的結(jié)果的話, nosetests 將會打印出一個(gè)錯(cuò)誤信息,這樣你就可以找到出錯(cuò)的地方并且修正過來。
在寫測試代碼時(shí),你可以照著下面這些不是很嚴(yán)格的指南來做:
~/projects/simplegame $ nosetests
...
----------------------------------------------------------------------
Ran 3 tests in 0.007s
OK
如果一切工作正常的話,你看到的結(jié)果應(yīng)該就是這樣。試著把代碼改錯(cuò)幾個(gè)地方,然后看錯(cuò)誤信息會是什么,再把代碼改正確。