python基础教程学习笔记十六

这篇博客介绍了Python的unittest框架,展示了如何编写和运行测试用例,以及如何处理测试结果。此外,还讲解了doctest模块,通过检查文档字符串来验证函数的正确性,并给出了使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

测试

测试工具:

Unitest 通用测试框架

示例代码如下:

#unittest框架的简单测试

import unittest,my_math

 

class PrductTestCase(unittest.TestCase):

 

    def testIntegers(self):

        for x in range(-10,10):

            for y in range(-10,10):

                p=my_math.product(x,y)

                self.assertTrue(p==x*y,'Integer multiplication failed')

 

    def testFloats(self):

        for x in range(-10,10):

               for y in range(-10,10):

                   x=x/10.0

                   y=y/10.0

                   p=my_math.product(x,y)

                   self.assertTrue(p==x*y,'Float multiplication failed')

 

if __name__=='__main__':

    unittest.main()

 

测试正确结果如下:

..

----------------------------------------------------------------------

Ran 2 tests in 0.004s

 

OK

Traceback (most recent call last):

  File "D:/workspace_python/unittest/unit_test.py", line 21, in <module>

    unittest.main()

  File "D:\Python32\lib\unittest\main.py", line 124, in __init__

    self.runTests()

  File "D:\Python32\lib\unittest\main.py", line 272, in runTests

    sys.exit(not self.result.wasSuccessful())

SystemExit: False

 

测试错误结果如下:

FF

======================================================================

FAIL: testFloats (__main__.PrductTestCase)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "D:/workspace_python/unittest/unit_test.py", line 18, in testFloats

    self.assertTrue(p==x*y,'Float multiplication failed')

AssertionError: False is not true : Float multiplication failed

 

======================================================================

FAIL: testIntegers (__main__.PrductTestCase)

----------------------------------------------------------------------

Traceback (most recent call last):

  File "D:/workspace_python/unittest/unit_test.py", line 10, in testIntegers

    self.assertTrue(p==x*y,'Integer multiplication failed')

AssertionError: False is not true : Integer multiplication failed

 

----------------------------------------------------------------------

Ran 2 tests in 0.007s

 

FAILED (failures=2)

Traceback (most recent call last):

  File "D:/workspace_python/unittest/unit_test.py", line 21, in <module>

    unittest.main()

  File "D:\Python32\lib\unittest\main.py", line 124, in __init__

    self.runTests()

  File "D:\Python32\lib\unittest\main.py", line 272, in runTests

    sys.exit(not self.result.wasSuccessful())

SystemExit: True

>>>

 

常用的testCase方法

Assert_() 表示假则为失败

failUnless()

assertEqual()

failUnlessEqual()

assertNotEqual()

failIfEqual()

assertAlmostEqual(0

 

 

单元测试以外的内容

使用pyCheckerPyLint检查源代码

分析

 

 

Doctest 检查文档

示例代码如下:

#如果要测试my_python.py,可以在最后添加

#求数字的平方

def square(x):

    '''

    Aquare a number and returns the result.

    >>> square(2)

    4

    >>> square(3)

    9

    '''

    return x*x

if __name__=='__main__':

    import doctest,my_math

    doctest.testmod(my_math)

在命令行运行时可以设定脚本

Python my_python -v

 

显示结果如下:

D:\workspace_python\unittest>ls

__pycache__  my_math.py  unit_test.py

 

D:\workspace_python\unittest>python my_math.py -v

Trying:

    square(2)

Expecting:

    4

ok

Trying:

    square(3)

Expecting:

    9

ok

1 items had no tests:

    my_math

1 items passed all tests:

   2 tests in my_math.square

2 tests in 2 items.

2 passed and 0 failed.

Test passed.

 

如果在文档注释中少写了一个2

运行时显示结果如下:

Trying:

    square()

Expecting:

    4

**********************************************************************

File "D:\workspace_python\unittest\my_math.py", line 5, in my_math.square

Failed example:

    square()

Exception raised:

    Traceback (most recent call last):

      File "D:\Python32\lib\doctest.py", line 1288, in __run

        compileflags, 1), test.globs)

      File "<doctest my_math.square[0]>", line 1, in <module>

        square()

    TypeError: square() takes exactly 1 argument (0 given)

Trying:

    square(3)

Expecting:

    9

ok

1 items had no tests:

    my_math

**********************************************************************

1 items had failures:

   1 of   2 in my_math.square

2 tests in 2 items.

1 passed and 1 failed.

***Test Failed*** 1 failures.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值