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

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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,120评论 19 139
  • 第一部分 准入训练 第1章 进入忍者世界 js开发人员通常使用js库来实现通用和可重用的功能。这些库需要简单易用,...
    如201608阅读 5,163评论 1 2
  • Python 四五事 介绍 Python 相关工具,工作流程和测试框架。 发布于 2014.1.19最后更新 20...
    hzyido阅读 65,088评论 0 4
  • shareSDK的网址 1.在经过将近一周时间的开发,终于搞定ios分享了。 2.由于原来使用的是友盟的分享,因此...
    阳光的大男孩儿阅读 13,627评论 19 21
  • 最近各位小主们都被太子夜华刷屏了,帅,有钱,有权,温柔,霸道,专一,会做饭,会带娃,疼老婆。 看了几集后,也被剧中...
    许多多的后花园阅读 1,675评论 0 0

友情链接更多精彩内容