pytest_用例运行级别_模块级

'''
pytest 参数说明
https://www.jianshu.com/p/7a7432340f02
 -x test_fixt_model.py  遇到错误时,停止运行
 用-v运行(-v显示运行的函数)py.test –v test_fixt_model.py,

用例设计原则
 文件名以 test_*.py 文件和*_test.py
 以 test_开头的函数
 以 Test 开头的类
 以 test_开头的方法
 所有的包 pakege 必项要有__init__.py 文件
 
 用例运行级别
 模块级(setup_module/teardown_module)开始于模块始末,
全局的在类中不起作用
 函数级(setup_function/teardown_function只对函数用例生
效(不在类中)
 类级(setup_class/teardown_class)只在类中前后运行一次(在
类中)
 方法级(setup_method/teardown_method)开始于方法始末
(在类中)
 类里面的(setup/teardown)运行在调用方法的前后
'''
import  pytest
def setup_module():
    """
    这是一个module级别的setup,它会在本module(test_fixt_model.py)里
    所有test执行之前,被调用一次。
    注意,它是直接定义为一个module里的函数"""
    print()
    print("-------------- setup before module --------------")
def teardown_module():
    """
    这是一个module级别的teardown,它会在本module(test_fixt_model.py)里
    所有test执行完成之后,被调用一次。
    注意,它是直接定义为一个module里的函数"""
    print("-------------- teardown after module --------------")

class TestClass():
    def setup(self):
        print("setup:每个用例前,都会执行")
    def teardown(self):
        print("teardown:每个用例后,都会执行")
    def test_one(self):
        print("正在执行test_one")
        x = "this"
        assert 'h' in x
    def test_two(self):
        print("正在执行test_two")
        a = "hello"
        b = "hello word"
        assert  a in b
class TestUser():
    def setup(self):
        print("setup:每个用例前,都会执行")
    def teardown(self):
        print("teardown:每个用例后,都会执行")
    def test_one(self):
        print("正在执行test_one")

if __name__ == '__main__':
    pytest.main(['-q','test_fixt_model'])

运行结果;


============================= 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 3 items

test_fixt_model.py
-------------- setup before module --------------
setup:每个用例前,都会执行
.正在执行test_one
teardown:每个用例后,都会执行
setup:每个用例前,都会执行
.正在执行test_two
teardown:每个用例后,都会执行
setup:每个用例前,都会执行
.正在执行test_one
teardown:每个用例后,都会执行
-------------- teardown after module --------------
[100%]

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

猜你喜欢

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