python进阶之Unittest用法(2)

本文详细探讨了如何使用Unittest库在Python中无条件地跳过测试,提供了一种在测试中添加skip原因的方法,以帮助理解何时及为何要跳过特定的测试用例。
摘要由CSDN通过智能技术生成
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值