《笨辦法學Python》筆記34-----自動化測試

自動化測試

計算機的工作就是為了提高人的效率,所以無聊、冗繁、重復的工作盡量讓它去自動化執(zhí)行,比如代碼測試。

復制上節(jié)中的骨架目錄,將項目名字改為ex47,并用EX47替代所有NAME。

終端進入項目根目錄,運行nosetests命令,如果出現(xiàn)以下提示,說明ex47項目骨架建立成功。

$ nosetests
.
~----------------------------------------------------------------------
Ran 1 test in 0.002s

OK

新增一個待測試的源碼文件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.003s

OK

說明測試功能正常。

assert_equal函數(shù)

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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 第一部分 準入訓練 第1章 進入忍者世界 js開發(fā)人員通常使用js庫來實現(xiàn)通用和可重用的功能。這些庫需要簡單易用,...
    如201608閱讀 1,407評論 1 2
  • Python 四五事 介紹 Python 相關工具,工作流程和測試框架。 發(fā)布于 2014.1.19最后更新 20...
    hzyido閱讀 65,115評論 0 4
  • shareSDK的網(wǎng)址 1.在經(jīng)過將近一周時間的開發(fā),終于搞定ios分享了。 2.由于原來使用的是友盟的分享,因此...
    扶光啟玄閱讀 10,758評論 19 21
  • 最近各位小主們都被太子夜華刷屏了,帥,有錢,有權,溫柔,霸道,專一,會做飯,會帶娃,疼老婆。 看了幾集后,也被劇中...
    許多多的后花園閱讀 258評論 0 0

友情鏈接更多精彩內容