用例运行级别
-
模块级(setup_module/teardown_module)开始于模块始末,全局的
-
函数级(setup_function/teardown_function)只对函数用例生效(不在类中)
-
类级(setup_class/teardown_class)只在类中前后运行一次(在类中)
-
方法级(setup_method/teardown_method)开始于方法始末(在类中)
-
类里面的(setup/teardown)运行在调用方法的前后
函数式
setup_function/teardown_function
1.pytest框架支持函数和类两种用例方式,先看函数里面的前置与后置用法:
setup_function/teardown_function 每个用例开始和结束调用一次
# test_1.py
# coding:utf-8
import pytest
# 函数式
def setup_function():
print("setup_function:每个用例开始前都会执行")
def teardown_function():
print("teardown_function:每个用例结束后都会执行")
def test_one():
print("正在执行----test_one")
x = "how"
assert 'h' in x
def test_two():
print("正在执行----test_two")
x = "hello"
assert hasattr(x, 'check')
def test_three():
print("正在执行----test_three")
a = "hello"
b = "hello world"
assert a in b
if __name__ == "__main__":
pytest.main(["-s", "test_1.py"])
本文详细介绍了pytest框架中不同级别的用例运行设置,包括模块级、函数级、类级和方法级等,并通过示例代码展示了如何使用setup_function与teardown_function进行每个测试用例的前置与后置处理。
980

被折叠的 条评论
为什么被折叠?



