【pytest02】pytest框架快速进阶篇-pytest前置和pytest后置,skipif跳过用例

本文介绍了Pytest如何实现测试的前置和后置方法,包括集成unittest以及pytest自身的setup、teardown等。此外,还详细讲解了如何使用skipif来跳过特定的测试用例。通过阅读,你可以了解到Pytest更深入的用法。
摘要由CSDN通过智能技术生成

一、Pytest的前置和后置方法

1.Pytest可以集成unittest实现前置和后置

import unittest
import pytest

class TestCase(unittest.TestCase):
    def setUp(self) -> None:
        print('unittest每个用例前置')

    def tearDown(self) -> None:
        print('unittest每个用例后置')

    @classmethod
    def setUpClass(cls) -> None:
        print('unittest所有用例的前置,所有用例之前只执行一次!')

    @classmethod
    def tearDownClass(cls) -> None:
        print('unittest所有用例的后置,所有用例执行之后只执行一次')

    def test_03(self):
        print('测试用例三')

    def test04(self):
        print('测试用例四')

if __name__ == '__main__':
    pytest.main(['-s','pytest-demo.py'])

注意:setUpClass和tearDownClass需要用@classmethod装饰器装饰。

2.Pytest前置和后置

import pytest

class TestCase:
引用\[1\]:在pytest中,可以使用`pytest.skip`函数来跳过测试用例。可以在测试函数或测试类中使用`pytest.skip`函数来指定跳过的原因。例如,在测试函数中使用`pytest.skip`函数可以跳过该测试函数,并在跳过时提供一个消息。示例代码如下: ```python import pytest def test_01(): print("我是测试函数。") pytest.skip(msg="跳过此条用例。") assert 1 == 1 class TestSkip: def test_one(self): print("test_one方法执行") assert 1 == 1 def test_two(self): pytest.skip(msg="跳过此条用例。") print("test_two方法执行") assert "king" in "hello,king" ``` 在上述示例中,`test_01`函数和`test_two`方法都使用了`pytest.skip`函数来跳过测试用例,并提供了跳过的原因。当运行这些测试用例时,它们将被跳过执行。 引用\[2\]:另一种在pytest跳过测试用例的方法是使用`pytest.mark.skip`装饰器。可以在测试函数或测试类上使用`pytest.mark.skip`装饰器来指定跳过的原因。示例代码如下: ```python import pytest pytestmark = pytest.mark.skip(reason="跳过当前模块") def test_01(): print("我是测试函数。") assert 1 == 1 class TestSkip: def test_one(self): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" ``` 在上述示例中,使用`pytest.mark.skip`装饰器标记了整个模块为跳过状态,并提供了跳过的原因。当运行这个模块的测试用例时,所有的测试用例都将被跳过执行。 引用\[3\]:还可以使用`pytest.mark.skip`装饰器来跳过测试函数或测试类。示例代码如下: ```python import pytest @pytest.mark.skip(reason="跳过测试函数的测试case") def test_01(): print("我是测试函数。") assert 1 == 1 class TestSkip: @pytest.mark.skip(reason="跳过类里面case") def test_one(self): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" ``` 在上述示例中,使用`pytest.mark.skip`装饰器标记了`test_01`函数和`test_one`方法为跳过状态,并提供了跳过的原因。当运行这些测试用例时,它们将被跳过执行。 综上所述,pytest中可以使用`pytest.skip`函数或`pytest.mark.skip`装饰器来跳过测试用例,并提供跳过的原因。 #### 引用[.reference_title] - *1* *2* *3* [pytest测试框架系列 - Pytest skipskipif 跳过用例看这篇就够了!](https://blog.csdn.net/u010454117/article/details/118469127)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值