pytest 学习记录(五)标记测试用例(skip skipif xfail)

示例代码:

# -*- coding: utf-8 -*-
import unittest
import pytest
from mudules.mudules import skip1, skipif1, run_these_please
class TestDemo(unittest.TestCase):
    # @skipif1 # pytest.mark.skipif(1 + 1 == 2, reason='跳过') 在mudules.mudules中定义的
    @skip1 # skip1 = pytest.mark.skip('跳过')   在mudules.mudules中定义的
    @pytest.mark.run_these_please
    def test_test01(self):
        print('进行testcase01')
        # self.assertEqual(1, 11, "判断相等")
        assert 1 == 11
        assert 1 <= 2
        assert 'h' in 'thes'

    # 跳过某条用例
    # @unittest.skip('跳过')
    @pytest.mark.skip('跳过')
    def test_test02(self):
        print('进行testcase02')
        self.assertIn('h1', 'this', '判断是否在里面')

    # 跳过用例当if条件成立
    # @unittest.skipIf(1 + 1 == 2, 'skipif跳过')
    @pytest.mark.skipif(1 + 1 == 2, reason='skipif跳过')
    def test_test03(self):
        print('进行testcase03')
        self.assertIn('h1', 'this', '判断是否在里面')

    @pytest.mark.run_these_please
    # @run_these_please  #run_these_please = pytest.mark.run_these_please 在mudules.mudules中定义的
    def test_test04(self):
        print('进行testcase04')
        # self.assertIn(h, 'this', '语法错误,str未用引号引起来')
        assert 1 == 11
        assert 1 <= 2
        assert 'h' in 'thes'

    # @pytest.mark.run_these_please
    @pytest.mark.xfail(resason='sasdasd')
    def test_test05(self):
        print('进行testcase05')
        # self.assertIn(h, 'this', '语法错误,str未用引号引起来')
        assert 1 == 11
        assert 1 <= 2
        assert 3 >= 2
        assert 'h' in 'thes'
        assert 'hh' not in 'h1y'

跳过测试用例

使用如下所示进行标记
@pytest.mark.skip()
@pytest.mark.skipif(condition, reason=None)

运行示例代码,运行结果如下,可以看出被标记为skip 或skipif的用例跳过未运行

在这里插入图片描述

标记测试用例

打标签:@pytest.mark.tagname

运行示例代码中@pytest.mark.run_these_please标记的测试用例

pytest -v .\test_case_demo_pytest.py -m run_these_please

在这里插入图片描述

标记测试函数为预期失败:

使用@pytest.mark.xfail(condition=None, reason=None, raises=None, run=True, strict=False)标记函数,如下代码片段所示:

import pytest
class TestDemo(unittest.TestCase):
    @pytest.mark.xfail(resason='预期失败并且执行失败')
    def test_test05(self):
        print('进行testcase05')
        # self.assertIn(h, 'this', '语法错误,str未用引号引起来')
        assert 1 == 11
        assert 1 <= 2
        assert 3 >= 2
        assert 'h' in 'thes'
        assert 'hh' not in 'h1y'

    @pytest.mark.xfail(resason='预期失败但是执行成功')
    def test_test06(self):
        print('进行testcase05')
        # self.assertIn(h, 'this', '语法错误,str未用引号引起来')
        assert 1 == 1
        assert 1 <= 2
        assert 3 >= 2
        assert 'h' in 'thes'
        assert 'hh' not in 'h1y'

运行命令

 pytest -v .\test_case_demo_pytest.py -k 'test_test05 or test_test06'

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值