前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Selenium2+python自动化70-unittest之跳过用例(skip)

Selenium2+python自动化70-unittest之跳过用例(skip)

作者头像
上海-悠悠
发布2018-04-08 16:49:25
7500
发布2018-04-08 16:49:25
举报

前言

当测试用例写完后,有些模块有改动时候,会影响到部分用例的执行,这个时候我们希望暂时跳过这些用例。

或者前面某个功能运行失败了,后面的几个用例是依赖于这个功能的用例,如果第一步就失败了,后面的用例也就没必要去执行了,直接跳过就行,节省用例执行时间。

一、skip装饰器

skip装饰器一共有四个

@unittest.skip(reason)

  • Unconditionally skip the decorated test. reason should describe why the test is being skipped. 翻译:无条件跳过用例,reason是说明原因
  • @unittest.skipIf(condition, reason)
  • Skip the decorated test if condition is true. 翻译:condition为true的时候跳过
  • @unittest.skipUnless(condition, reason)
  • Skip the decorated test unless condition is true. 翻译:condition为False的时候跳过
  • @unittest.expectedFailure
  • Mark the test as an expected failure. If the test fails when run, the test is not counted as a failure. 翻译:断言的时候跳过(暂时不知道有啥用,没看懂,貌似断言失败,也变成用例pass了。)

二、skip案例

运行结果:

代码语言:javascript
复制
测试1
测试4
.ssx
----------------------------------------------------------------------
Ran 4 tests in 0.003s

OK (skipped=2, expected failures=1)

三、跳过整个测试类

四、参考代码:

代码语言:javascript
复制
# coding:utf-8
import unittest
class Test(unittest.TestCase):

    @unittest.skip(u"无条件跳过此用例")
    def test_1(self):
        print "测试1"

    @unittest.skipIf(True, u"为True的时候跳过")
    def test_2(self):
        print "测试2"

    @unittest.skipUnless(False, u"为False的时候跳过")
    def test_3(self):
        print "测试3"

    @unittest.expectedFailure
    def test_4(self):
        print "测试4"
        self.assertEqual(2, 4, msg=u"判断相等")

if __name__ == "__main__":
    unittest.main()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2017-06-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档