《笨办法学Python》笔记34-----自动化测试

自动化测试

计算机的工作就是为了提高人的效率,所以无聊、冗繁、重复的工作尽量让它去自动化执行,比如代码测试。

复制上节中的骨架目录,将项目名字改为ex47,并用EX47替代所有NAME。

终端进入项目根目录,运行nosetests命令,如果出现以下提示,说明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函数

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)

若出现提示

$ nosetests
.
~———————————————————————-
Ran 3 test in 0.003s

OK

说明测试功能正常。

assert_equal函数

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函数的功能就是比较第一个参数和第二个参数是否相等。

MethodChecks thatNew 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 b2.7
assertIsNot(a, b)a is not b2.7
assertIsNone(x)x is None2.7
assertIsNotNone(x)x is not None2.7
assertIn(a, b)a in b2.7
assertNotIn(a, b)a not in b2.7
assertIsInstance(a, b)isinstance(a, b)2.7
assertNotIsInstance(a, b)not isinstance(a, b)2.7
    原文作者:大猫黄
    原文地址: https://www.jianshu.com/p/03f95ecec4df
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞