概述
1.测试脚手架(test fixture)
测试准备前要做的工作和测试执行完后要做的工作.包括setUp()和tearDown().
2.测试案例(test case)
最小的测试单元.
3.测试套件(test suite)
测试案例的集合.
4.测试运行器(test runner)
测试执行的组件.
命令行接口
可以用命令行运行测试模块,测试类以及测试方法.
unittest支持简单的test discovery. 命令行传入discovery后,框架会自动在当前目录搜索要测试的案例并执行.搜索目录必须是包或者模块.基本使用如下:
-s,  start-directory directory 
开始搜索目录 (默认为当前目录)
-p,  pattern pattern 
匹配的文件名 (默认为test*.py)
-t,  top-level-directory directory 
搜索的顶层目录 (默认为start directory)
创建测试代码
1.方式一
创建子类继承unittest.TestCase,然后重写以下方法
运行
2.方式二
编写以test开头的方法
    def test_xx1(self)
    def test_xx2(self)
    ...
    def test_xxN(self)
    def tearDown(self):
        pass
方式一
方式二(推荐)
多个测试套件构建成更大的测试套件
unittest的TestLoader提供生成默认的测试套件
忽略测试案例( Python2.7支持)
可以分无条件忽略和有条件忽略,通过装饰器实现
    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")
    @unittest.skipIf(mylib.__version__ < (1, 3),
                     "not supported in this library version")
    def test_format(self):
        # Tests that work for only a certain version of the library.
        pass
    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        # windows specific testing code
        pass
新闻热点
疑难解答
图片精选