python进阶之Unittest用法(2)

本文详细探讨了如何使用Unittest库在Python中无条件地跳过测试,提供了一种在测试中添加skip原因的方法,以帮助理解何时及为何要跳过特定的测试用例。

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

4. Test code组织结构
4.1 在unittest中,test case都是测试类的实力,所以编写testcase时,必须预先定义测试子类。继承自Testcase或者FunctionTestcase。
Testcase提供的 assert*()方法是用于检测测试结果,
官方说明:
Note that in order to test something, we use one of the   assert*()  methods provided by the   TestCase  base class. If the test fails, an exception will be raised, and   unittest  will identify the test case as a   failure. Any other exceptions will be treated as   errors. This helps you identify where the problem is:   failures  are caused by incorrect results - a 5 where you expected a 6.   Errors  are caused by incorrect code - e.g., a   TypeError  caused by an incorrect function call.

例子:
import unittest
class WidgetTestCase(unittest.TestCase):
    def setUp(self):
        self.widget = Widget('The widget')

    def tearDown(self):
        self.widget.dispose()
        self.widget = None

    def test_default_size(self):
        self.assertEqual(self.widget.size(), (50,50),
                         'incorrect default size')

    def test_resize(self):
        self.widget.resize(100,150)
        self.assertEqual(self.widget.size(), (100,150),
                         'wrong size after resize')

(1)如果setUp()succeed, tearDown()不管testcase是否成功都会执行。
     如果setUp() failed, testcase不执行
(2)我们可以指定执行某一个testcase
widgetTestSuite = unittest.TestSuite()
widgetTestSuite.addTest(WidgetTestCase('test_default_size'))
widgetTestSuite.addTest(WidgetTestCase('test_resize'))
也可以直接执行整个类里面的所有testcase:
suite = unittest.TestLoader().loadTestsFromTestCase(WidgetTestCase)
我们也可以同时run指定所有类里面的testcase:
suite1 = module1.TheTestSuite()
suite2 = module2.TheTestSuite()
alltests = unittest.TestSuite([suite1, suite2])

5
5.1.怎样skip某些expected的failure
unnitest能将某些预先定义的failure标记为expectd,从而将这些failure不加入早testresult数据结果中。也可以直接skip掉某些方法或者类
使用skip() decorator这个修饰器来skip
(1)skip某些method
例子:
import unittest
import sys
import time
class MyTestCase(unittest.TestCase):

    @unittest.skip("demonstrating skipping")
    def test_nothing(self):
        self.fail("shouldn't happen")

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        # windows specific testing code
        pass

    def test_path(self):
        zone = time.timezone
        print "the zone is %s" ,zone

suite = unittest.TestLoader().loadTestsFromTestCase(MyTestCase)
unittest.TextTestRunner(verbosity=2).run(suite)

output如下:
the zone is %s -28800
test_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping'
test_path (__main__.MyTestCase) ... ok
test_windows_support (__main__.MyTestCase) ... ok

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

OK (skipped=1)

(2)Skip某些class
@unittest.skip("showing class skipping")
class MySkippedTestCase(unittest.TestCase):
    def test_not_run(self):
        pass
注意:setUp()也能被skip,当某些source不存在时,可以使用skip

5.2 expected failure的使用
定义需要使用expectedFailure() decorator
例子:
class ExpectedFailureTestCase(unittest.TestCase):
    @unittest.expectedFailure
    def test_fail(self):
        self.assertEqual(1, 0, "broken")
suite = unittest.TestLoader().loadTestsFromTestCase(ExpectedFailureTestCase)
unittest.TextTestRunner(verbosity=2).run(suite)

output如下:
test_fail (__main__.ExpectedFailureTestCase) ... expected failure
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK (expected failures=1)

5.3 skip和expected failure联合作战
 
 
用法如下: unittest. skip( reason)

Unconditionally skip the decorated test. reason should describe why the test is being skipped.

unittest. skipIf( condition, reason)

Skip the decorated test if condition is true

unittest. skipUnless( condition, reason)

Skip the decorated test unless condition is true

unittest. expectedFailure()

Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure

exception unittest. SkipTest( reason)

This exception is raised to skip a test.

 
 
Usually you can use TestCase.skipTest() or one of the skipping
decorators instead of raising this directly.

Skipped tests will not have setUp() or tearDown() run around them. Skipped classes will not have setUpClass() or tearDownClass() run.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值