【Pytest02】全网最全最新的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  执行结果显示更清楚。

猜你喜欢

转载自www.cnblogs.com/csmashang/p/12521064.html