python中的单元测试可以使用doctest,unittest完成
1.doctest的使用
(1)写入程序如下(cubetest.py):
#!/usr/bin/python def cube(x): """ cube a number and return the result >>> cube(2) 8 >>> cube(3) 27 >>> cube(4) 64 """ return x**3; if __name__=='__main__': import doctest; doctest.testmod(verbose=True); |
测试用例写在字符串””” cube a number… “””中。
调用doctest中的testmod的方法即可测试。
输出如下
Trying: cube(2) Expecting: 8 ok Trying: cube(3) Expecting: 27 ok Trying: cube(4) Expecting: 64 ok 1 itemshad no tests: __main__ 1 itemspassed all tests: 3 tests in __main__.cube 3 testsin 2 items. 3 passedand 0 failed. Testpassed. TestResults(failed=0, attempted=3) |
(2)也可以把测试用例写在记事本文件中。新建记事本example.txt,写入以下内容:
>>> from cubetest import cube >>> cube(1) 1 >>> cube(2) 8 >>> cube(4) 64 |
然后写测试代码 test.py:
import
doctest
doctest.testfile("example.txt")
|
使用python test.py –v 执行测试代码,可以得到以下结果
Trying: from cubetest import cube Expectingnothing ok Trying: cube(1) Expecting: 1 ok Trying: cube(2) Expecting: 8 ok Trying: cube(4) Expecting: 64 ok 1 items passedall tests: 4 tests in example.txt 4 tests in 1items. 4 passed and 0failed. Test passed. |
2.unittest的使用
(1)写待测试的程序如下:
def iseven(x): if(x%2==0): return 1; else: return 0; def isodd(x): if(x%2==1): return 1; else: return 0; |
测试程序中的类eventest类继承自TestCase,它包含setUp() ,test_1(),test_2()和tearDown()四个方法。在测试运行前,会自动运行setUp()方法,在测试完成之前,会自动调用tearDown()方法。test_1()和test_2()是自定义的测试方法。写测试程序如下:
import unittest,evenodd class eventest(unittest.TestCase): def setUp(self): self.ex1=range(0,2,20); self.ex2=range(1,2,20); def test_1(self): for x in self.ex1: x=evenodd.iseven(x); self.assertEqual(x,1); for x in self.ex2: x=evenodd.iseven(x); self.assertEqual(x,0); def test_2(self): for x in self.ex1: x=evenodd.isodd(x); self.assertEqual(x,0); for x in self.ex2: x=evenodd.isodd(x); self.assertEqual(x,1); def tearDown(self): self.ex1=None; self.ex2=None; if __name__=='__main__': unittest.main(); 上面用到了self.AssertEqual(),类似的判断方法还有: assertNotEqual(a,b) a!=b asserTrue(x) bool(x)为真 assertFalse(x) bool(x)为假 assertIs(a,b) a is b assertIsNot(a,b) a is not b assertIsNone(x) x is None assertIsNotNone(x) x is not None assertIn (a,b) a in b assertNotIn(a,b) a not in b assertIsInstance(a,b) isinstance(a,b) assertNotIsInstance(a,b) not instance(a,b) |
(2) 也可以使用TestSuite对测试用例进行封装,下面的程序定义了类evensuite,它继承自类TestSuite,方法addTest()用于添加测试方法。
测试程序如下:
import unittest,evenodd class eventest(unittest.TestCase): def setUp(self): self.ex1=range(0,2,20); self.ex2=range(1,2,20); def test_1(self): for x in self.ex1: x=evenodd.iseven(x); self.assertEqual(x,1); for x in self.ex2: x=evenodd.iseven(x); self.assertEqual(x,0); def test_2(self): for x in self.ex1: x=evenodd.isodd(x); self.assertEqual(x,0); for x in self.ex2: x=evenodd.isodd(x); self.assertEqual(x,1); def tearDown(self): self.ex1=None; self.ex2=None; class evensuite(unittest.TestSuite): def suite(self): suite=unittest.TestSuite(); suite.addTest(eventest('test_1')); suite.addTest(eventest('test_2')); return suite; if __name__=='__main__': unittest.main(); |
运行程序 ,结果输出:
_ _ ran 2 tests in 0.001s OK |
|