模块级别:setup_module、teardown_module
说明:该方法表示只能类外面执行用例过程中,只执行1次。相当于unittest中的setupclass和teardownclass方法
函数级别:setup_function、teardown_function
说明:该方法表示在类外面执行用例过程中,每次都会执行前置和后置。
类级别:setup_class、teardown_class
说明:该方法表示在类中执行测试用例前,只执行1次测试前置和测试后置,注意:放在类外面不生效
方法级别:setup_method、teardown_method
说明:该方法表示在类中每次执行测试用例前,测试前置和测试后置都会执行一次,注意:放在类外面不生效
函数和方法级别:setup、teardown
说明:该方法这个可以在类中使用,也可以在类外进行使用,大家最熟悉
多个组合时
# _*_coding:utf-8 _*_
# @Time :2021/7/2 22:18
# @Author : king
# @File :test_demo01.py
# @Software :PyCharm
# @blog :https://blog.youkuaiyun.com/u010454117
# @WeChat Official Account: 【测试开发知识库】
import pytest
def setup_module():
print("我是类外面的 setup_module 方法")
def teardown_module():
print("我是类外面的 teardown_module 方法")
def setup_function():
print("我是类外面的 setup_function 方法")
def teardown_function():
print("我是类外面的 teardown_function 方法")
def setup():
print("我是类外面 setup 方法")
def teardown():
print("我是类外面 teardown 方法")
def test_one():
print("我是类外面的 test_one 用例")
def test_two():
print("我是类外面的 test_two 用例")
class TestDemo():
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 setup(self):
print("我是类里面 setup 方法")
def teardown(self):
print("我是类里面 teardown 方法")
def test_one(self):
print("我是类里面的 test_one 用例")
def test_two(self):
print("我是类里面的 test_two 用例")
if __name__ == '__main__':
pytest.main()
我们根据执行结果看见方法执行顺序
setup_module、teardown_module 用在类外面,整个测试只执行一次
setup_function、teardown_function 类外面方法每个用例都执行
setup_class、teardown_class 类里面只执行一次
setup_method、teardown_method 类里面每个用例都执行
setup、teardown 类里面和外面每个用例都执行,有3个用例 ,就执行3次
pytest测试框架系列 - setup和teardown的详细讲解_测试之路king的博客-优快云博客