pytest框架-测试用例setup和teardown

用例运行级别

模块级(setup_module/teardown_module)

函数级(setup_function/teardown_function)

类、类级方法级

当函数和类都有的时候

用例运行级别

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

模块级(setup_module/teardown_module)

setup_module:所有用例开始前只执行一次

teardown_module:所有用例结束后只执行一次

示例1:

#coding:utf-8
#模块级(setup_module/teardown_module)

import pytest

def setup_module():
    print("setup_module:所有用例开始前只执行一次")
    print("----------------------------")

def teardown_module():
    print("-----------------------------")
    print("teardown_module:所有用例结束后只执行一次")

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

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

def test_1():
    print("正在执行test_1。。。。。。")
    a="hello"
    #assert断言
    assert 'e' in a

def test_2():
    print("正在执行test_2。。。。。。")
    a="halo"
    assert  hasattr(a,'check')

def test_3():
    print("正在执行test_3。。。。。。")
    c="hello"
    d="hello world"
    assert c in d

if __name__ == '__main__':
    pytest.main(["-s", "test_01.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.9.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: D:\Python\WareSpace\py_case\python-pytest\setup_teardown
plugins: html-3.1.1, metadata-1.11.0
collected 3 items

test_01.py setup_module:所有用例开始前只执行一次
teardown_module:所有用例结束后只执行一次

================================== FAILURES ===================================
___________________________________ test_2 ____________________________________

    def test_2():
        print("正在执行test_2。。。。。。")
        a="halo"
>       assert  hasattr(a,'check')
E       AssertionError: assert False
E        +  where False = hasattr('halo', 'check')

test_01.py:29: AssertionError
=========================== short test summary info ===========================
FAILED test_01.py::test_2 - AssertionError: assert False
========================= 1 failed, 2 passed in 0.14s =========================

从运行结果看:setup_module和teardown_module只执行了一次

setup_function和teardown_function每次执行用例都执行了一次

这四种方法是可以任意组合的,可以用多个

函数级(setup_function/teardown_function)

pytest框架中只支持函数和类两种用例方式

setup_function/teardown_function:每个用例开始和结束调用一次,只对函数用例有效

示例2:

#coding:UTF-8
#函数级setup_function/teardown_function

import pytest

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

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

def test_1():
    print("正在执行test_1。。。。。。")
    a="hello"
    #assert断言
    assert 'e' in a

def test_2():
    print("正在执行test_2。。。。。。")
    a="halo"
    assert  hasattr(a,'check')

def test_3():
    print("正在执行test_3。。。。。。")
    c="hello"
    d="hello world"
    assert c in d

if __name__ == '__main__':
    pytest.main(["-s", "test_02.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.9.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: D:\Python\WareSpace\py_case\python-pytest\setup_teardown
plugins: html-3.1.1, metadata-1.11.0
collected 3 items

test_02.py setup_function:每个用例开始前执行
正在执行test_1。。。。。。
.teardown_function:每个用例结束后执行
setup_function:每个用例开始前执行
正在执行test_2。。。。。。
Fteardown_function:每个用例结束后执行
setup_function:每个用例开始前执行
正在执行test_3。。。。。。
.teardown_function:每个用例结束后执行

================================== FAILURES ===================================
___________________________________ test_2 ____________________________________

    def test_2():
        print("正在执行test_2。。。。。。")
        a="halo"
>       assert  hasattr(a,'check')
E       AssertionError: assert False
E        +  where False = hasattr('halo', 'check')

test_02.py:21: AssertionError
=========================== short test summary info ===========================
FAILED test_02.py::test_2 - AssertionError: assert False
========================= 1 failed, 2 passed in 0.13s =========================

从运行结果可以看出执行顺序:setup_function–用例–teardown_function

-s参数是为了显示用例的打印信息。 -q参数只显示结果,不显示过程

类、类级方法级

类(setup/teardown),类级(setup_class/teardown_class),方法级(setup_method/teardown_method)

示例3:

#coding:utf-8
#类、类级方法级
#类(setup/teardown),类级(setup_class/teardown_class),方法级(setup_method/teardown_method)

import pytest
class Test():
     def setup(self):
         print("setup:每个用例开始前都执行。。。。")
         print("------------------------------")

     def teardown(self):
         print("teardowm:每个用例结束后都执行。。。。")
         print("---------------------------------")

     def setup_class(self):
         print("setup_calss:所有用例执行之前。。。。")

     def teardown_calss(self):
         print("teardowm_calss:所有用例执行之后。。。。")

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

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

     def test_1(self):
         print("正在执行用例1。。。。。。")
         a="hello"
         assert 'o' in a
     def test_2(self):
         print("正在执行用例2。。。。。。")
         a="hello"
         assert hasattr(a,'check')
     def test_3(self):
         print("正在执行用例3。。。。。。")
         a="hello"
         b="hello halo"
         assert a in b

     if __name__ == "__main__":
         pytest.main(["-s", "test_03.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.9.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: D:\Python\WareSpace\py_case\python-pytest\setup_teardown
plugins: html-3.1.1, metadata-1.11.0
collected 3 items

test_03.py setup_calss:所有用例执行之前。。。。
setup_method:每个用例开始前执行
setup:每个用例开始前都执行。。。。
tear_method:每个用例结束后执行
setup_method:每个用例开始前执行
setup:每个用例开始前都执行。。。。
tear_method:每个用例结束后执行
setup_method:每个用例开始前执行
setup:每个用例开始前都执行。。。。
tear_method:每个用例结束后执行

从运行结果看:setup_calss>>setup_method>>setup>>用例>>teardown>>teardown_method>>>teardown_class

setup_method、teardown_methodhe 和 setup、teardown一样的,二者可以选其中一个

当函数和类都有的时候

示例4:

#coding:utf-8
#当函数和类都有的事情

import pytest

#模块级
def setup_module():
    print("setup_module:所有用例开始前只执行一次")
    print("----------------------------")

def teardown_module():
    print("-----------------------------")
    print("teardown_module:所有用例结束后只执行一次")

#函数级
def setup_function():
    print("setup_function:每个用例开始前执行")

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

def test_1():
    print("正在执行test_1。。。。。。")
    a="hello"
    #assert断言
    assert 'e' in a

def add(a,b):
    return a+b

def test_add():
    print("正在执行test_add。。。。。。")
    assert  add(3,4)==7

#类
class Test():
    #类级
    def setup_class(self):
        print("setup_calss:所有用例执行之前aaaaa")

    def teardown_class(self):
        print("teardown_class:所有用例执行之后bbbbbb")

    def test_3(self):
        print("正在执行用例3。。。。。。")
        a = "hello"
        assert 'o' in a

    def test_4(self):
        print("正在执行用例4。。。。。。")
        a = "hello"
        b = "hello halo"
        assert a in b

    if __name__ == "__main__":
        pytest.main(["-s", "test_04.py"])

运行结果:

test_04.py setup_module:所有用例开始前只执行一次
teardown_module:所有用例结束后只执行一次

从运行结果看:模块级setup_module、teardown_module优先级最大

函数级setup_function/teardown_function与类里面的setup_class/teardown_class同级,互不干涉不影响

Original: https://blog.csdn.net/weixin_46731640/article/details/119000156
Author: 你像甜甜的益达liy
Title: pytest框架-测试用例setup和teardown

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/776803/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球