pytest_用例运行级别_class级

 
 
'''
模块级(setup_module/teardown_module)开始于模块始末,
全局的在类中不起作用
类级(setup_class/teardown_class)只在类中前后运行一次(在
类中)
方法级(setup_method/teardown_method)开始于方法始末
(在类中)

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

def teardown_function():
    print("teardown_function:没个用例后开始执行")
import  pytest
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 --------------")
class TestCase():
    def setup_module(self):
        """
        这是一个module级别的setup,它会在本module(test_fixt_class.py)里
        所有test执行之前,被调用一次。
        注意,它是直接定义为一个module里的函数"""
        print()
        print("-------------- setup before module in class --------------")

    def teardown_module(self):
        """
        这是一个module级别的teardown,它会在本module(test_fixt_class.py)里
        所有test执行完成之后,被调用一次。
        注意,它是直接定义为一个module里的函数"""
        print("-------------- teardown after module  in class --------------")

    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
if __name__ == '__main__':
    pytest.main(['-q', '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_pytest\interfacecollected 2 items

test_fixt_class.py
-------------- setup before module --------------
setup_class:所有用例执行之前
setup_method: 每个用例开始前执行
setup: 每个用例开始前执行
.正在执行----test_one
teardown: 每个用例结束后执行
teardown_method: 每个用例结束后执行
setup_method: 每个用例开始前执行
setup: 每个用例开始前执行
.正在执行test_two
teardown: 每个用例结束后执行
teardown_method: 每个用例结束后执行
teardown_class:所有用例执行之前
-------------- teardown after module --------------
[100%]

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

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

猜你喜欢

转载自www.cnblogs.com/tallme/p/11369791.html