加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

pytest_用例运行级别_class级

发布时间:2020-12-20 10:57:32 所属栏目:Python 来源:网络整理
导读:‘‘‘ 模块级(setup_module/teardown_module)开始于模块始末,全局的在类中不起作用类级(setup_class/teardown_class)只在类中前后运行一次(在类中)方法级(setup_method/teardown_method)开始于方法始末(在类中)函数级(setup_function/teardown_fu
‘‘‘
模块级(setup_module/teardown_module)开始于模块始末,
全局的在类中不起作用
类级(setup_class/teardown_class)只在类中前后运行一次(在
类中)
方法级(setup_method/teardown_method)开始于方法始末
(在类中)

函数级(setup_function/teardown_function只对函数用例生
效(在类中不生效)
setup_function
teardown_function
‘‘‘
import  pytest
def setup_function():
    print()
    print("setup_function:class外的每个用例前开始执行")

def teardown_function():
    print("teardown_function:class外的每个个用例后开始执行")

def setup_module():
    """
    这是一个module级别的setup,它会在本module(test_fixt_class.py)里
    所有test执行之前,被调用一次。
    注意,它是直接定义为一个module里的函数"""
    print()
    print("-------------- setup before module --------------")
def teardown_module():
    """
    这是一个module级别的teardown,它会在本module(test_fixt_class.py)里
    所有test执行完成之后,被调用一次。
    注意,它是直接定义为一个module里的函数"""
    print("-------------- teardown after module --------------")
def test_add():
    print("正在执行test_fire")
    a = "hello"
    b = "hello word"
    assert a in b
class TestCase():
    def setup(self):
        print("setup: 每个用例开始前执行")
    def teardown(self):
        print("teardown: 每个用例结束后执行")
    def setup_class(self):
        print("setup_class:所有用例执行之前")
    def teardown_class(self):
        print("teardown_class:所有用例执行之前")
    def setup_method(self):
        print("setup_method: 每个用例开始前执行")
    def teardown_method(self):
        print("teardown_method: 每个用例结束后执行")
    def test_one(self):
        print("正在执行----test_one")
        x = "this"
        assert h in x
    def test_three(self):
        print("正在执行test_two")
        a = "hello"
        b = "hello word"
        assert a in b
    def add(self,a,b):
        print("这是加减法")
        return a + b
if __name__ == __main__:
    pytest.main([-s,test_fixt_class])

运行结果:
============================= test session starts =============================
platform win32 -- Python 3.7.4,pytest-5.1.0,py-1.8.0,pluggy-0.12.0
rootdir: E:py_pytestinterfacecollected 3 items

test_fixt_class.py
-------------- setup before module --------------

setup_function:class外的每个用例前开始执行
.正在执行test_fire
teardown_function:class外的每个个用例后开始执行
setup_class:所有用例执行之前
setup_method: 每个用例开始前执行
setup: 每个用例开始前执行
.正在执行----test_one
teardown: 每个用例结束后执行
teardown_method: 每个用例结束后执行
setup_method: 每个用例开始前执行
setup: 每个用例开始前执行
.正在执行test_two
teardown: 每个用例结束后执行
teardown_method: 每个用例结束后执行
teardown_class:所有用例执行之前
-------------- teardown after module --------------
[100%]

============================== 3 passed in 0.02s ==============================

?

结论1:
模块级(setup_module/teardown_module)开始于模块始末,
全局的在类中不起作用
函数级(setup_function/teardown_function只对函数用例生 效(在类中不生效)

结论2:
从结果看出,运行的优先级: setup_model>setup_class>setup_method> setup >用例case> teardown> teardown_method> teardown_class >teardown_model

结论3:
 从运行结果看出, setup_module/teardown_module 的优先级 
 是最大的,然后函数里面用到的 setup_function/teardown_function 
 不类里面的 setup_class/teardown_class 互不干涉

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读