Python单元测试_使用装饰器实现测试跳过和预期故障的方法

作者:jingxian 时间:2022-07-27 17:53:44 

Python单元测试unittest中提供了一下四种装饰器实现测试跳过和预期故障。(使用Python 2.7.13)

请查考Python手册中:

https://docs.python.org/dev/library/unittest.html

The following decorators implement test skipping and expected failures:

#以下装饰器实施测试跳过和预期故障:

@unittest.skip(原因)
Unconditionally skip the decorated test. reason should describe why the test is being skipped.

#无条件跳过装饰测试。 原因应该说明为什么要跳过测试。

@unittest.skipIf(条件,原因)
Skip the decorated test if condition is true.

#如果条件为真,跳过装饰测试。

@unittest.skipUnless(条件,原因)
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.

#将测试标记为预期的失败。 如果测试在运行时失败,则测试不会被视为失败。

(以上采用谷歌翻译,可能会有差异)

好了,写段代码看下,test.py ,使用的Eclipse


#coding:UTF-8
import unittest
from test.test_pprint import uni
class Test_ce(unittest.TestCase):
 a=16
 b=10

@unittest.skip('无条件跳过')
 def test_ce1(self):
   self.assertEqual((self.a-self.b), 16)
   #判断是否相等

@unittest.skipIf(True==1, '条件为真则跳过')
 def test_ce_2(self):
   self.assertFalse(self.a==self.b)
   #判断是否为False

@unittest.skipUnless(1==1, '条件为假则跳过')
 def test_ce_3(self):
   self.assertTrue(self.a>16)
   #判断是否为True

@unittest.expectedFailure
 def test_ce_4(self):
   self.assertFalse(self.a==16)

@unittest.expectedFailure
 def test_ce_5(self):
   self.assertFalse(self.a==15)

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

好的,运行一下


ssFxu
======================================================================
FAIL: test_ce_3 (__main__.Test_ce)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\Escplise\workspace\Pytest\src\test001\CE.py", line 20, in test_ce_3
 self.assertTrue(self.a>16)
AssertionError: False is not true

----------------------------------------------------------------------
Ran 5 tests in 0.000s

FAILED (failures=1, skipped=2, expected failures=1, unexpected successes=1)

好的,我们对第1行代码进行分析:

s:全称是skipped(跳过)

s:条件为真,所以也是skipped(跳过)

F:条件为真,所以忽略装饰器,执行断言代码,显然是failures(失败)

x:断言结果显然是失败的,但是这是在我们意料之中,所以是expected failures(预期的失败)

u:断言结果显然是pass,但是我们预计可能不通过,所以是unexpected successes(意想不到的成功)

即第13行代码 所示  FAILED (failures=1, skipped=2, expected failures=1, unexpected successes=1)

标签:Python,装饰器,测试跳过,预期故障
0
投稿

猜你喜欢

  • asp中将有双引号标题入库的方法

    2023-07-09 15:24:56
  • js三维正方体(兼容ie/ff)

    2008-04-12 14:38:00
  • js和jquery判断数据类型的4种方法总结

    2023-08-25 08:49:18
  • CSS 的模块化思想

    2009-02-03 12:52:00
  • python实现代理服务功能实例

    2023-10-04 05:36:57
  • 设计模式学习笔记之 - 简单工厂模式

    2009-03-11 13:38:00
  • 浅谈Django中的QueryDict元素为数组的坑

    2023-08-22 18:55:54
  • 使用MyISAM表和InnoDB的一些记录

    2009-12-20 18:21:00
  • 使用phpMyAdmin修改MySQL数据库root用户密码的方法

    2023-11-20 02:12:13
  • PHP实现获取第一个中文首字母并进行排序的方法

    2023-10-30 12:29:08
  • asp中的on error resume next用法

    2008-03-09 15:22:00
  • 请谨慎对待程序的图标和名称

    2011-06-16 20:35:22
  • js+csss实现的一个带复选框的下拉框

    2023-08-18 03:11:19
  • JavaScript setTimeout和setInterval的使用方法 说明

    2023-08-31 10:48:19
  • 浅谈如何使用Python控制手机(二)

    2022-05-01 20:16:28
  • ASP函数指针试探-GetRef

    2009-10-12 12:39:00
  • 如何对MySQL数据库表进行锁定

    2009-02-10 10:39:00
  • 微信小程序如何调用图片接口API并居中显示

    2023-08-09 15:05:30
  • ASP基础知识介绍

    2009-02-11 13:44:00
  • JavaScript 各种动画渐变效果

    2008-09-02 10:38:00
  • asp之家 网络编程 m.aspxhome.com