Python Test使用

本文介绍了Python自带的单元测试模块,详细讲解了如何在test_list.py中编写以test开头的方法,以及如何利用TestSuite进行更细粒度的测试组织。此外,还探讨了如何使用test_skip.py中的skip功能跳过特定测试,并介绍了test_doc.py中测试文档的使用,确保测试正确无误。

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

python自带的单元测试模块

unittest doctest

test_list.py

# -*- coding: utf-8 -*-
import unittest

class TestList(unittest.TestCase):
    def setUp(self):
        self.c_list = range(5)
        print("setup test module")

    def tearDown(self):
        del self.c_list
        print("teardown test module")

    def test_equal(self):
        print("test equal")
        c = self.c_list
        self.assertEqual(c, [0,1,2,3,4])

    def test_in(self):
        print("test in")
        c = self.c_list
        self.assertIn(1, c)

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

每个以test开头的method都会被调用,调用之前会调用setUp方法,之后会调用tearDown方法

test_with_suit

# -*- coding: utf-8 -*-
import unittest

class TestList(unittest.TestCase):
    def setUp(self):
        self.c_list = range(5)
        print("setup test module")

    def tearDown(self):
        del self.c_list
        print("teardown test module")

    def test_equal(self):
        print("test equal")
        c = self.c_list
        self.assertEqual(c, [0,1,2,3,4])

    def test_in(self):
        print("test in")
        c = self.c_list
        self.assertIn(1, c)

if __name__ == "__main__":
    suite = unittest.TestSuite()
    suite.addTest(TestList("test_equal"))  
    suite.addTest(TestList("test_in"))
    runner = unittest.TextTestRunner(verbosity=2)
    runner.run(suite)

使用TestSuite,可以让测试更细化,将需要测试的method放入suite中

test_skip.py

# -*- coding: utf-8 -*-
import unittest
SKIP = False
class TestList(unittest.TestCase):
    def setUp(self):
        self.c_list = range(5)
        print("setup test module")

    def tearDown(self):
        del self.c_list
        print("teardown test module")

    @unittest.skip("skip this test")
    def test_equal(self):
        print("test equal")
        c = self.c_list
        self.assertEqual(c, [0,1,2,3,4])

    @unittest.skipIf(SKIP==True, "skip this test with if")
    def test_in(self):
        print("test in")
        c = self.c_list
        self.assertIn(1, c)

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

可以让某个测试方法直接跳过,或者使用@unittestskipIf,@unittest.skipUnless装饰器,控制测试的数量

test_doc.py

# -*- coding: utf-8 -*-

def double(num):
    """ This function calculate positive integer square value.

    Example:

        >>> double(10)
        100
        >>> double(-1)
        Traceback (most recent call last):
        ...
        ValueError: the num must is positive integer
    """

    if num < 0:
        raise ValueError('the num must is positive integer')
    return num**2

if __name__ == "__main__":
    import doctest
    doctest.testmod()

如果没有输出,就说明是测试时正确的

Github地址
(于2017年4月8日,http://blog.youkuaiyun.com/bzd_111

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值