栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

py自动化之跳过某一条用例(unittest)

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

py自动化之跳过某一条用例(unittest)

在自动化过程中,我们可能因为某一些原因跳过一些自动话用例,可以使用以下几种方法

1.@unittest.skip(reason=‘跳过原因’)的功能是跳过,可装饰于类与方法。打上后直接跳过。

2.@unittest.skipIf(判断, reason=‘跳过原因’)的功能是跳过,可装饰于类与方法。当判断为True时跳过。

3、@unittest.skipUnless(判断, reason=‘跳过原因’)的功能是跳过,可装饰于类与方法。当判断为False时跳过。

4、@unittest.expectedFailure:如果test失败了,这个test不计入失败的case数目。

import unittest


class TestDemo(unittest.TestCase):

    @unittest.skip(reason="跳过用例test_01")
    def test_01(self):
        print("test_01用例")

    @unittest.skipIf(0 < 3, reason="0小于3为True,跳过这条用例")
    def test_02(self):
        print("test_02用例")

    @unittest.skipIf(0 > 3, reason="0大于3为False,执行这条用例")
    def test_03(self):
        print("test_03用例")

    @unittest.skipUnless(5 < 10, reason="5小于10为True,执行这条用例")
    def test_04(self):
        print("test_04用例")

    @unittest.skipUnless(5 > 10, reason="5大于10为False,跳过这条用例")
    def test_05(self):
        print("test_04用例")

    @unittest.expectedFailure
    def test_06(self):
        i = 1
        self.assertEqual(i, "2")


# 创建测试套件
suite = unittest.TestSuite()
# 添加TestDemo这个测试类到测试套件中,这个类中的所有用例都会被添加进来
suite.addTest(unittest.makeSuite(TestDemo))
# 创建执行器
runner = unittest.TextTestRunner()
runner.run(suite)

执行结果:

以上是针对装饰方法使用的例子,也可以装饰类使用

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860047.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号