25 【python入门指南】如何编写测试代码

python如何编写测试代码

python内置了unittest,使得写应用层的单元测试变得超乎寻常的简单。

 

1,执行单个测试函数

#!/bin/python

import unittest

class TestMathFunc(unittest.TestCase):
    def test_add(self):
        self.assertEqual(3, 1+2)
        self.assertEqual(4, 2+2)
        self.assertNotEqual(3, 1+3)

    def runTest(self):
        self.test_add()

suite = unittest.TestSuite()
testCase = [TestMathFunc()]
suite.addTests(testCase)
runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite)

单元测试,只需要仿照上面的例子写就可以完成自己的测试代码。

 

其中包含几个部分:

a,测试类(要继承unittest.TestCase);

b,测试小组件,unittest.TestSuite,负责将测试类装入进来;

c,执行类,unittest.TextTestRunner;

 

2,执行多个测试函数

#!/bin/python

import unittest

class TestMathFunc(unittest.TestCase):
    def test_add(self):
        self.assertEqual(3, 1+2)
        self.assertEqual(4, 2+2)
        self.assertNotEqual(3, 1+3)

    def test_minus(self):
        self.assertEqual(-1, 1-2)

    def runTest(self):
        self.test_add()
        self.test_minus()

suite = unittest.TestSuite()
#testCase = [TestMathFunc('test_add'), TestMathFunc('test_minus')]
testCase = [TestMathFunc()]
suite.addTests(testCase)
runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite)

修改runTest函数,将测试函数包含进来即可。

 

3,执行多个测试类

#!/bin/python

import unittest

class TestStringFunc(unittest.TestCase):
    def test_str_concat(self):
        self.assertEqual("abcabc", "abc" + "abc")

    def runTest(self):
        self.test_str_concat()

class TestMathFunc(unittest.TestCase):
    def test_add(self):
        self.assertEqual(3, 1+2)
        self.assertEqual(4, 2+2)
        self.assertNotEqual(3, 1+3)

    def test_minus(self):
        self.assertEqual(-1, 1-2)

    def runTest(self):
        self.test_add()
        self.test_minus()

suite = unittest.TestSuite()
testCase = [TestMathFunc(), TestStringFunc()]
suite.addTests(testCase)
runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite)

在suite添加多个测试类,即可。

当然还有另外的方式:

...

suite = unittest.TestSuite()

testCase = [TestMathFunc()]
suite.addTests(testCase)

testCase2 = TestStringFunc()
suite.addTest(testCase2)

runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite)

 

通过以上代码片段,即可实现,自主地控制多个测试类,自主地控制多个函数执行测试,十分方便。

 

4,有时候我们不想每个测试函数都显示调用,而是加载所有test_开头的测试函数,怎么做?

构建两个文件:t3.py,main.py

main文件存放执行代码,t3文件仅存放测试类,不存放任何执行代码

main.py

#!/bin/python
#main.py
import unittest
from t3 import TestMathFunc, TestStringFunc

suite2 = unittest.TestLoader().discover('.', 't3.py')

runner = unittest.TextTestRunner(verbosity = 2)
runner.run(suite2)

 

 t3.py

#!/bin/python
#t3.py

import unittest

class TestStringFunc(unittest.TestCase):
    def test_str_concat(self):
        self.assertEqual("abcabc", "abc" + "abc")

#    def runTest(self):
#        self.test_str_concat()

class TestMathFunc(unittest.TestCase):
    def test_add(self):
        self.assertEqual(3, 1+2)
        self.assertEqual(4, 2+2)
        self.assertNotEqual(3, 1+3)

    def test_minus(self):
        self.assertEqual(-1, 1-2)

#    def runTest(self):
#        self.test_add()
#        self.test_minus()

 

执行main.py,输出结果:

test_add (t3.TestMathFunc) ... ok
test_minus (t3.TestMathFunc) ... ok
test_str_concat (t3.TestStringFunc) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.001s

 

通过 unittest.TestLoader().discover('.', 't3.py') 将t3文件中包含的所有测试类导入到suite中。

依赖这个功能,我们可以将所有的需要测试的文件,导入到main主函数中,进行集中管理和测试。注意:这里不再依赖runTest函数,只要函数名中包含test前缀就会被包含进来。

 

5,有时候测试用例需要提供上下文环境,怎么做?

我们针对例子4,仅修改t3.py测试类。

class TestMathFunc(unittest.TestCase):
    def setUp(self):
        print("\nTestMathFunc:setup")
        self.a = 1 
        self.b = 2 

    def test_add(self):
        self.assertEqual(3, 1+2)
        self.assertEqual(4, 2+2)
        self.assertNotEqual(3, 1+3)
        self.assertEqual(3, self.a + self.b)

    def test_minus(self):
        self.assertEqual(-1, 1-2)

 这里我们准备了环境信息(准备两个变量a和b),使用这两个变量进行单元测试。

test_add (t3.TestMathFunc) ... 
TestMathFunc:setup
ok
test_minus (t3.TestMathFunc) ... 
TestMathFunc:setup
ok
test_str_concat (t3.TestStringFunc) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.002s

OK

 

函数的输出如上。

类的单元测试,每个函数执行之前都会执行setUp函数。

对应的是tearDown,用来销毁构建的环境信息。可以自己尝试下。

 

 

参考网站:

更详细的解释:https://www.jianshu.com/p/38948d0d73f5

最详细的官方文档:https://docs.python.org/3/library/unittest.html#module-unittest

posted on 2018-11-15 11:39  awildfish  阅读(786)  评论(0编辑  收藏  举报

导航