pytest的setup和teardown

Posted 永远不要矫情

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 =========================================================================

以上是关于pytest的setup和teardown的主要内容,如果未能解决你的问题,请参考以下文章

4.pytest中固定装置setup和teardown

pytest的setup与teardown_byseyOrd

[接口测试_B] 06 Pytest的setup和teardown

Pytest测试用例之setup与teardown方法

Python测试框架pytest(03)setup和teardown

pytest4-测试用例setup和teardown