Python unittest模块心得(一)

关于unittest模块的一些心得,主要是看官网的例子,加上一点自己的理解,官网地址:https://docs.python.org/3.6/library/unittest.html

基础概念介绍:

unittest模块是Python的单元测试框架,支持自动化测试,所有用例共享setUp和tearDown代码,可以将测试用例聚合成测试集合,测试用例与报告框架独立。

为了实现这些功能,unittest支持下面几个面向对象式的概念:

  1. test fixture:代表了执行一个或多个测试用例所需要的准备工作,以及所有相关的清除工作。比如创建临时的或者代理的数据库,文件夹或者启动服务器。
  2. test case: 代表了一个单独的单元测试用例,会检查输入参数对应的反馈。unittest提供了一个基类TestCase用来创建测试用例。
  3. test suite: 代表了测试用例及测试套件的集合,用来将测试用例聚合到一起去执行。
  4. test runner: 用来执行测试用例并将测试结果反馈给用户。可以用图形接口,文字接口或者返回一些指定值来指示测试结果。
  5. test report: 可以使用unittest自带的TextTestRunner(),也可以使用HTMLTestRunner()产生HTML格式的测试报告,现在用BSTestRunner()代替HTMLTestRunner()

这些概念间的关系见下图:

下面给一些例子和说明:

import unittest

###编写的测试类要继承unittest.TestCase,类名称要以test开头,后面讲原因。
class TestStringMethods(unittest.TestCase):

    ###所有用例共用的setup,在用例执行前先执行,用来搭建环境。
    def setUp(self):
        print('case setup')

    ###所有用例共用的tearDown,在用例执行结束后执行,用来清理环境。setUp和tearDown保证了每个test case都是独立的,不依赖与其他case.
    def tearDown(self):
        print('case teardown
')

    def test_upper(self):###测试用例1,需要以test开头,原因后面讲。
        print('case test_upper')
        self.assertEqual('foo'.upper(), 'FOO')###assertEqual(a,b)如果a==b则用例pass,否则fail.

    def test_isupper(self):###测试用例2,需要以test开头。
        print('case test_isupper')
        self.assertTrue('FOO'.isupper())###类似的assert用法
        self.assertFalse('Foo'.isupper())

    def test_split(self):###测试用例3
        print('case test_split')
        s = 'hello world'
        self.assertEqual(s.split(), ['hello', 'world'])
        # check that s.split fails when the separator is not a string
        with self.assertRaises(TypeError):
            s.split(2)

if __name__ == '__main__':
    unittest.main()###具体做了哪些工作后面会详细讲。

下面看一下case的运行结果:

case setup
case test_isupper
case teardown

case setup
case test_split
case teardown

case setup
case test_upper
case teardown

###三个case全部pass,因为assert里面的结果都正确。这里并没有体现出测试结果,只是将打印信息展示出来。我们可以看到针对每个test_的case都运行了一遍,每次都独立的调用了setUp和tearDown.测试case执行的顺序不是按照代码的顺序,而是按照case名称字母的顺序,这个是unittest.main()函数决定的。

命令行模式:

unittest模块可以通过命令行模式从模块,类,或者类的方法中运行测试用例。我使用的也不多,给出最基本的例子。

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

跳过某些测试用例:

class MyTestCase(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        print("shouldn't happen")

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        # windows specific testing code
        print('this is Windows')
        pass

类似的@unittest.skip()装饰器包括:

@unittest.skip(reason)
#直接跳过这条测试用例,需要给出跳过的理由

@unittest.skipIf(condition, reason)
#跳过这条用例,如果condition值为True

@unittest.skipUnless(condition, reason)

@unittest.expectedFailure

exception unittest.SkipTest(reason)

@unittest.skip()可以用来装饰类的方法,也可以用来装饰class,将这个class全部跳过.

原文地址:https://www.cnblogs.com/frost-hit/p/8295818.html