5. pytest的xunit风格的前置后置

前言

pytest作为更高级的测试框架,在底层上兼容了unittest的前置后置逻辑,以便测试人员兼容开发
前置分为两个部分去讲,一种是纯方法层次上的前后置,一种是类内部的用例的前后置

前后置类型

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

方法用例前后置

依次执行setup_module >[ setup_function > setup > testcase > teardown > teardown_method ]> teardown_module,其中[]在有多个用例时循环执行

  • 看下示例代码
# content of test_setup_teardown.py
import pytest


def setup_module():
    print('\nsetup_module:模块级别前置,每个模块执行1次')


def teardown_module():
    print('setup_module:模块级别后置,每个模块执行1次')


def setup_function():
    print("setup_function:每个用例开始前都会执行")


def teardown_function():
    print("teardown_function:每个用例结束后都会执行")


def setup():
    print('setup:方法调用前,每个测试函数调用前都会执行')


def teardown():
    print('\nteardown:方法调用后,每个测试函数结束后都会执行')


def test_one():
    print("正在执行---test_one")


def test_two():
    print("正在执行---test_two")

  • 看下执行结果是否和预设一致
(venv) C:\测试文件夹\project\python\pytest_demo\fixture>  pytest -sv test_setup_teardown.py
======================================================= test session starts ========================================================
platform win32 -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- c:\program files (x86)\python36-32\python.exe
cachedir: .pytest_cache
rootdir: C:\测试文件夹\project\python\pytest_demo, configfile: pytest.ini
collected 2 items                                                                                                                   

test_setup_teardown.py::test_one
setup_module:模块级别前置,每个模块执行1次
setup_function:每个用例开始前都会执行
setup:方法调用前,每个测试函数调用前都会执行
正在执行---test_one
PASSED
teardown:方法调用后,每个测试函数结束后都会执行
teardown_function:每个用例结束后都会执行

test_setup_teardown.py::test_two setup_function:每个用例开始前都会执行
setup:方法调用前,每个测试函数调用前都会执行
正在执行---test_two
PASSED
teardown:方法调用后,每个测试函数结束后都会执行
teardown_function:每个用例结束后都会执行
setup_module:模块级别后置,每个模块执行1次

======================================================== 2 passed in 0.01s =========================================================

类用例前后置

依次执行setup_module > setup_class > [ setup_function > setup > testcase > teardown > teardown_method ]> teardown_class > teardown_module,其中[]在有多个用例时循环执行

  • 看下示例代码
# content of test_setup_teardown.py
import pytest


def setup_module():
    print('\nsetup_module:模块级别前置,每个模块执行1次')


def teardown_module():
    print('setup_module:模块级别后置,每个模块执行1次')


class Test:
    
    @classmethod
    def setup_class(cls):
        print("setup_class:所有用例执行之前(类级)")

    @classmethod
    def teardown_class(cls):
        print("teardown_class:所有用例执行之后(类级)")

    def setup(self):
        print("setup:每个用例开始前执行(调用方法前)")

    def teardown(self):
        print("teardown:每个用例结束后执行(调用方法后)")

    def setup_method(self):
        print("setup_method:每个用例开始前执行(方法级)")

    def teardown_method(self):
        print("teardown_method:每个用例结束后执行(方法级)")

    def test_one(self):
        print("正在执行---test_one")

    def test_two(self):
        print("正在执行---test_two")


  • 看下执行结果是否和预设一致
(venv) C:\测试文件夹\project\python\pytest_demo\fixture>pytest -sv test_setup_teardown.py::Test
======================================================= test session starts ========================================================
platform win32 -- Python 3.6.8, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- c:\program files (x86)\python36-32\python.exe
cachedir: .pytest_cache
rootdir: C:\测试文件夹\project\python\pytest_demo, configfile: pytest.ini
collected 2 items                                                                                                                   

test_setup_teardown.py::Test::test_one
setup_module:模块级别前置,每个模块执行1次
setup_class:所有用例执行之前(类级)
setup_method:每个用例开始前执行(方法级)
setup:每个用例开始前执行(调用方法前)
正在执行---test_one
PASSEDteardown:每个用例结束后执行(调用方法后)
teardown_method:每个用例结束后执行(方法级)

test_setup_teardown.py::Test::test_two setup_method:每个用例开始前执行(方法级)
setup:每个用例开始前执行(调用方法前)
正在执行---test_two
PASSEDteardown:每个用例结束后执行(调用方法后)
teardown_method:每个用例结束后执行(方法级)
teardown_class:所有用例执行之后(类级)
setup_module:模块级别后置,每个模块执行1次

======================================================== 2 passed in 0.02s =========================================================

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容