pytest的setup和teardown

在unittest中就有setup和teardown,pytest也不例外。主要有五种:

  • 模块级(setup_module/teardown_module),开始于模块始末,全局的
  • 函数级(setup_function/teardown_function),只对函数用例生效(不在类中)
  • 类级(setup_class/teardown_class),只在类中前后运行一次(在类中)
  • 方法级(setup_method/teardown_method),开始于方法前后(在类中)
  • setup/teardown运行在调用方法/函数的前后

例如:下面的test_setup.py测试了上面五种方法,且setup/teardown在模块里。

import pytest

def setup_module():
    print('setup_module')

def teardown_module():

    print('teardown_module')
def setup_function():
    print('setup_function')

def teardown_function():
    print('teardown_function')

def test_01():
    print('test01')

def test_02():
    print('test02')

class TestSet():
    @classmethod
    def setup_class(cls):
        print('setup_class')

    @classmethod
    def teardown_class(cls):
        print('teardown_class')

    def test_method1(self):
        print('test_method1')

    def test_method2(self):
        print('test_method2')

    def setup_method(self):
        print('setup_method')


    def teardown_method(self):
        print('teardown_method')


def setup():
    print('setup')

def teardown():
    print('teardown')

输出为:

D:\pythonProject\my_selenium_project\testcases\pytest>pytest -v -s  test_setup.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\my_selenium_project\testcases\pytest, configfile: pytest.ini
collected 4 items                                                                                                                                                    

test_setup.py::test_01 setup_module
setup_function
setup
test01
PASSEDteardown
teardown_function

test_setup.py::test_02 setup_function
setup
test02
PASSEDteardown
teardown_function

test_setup.py::TestSet::test_method1 setup_class
setup_method
test_method1
PASSEDteardown_method

test_setup.py::TestSet::test_method2 setup_method
test_method2
PASSEDteardown_method
teardown_class
teardown_module

setup/teardown在类中:

D:\pythonProject\my_selenium_project\testcases\pytest>pytest -v -s  test_setup.py
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.7.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- d:\python3.7.6\python.exe
cachedir: .pytest_cache
rootdir: D:\pythonProject\my_selenium_project\testcases\pytest, configfile: pytest.ini
collected 4 items                                                                                                                                                    

test_setup.py::test_01 setup_module
setup_function
test01
PASSEDteardown_function

test_setup.py::test_02 setup_function
test02
PASSEDteardown_function

test_setup.py::TestSet::test_method1 setup_class
setup_method
setup
test_method1
PASSEDteardown
teardown_method

test_setup.py::TestSet::test_method2 setup_method
setup
test_method2
PASSEDteardown
teardown_method
teardown_class
teardown_module


========================================================================= 4 passed in 0.04s =========================================================================

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

永远不要矫情

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值