Python unittest单元测试框架

一、python unittest简介

unittest是python下的单元测试框架,是java JUnit的python版本, 跟其它语言下的单元测试框架风格类似,unittest支持自动化测试、共享setup和teardown代码、测试聚合成集、独立于报告框架。unittest模块提供了一个丰富的工具集用于构建和执行用例,先看一个入门的例子

import unittest

class TestStringMethods(unittest.TestCase):

    def test_upper(self):
        self.assertEqual('foo'.upper(), 'FOO')

    def test_isupper(self):
        self.assertTrue('FOO'.isupper())
        self.assertFalse('Foo'.isupper())

    def test_split(self):
        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()

可以通过继承unittest.TestCase创建一个测试用例TestStringMethods,在这个用例中定义了测试函数,这些函数名字都以”test”开头,在执行测试用例TestStringMethods时,这些方法会被自动调用。每个测试函数中都调用了assertTrue()和assertFalse()方法检查预期结果,或者使用assertRaises()确认产生了一个特定异常。现在来看一下这段代码的运行结果:
 

...
----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

有时我们需要在用例执行前后做一些工作如初始化和清理,这就需要实现setUp()和tearDown()方法

import unittest

class WidgetTestCase(unittest.TestCase):
    def setUp(self):
        print("setUp()")

    def test_1(self):
        print("test_1")

    def test_2(self):
        print("test_2")

    def tearDown(self):
        print("tearDown()")

if __name__ == '__main__':
    unittest.main()

运行结果:

setUp()
.test_1
tearDown()
setUp()
.test_2
tearDown()
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

注:如果setUp()执行成功(没有异常发生),那么无论测试方法是否通过,tearDown()都会被执行 
根据所测的特性测试用例被组合在一起,通过调用unittest.main(),unittest测试框架会自动收集所有模块的测试用例然后执行。

import unittest

class WidgetTestCase(unittest.TestCase):
    def setUp(self):
        print("WidgetTestCase setUp()")

    def test_Widget(self):
        print("test_Widget")

    def tearDown(self):
        print("WidgetTestCase tearDown()")

class FuncTestCase(unittest.TestCase):
    def setUp(self):
        print("FuncTestCase setUp()")

    def test_func(self):
        print("test_func")

    def tearDown(self):
        print("FuncTestCase tearDown()")


if __name__ == '__main__':
    unittest.main()

如果想构建自已的用例集,只需要这么做:

import unittest

class WidgetTestCase(unittest.TestCase):
    def setUp(self):
        print("WidgetTestCase setUp()")

    def test_Widget(self):
        print("test_Widget")

    def tearDown(self):
        print("WidgetTestCase tearDown()")

class FuncTestCase(unittest.TestCase):
    def setUp(self):
        print("FuncTestCase setUp()")

    def test_func(self):
        print("test_func")

    def tearDown(self):
        print("FuncTestCase tearDown()")

def suite():
    suite = unittest.TestSuite()
    suite.addTest(FuncTestCase('test_func'))
    return suite

if __name__ == '__main__':
    runner=unittest.TextTestRunner()
    runner.run(suite())

二、unittest中相关类和函数

在unittest中 TestCase类的实例代表逻辑测试单元,这个类通常被当作测试类的基类使用, TestCase类实现了许多测试相关的接口,主要是以下三组方法:

setUp()
#在每个测试方法之前执行,这个方法引发的异常会被认为是错误,而非测试失败,默认实现是不做任何事
tearDown()
#在每个测试方法之后执行,即使测试方法抛出异常tearDown()方法仍会执行,并且只有setUp()成功执行时tearDown()才会执行,
#同样这个方法引发的异常会被认为是错误,而非测试失败。默认实现是不做任何事
setUpClass()
#在一个测试类的所有测试方法执行之前执行,相当于google test中的SetUpTestCase()方法,setUpClass()必须被装饰成一个classmethod()
@classmethod
def setUpClass(cls):
    ...
tearDownClass()
#在一个测试类的所有测试方法执行之后执行,相当于google test中的TearDownTestCase()方法,tearDownClass()必须被装饰成一个classmethod()
@classmethod
def tearDownClass(cls):
    ...

猜你喜欢

转载自blog.csdn.net/weixin_41440777/article/details/85043207