Python单元测试框架unittest使用方法讲解

所属分类: 脚本专栏 / python 阅读数: 237
收藏 0 赞 0 分享

概述

1.测试脚手架(test fixture)

测试准备前要做的工作和测试执行完后要做的工作.包括setUp()和tearDown().

2.测试案例(test case)

最小的测试单元.

3.测试套件(test suite)

测试案例的集合.

4.测试运行器(test runner)

测试执行的组件.

命令行接口

可以用命令行运行测试模块,测试类以及测试方法.

复制代码 代码如下:

python -m unittest test_module1 test_module2
python -m unittest test_module.TestClass
python -m unittest test_module.TestClass.test_method

可加-v打印详细信息
复制代码 代码如下:

python -m unittest -v test_module

测试案例自动搜索

unittest支持简单的test discovery. 命令行传入discovery后,框架会自动在当前目录搜索要测试的案例并执行.搜索目录必须是包或者模块.基本使用如下:

复制代码 代码如下:

cd project_directory
python -m unittest discover

子选项如下:
-v, –verbose
输出信息的详细级别

-s, –start-directory directory
开始搜索目录 (默认为当前目录)

-p, –pattern pattern
匹配的文件名 (默认为test*.py)

-t, –top-level-directory directory
搜索的顶层目录 (默认为start directory)

创建测试代码

1.方式一

创建子类继承unittest.TestCase,然后重写以下方法

复制代码 代码如下:

class WidgetTestCase(unittest.TestCase):
    def setUp(self):
        pass
    def runTest(self):
        pass
    def tearDown(self):
        pass

运行

2.方式二

编写以test开头的方法

复制代码 代码如下:

class WidgetTestCase(unittest.TestCase):
    def setUp(self):
        pass

    def test_xx1(self)
    def test_xx2(self)
    ...
    def test_xxN(self)

    def tearDown(self):
        pass


构建测试套件

方式一

复制代码 代码如下:

widgetTestSuite = unittest.TestSuite()
widgetTestSuite.addTest(WidgetTestCase('test_default_size'))
widgetTestSuite.addTest(WidgetTestCase('test_resize'))

方式二(推荐)

复制代码 代码如下:

def suite():
    suite = unittest.TestSuite()
    suite.addTest(WidgetTestCase('test_default_size'))
    suite.addTest(WidgetTestCase('test_resize'))
    return suite

方式三(推荐)
复制代码 代码如下:

def suite():
    tests = ['test_default_size', 'test_resize']
    return unittest.TestSuite(map(WidgetTestCase, tests))

方式四

多个测试套件构建成更大的测试套件

复制代码 代码如下:

suite1 = module1.TheTestSuite()
suite2 = module2.TheTestSuite()
alltests = unittest.TestSuite([suite1, suite2])

方式五

unittest的TestLoader提供生成默认的测试套件

复制代码 代码如下:

suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)

忽略测试案例( Python2.7支持)

可以分无条件忽略和有条件忽略,通过装饰器实现

复制代码 代码如下:

class MyTestCase(unittest.TestCase):

    @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


测试类也可以忽略
复制代码 代码如下:

@unittest.skip("showing class skipping")
class MySkippedTestCase(unittest.TestCase):
    def test_not_run(self):
        pass

更多精彩内容其他人还在看

pandas的qcut()方法详解

这篇文章主要介绍了pandas的qcut()方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

从列表或字典创建Pandas的DataFrame对象的方法

这篇文章主要介绍了从列表或字典创建Pandas的DataFrame对象的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas.DataFrame的pivot()和unstack()实现行转列

这篇文章主要介绍了pandas.DataFrame的pivot()和unstack()实现行转列,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

pandas中的series数据类型详解

这篇文章主要介绍了pandas中的series数据类型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas 时间格式转换的实现

这篇文章主要介绍了pandas 时间格式转换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python中时间、日期、时间戳的转换的实现方法

这篇文章主要介绍了python中时间、日期、时间戳的转换的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

pandas进行时间数据的转换和计算时间差并提取年月日

这篇文章主要介绍了pandas进行时间数据的转换和计算时间差并提取年月日,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法

这篇文章主要介绍了详解将Pandas中的DataFrame类型转换成Numpy中array类型的三种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

python和c语言的主要区别总结

在本篇文章里小编给各位整理了关于python和c语言的主要区别的相关知识帖内容,有需要的朋友们学习阅读下。
收藏 0 赞 0 分享

选择Python写网络爬虫的优势和理由

在本篇文章里小编给各位整理了一篇关于选择Python写网络爬虫的优势和理由以及相关代码实例,有兴趣的朋友们阅读下吧。
收藏 0 赞 0 分享
查看更多