欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > Pytest框架中的Setup和Teardown功能

Pytest框架中的Setup和Teardown功能

2024/10/24 8:22:21 来源:https://blog.csdn.net/kymppcds/article/details/139383898  浏览:    关键词:Pytest框架中的Setup和Teardown功能

        在 pytest 测试框架中,setup 和 teardown是用于在每个测试函数之前和之后执行设置和清理的动作,而pytest 实际上并没有内置的 setup 和 teardown 函数,而是使用了一些装饰器或钩子函数来实现类似的功能。

学习目录

钩子函数(Hook Functions)

Fixture方式

    1 在测试函数执行前执行setup和teardown的动作 4

    2 在测试类执行前执行setup和teardown的动作 5

    3 同时实现测试类和测试函数的setup和teardown 6

     

钩子函数(Hook Functions)

以下钩子函数运行的优先级是模块级 > 类级 > 方法级

函数名

说明

setup_module

在python文件执行中的第1条用例执行前,先执行setup_module函数

teardown_module

在python文件执行中的最后1条用例执行结束后(不管成功还是失败),执行teardown_module函数

setup_class

在测试类中定义该函数,在测试类的第1条用例执行前,先执行setup_class函数

teardown_class

在测试类中定义该函数,在测试类的最后1条用例执行结束后(不管成功还是失败),执行teardown_class函数

setup_function

在python文件执行中的每一条用例执行前执行setup_function函数(适用于类外面和类里面的函数)

teardown_function

在python文件执行中的每一条用例执行结束后(不管成功还是失败),执行teardown_function函数(适用于类外面和类里面的函数)

举例:定义test_case1.py文件,设置函数用例test_1,测试类TestCase中两条用例test_2和test3。

定义setup_module和teardown_module、setup_class和teardown_class、setup_function和teardown_function函数。

import pytest
def setup_module():print("\nSetting up module... 模块开始前执行")
def teardown_module():print("\nTearing down module...模块结束后执行")def setup_function(function):print(f"\nSetting up for {function.__name__}... 函数开始前执行")
def teardown_function(function):print(f"\nTearing down for {function.__name__}...函数结束后执行")
#函数1
def test_1():assert 1!=1class TestCase():def setup_class():print(f"\nSetting up for class... 类开始前执行")def teardown_class():print(f"\nTearing down for class...类结束后执行")@staticmethoddef test_2():        #函数2assert 1 == 1@staticmethoddef test_3():         #函数3assert 1 == 1

运行该测试文件,执行结果如下:

按照测试用例的顺序,第一个函数test_1在类外面,执行函数的setup和teardown,而到了test_2和test_3函数因为在类里面,所以按照优先级先执行类的setup和teardown,再执行函数级setup和teardown

Fixture方式

fixture是pytest中最强大和推荐的方式,用于设置和清理测试环境。我们可以定义一个fixture,并在测试函数中使用它,该fixture将在测试函数之前执行,并在测试函数之后进行清理

  • 使用@pytest.fixture装饰器定义fixture。
  • 在测试函数中,通过函数参数的方式使用fixture。

1 在测试函数执行前执行setup和teardown的动作

设置一个函数,使用@pytest.fixture进行装饰

import pytest
@pytest.fixture
def my_fixture():print("\nSetting up fixture... 函数执行前执行该步骤")# 这里可以放置设置代码yieldprint("\nTearing down fixture... 函数执行后执行该步骤")# 这里可以放置清理代码def test_1(my_fixture):print("\nRunning testcase1...")

     yield 关键字的使用,它允许在 fixture 函数中定义两部分:设置代码(在 yield 之前)和清理代码(在 yield 之后)。将my_fixture函数作为参数传入测试函数test_1中。

用例执行后结果如下 同样实现了函数级的setup和teardown的功能

2 在测试类执行前执行setup和teardown的动作

定义一个fixture函数class_setup_teardown,在该函数使用 @pytest.fixture(scope='class', autouse=True) 装饰器(应用于类时scope参数定义为class,而上面的例子中应用于函数时可保持默认值function),并将 这个fixture 作为类中测试方法的参数,以此来实现类中的setup和teardown功能。

@pytest.fixture(scope='class', autouse=True)
def class_setup_teardown():print("\nClass setup (equal to setup_class)")# 设置代码yieldprint("\nClass teardown (equal to teardown_class)")# 清理代码

第一种方式:在测试函数中传入定义的fixture函数作为参数

class Testcase:def test_one(self, class_setup_teardown): # 直接将fixture作为参数print("\nRunning test_one")# 测试代码def test_two(self, class_setup_teardown): # 同样地,直接作为参数print("\nRunning test_two")# 测试代码
  • 第二种方式:在测试类中使用装饰器注明使用fixture,测试函数中不传参数
@pytest.mark.usefixtures("class_setup_teardown")
class Testcase:def test_one(self): print("\nRunning test_one")# 测试代码def test_two(self):print("\nRunning test_two")# 测试代码

用例执行后结果如下:两种方式都实现了测试类setup和teardown的功能

3 同时实现测试类和测试函数的setup和teardown

将以上两种方式结合在一起就可以了

@pytest.fixture
def my_fixture():print("\nSetting up fixture... 函数执行前执行该步骤")# 这里可以放置设置代码yieldprint("\nTearing down fixture... 函数执行后执行该步骤")# 这里可以放置清理代码@pytest.fixture(scope='class', autouse=True)
def class_setup_teardown():print("\nClass setup (equal to setup_class)")# 设置代码yieldprint("\nClass teardown (equal to teardown_class)")# 清理代码
  • 第1种方式: 在测试类中注明使用类fixture,在测试函数传入函数级fixture
@pytest.mark.usefixtures("class_setup_teardown")
class Testcase:def test_one(self,my_fixture): # 直接将fixture作为参数print("\nRunning test_one")# 测试逻辑def test_two(self,my_fixture): # 同样地,直接作为参数print("\nRunning test_two")# 测试逻辑
  • 第二种方式:在测试函数同时传入类级fixture和函数级fixture
class Testcase:def test_one(self,class_setup_teardown,my_fixture): # 直接将fixture作为参数print("\nRunning test_one")# 测试逻辑def test_two(self,class_setup_teardown,my_fixture): # 同样地,直接作为参数print("\nRunning test_two")# 测试逻辑

用例执行后结果如下:两种方式都实现了测试类setup和teardown的功能

当然Pytest的fixture功能远不止这些,后续我们再做其他功能的介绍。

共勉: 东汉·班固《汉书·枚乘传》:“泰山之管穿石,单极之绠断干。水非石之钻,索非木之锯,渐靡使之然也。”

-----指水滴不断地滴,可以滴穿石头;

-----比喻坚持不懈,集细微的力量也能成就难能的功劳。

----感谢读者的阅读和学习,谢谢大家

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com