全网最全最新的Pytest框架快速进阶篇

 

一、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:
 

    def setup_class(self):

        print('Pytest所有用例的前置,所有用例之前只执行一次!')
 

    def teardown_class(self):

        print('Pytest所有用例的后置,所有用例执行之后只执行一次')
 

    def setup(self):

        print('Pytest每个用例前置')
 

    def teardown(self):

        print('Pytest每个用例后置')
 

    def test_03(self):

        print('测试用例三')
 

    def test04(self):

        print('测试用例四')
 

if __name__ == '__main__':

    pytest.main(['-s','pytest-demo.py'])

 

注意:setup、teardown、setup_class、teardown_class都是小写!

 

二、跳过用例

使用方法:

@pytest.mark.skipif(2>1,reason='当条件不True时跳过')

使用命令:pytest -vv  执行结果显示更清楚。

 

posted @   xmx测试员  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示