• 全网最牛自动化测试框架系列之pytest(6)-Fixture(固件)


    【文章末尾给大家留下了大量的福利】 

    什么是固件

    Fixture 翻译成中文即是固件的意思。它其实就是一些函数,会在执行测试方法/测试函数之前(或之后)加载运行它们,常见的如接口用例在请求接口前数据库的初始连接,和请求之后关闭数据库的操作。

    我们之前在APP UI自动化系列中已经介绍过 unittest 的相关测试固件,如setupteardown等。而 pytest 中提供了功能更加丰富的Fixture,用于实现setupteardown功能。

    定义方式

    使用@pytest.fixture()进行定义,简单示例如下:

    1. import pytest
    2. @pytest.fixture()
    3. def before():
    4. print("连接数据库")

    调用方式

    调用单个fixture函数

    • 方式一,使用fixture函数名作为参数

      1. import pytest
      2. @pytest.fixture()
      3. def before():
      4. print("连接数据库")
      5. # 调用before
      6. def test_01(before):
      7. print("执行test_01")
    • 方式二,使用 @pytest.mark.usefixtures('fixture函数名')装饰器

      1. import pytest
      2. @pytest.fixture()
      3. def before():
      4. print("连接数据库")
      5. # 调用before
      6. @pytest.mark.usefixtures('before')
      7. def test_01():
      8. print("执行test_01")
    • 方式三,使用autouse参数自动执行fixture函数

      1. import pytest
      2. # fixture函数定义的时候使用autouse参数,作用域范围内的测试用例会自动调用该fixture函数
      3. @pytest.fixture(autouse=True)
      4. def before():
      5. print("连接数据库")
      6. # 自动调用before
      7. def test_01():
      8. print("执行test_01")

    三种方式调用后的结果都如下:

    我们可以看到,先执行了fixture函数,再执行测试函数。

    调用多个fixture函数

    1. import pytest
    2. @pytest.fixture()
    3. def before():
    4. print("连接数据库")
    5. @pytest.fixture()
    6. def before_s():
    7. print("初始化数据")
    8. def test_01(before, before_s):
    9. print("执行test_01")

    调用多个 fixture 函数时,由前至后依次执行,所以test_01()调用时先执行before,再执行before_s

    对fixture函数重命名

    定义fixture函数时,可以利用name参数进行重命名,方便用于调用,示例如下:

    1. import pytest
    2. @pytest.fixture(name='db')
    3. def connect_order_db():
    4. print("连接数据库")
    5. def test_01(db):
    6. print("执行test_01")

    使用fixture传递测试数据

    在执行完fixture函数后,有时需要将该fixture中得到到某些数据传递给测试函数/测试方法,用于后续的执行。

    fixture中提供普通传递和参数化传递两种数据传递方式。

    普通传递

    示例如下:

    1. import pytest
    2. @pytest.fixture()
    3. def before():
    4. print("连接数据库")
    5. return "连接成功!"
    6. def test_01(before):
    7. print("执行test_01")
    8. assert before == "连接成功!"

    注意,如果自定义的fixture函数有返回值,需要使用上面说的方式一调用才能获取fixture函数的返回值并传入测试函数中,方式二就无法获取返回值。

    参数化传递

    fixture函数进行参数化时,需要使用参数params,并且需要传入参数request,简单示例如下:

    1. import pytest
    2. test_params = [1, 2, 0]
    3. @pytest.fixture(params=test_params)
    4. def before(request):
    5. result = request.param
    6. return result
    7. def test_02(before):
    8. print("执行test_02")
    9. assert before
    10. if __name__ == '__main__':
    11. pytest.main()

    执行结果:

    可以看到,因为所调用的fixture函数进行了参数化,虽然只有一个测试函数但执行了3次。

    conftest.py

    上面我们举的例子都是把fixture函数放在测试用例模块里面,但如果很多测试模块需要引用同一个fixture函数怎么办,这是时候就需要把它放在命名为conftest的模块里,这样同级或以下目录中的测试用例便能调用这些自定义的fixture函数。

    例如,有如下目录:

    1. ├─testcase
    2. │ │
    3. │ ├─test_module_01
    4. │ │ test_case_1.py
    5. │ │ test_case_2.py
    6. │ │
    7. │ ├─test_module_02
    8. │ │ test_case_3.py

    test_module_01 中的test_case_1.pytest_case_2.py都需要调用同一个 fixture 函数,那么我们只需要在 test_module_01 中新建conftest.py并编写这个fixture函数即可,示例如下:

    1. ├─testcase
    2. │ │
    3. │ ├─test_module_01
    4. │ │ conftest.py
    5. │ │ test_case_1.py
    6. │ │ test_case_2.py
    7. │ │
    8. │ ├─test_module_02
    9. │ │ test_case_3.py

    conftest.py:

    1. import pytest
    2. @pytest.fixture(autouse=True)
    3. def before():
    4. print("连接数据库")

    test_case_1.py

    1. def test_01():
    2. print("执行test_01")

    test_case_2.py

    1. def test_02():
    2. print("执行test_02")

    这样,执行这两个模块的测试用例时会自动先去调用conftest.py中的before()函数。

    假设 test_module_02 中的 test_case_3.py 也需要调用这个before()函数,那么这个时候我们就需要在上一层即 testcase 中新建conftest.py并编写这个before()函数,才能在 test_case_3.py 中调用,如下:

    1. ├─testcase
    2. │ │ conftest.py
    3. │ │
    4. │ ├─test_module_01
    5. │ │ conftest.py
    6. │ │ test_case_1.py
    7. │ │ test_case_2.py
    8. │ │
    9. │ ├─test_module_02
    10. │ │ test_case_3.py

    conftest.py只作用于同级以下目录中的测试模块,且需要注意,当以下层级中存在了另一个conftest.py,那么以下层级将由另一个conftest.py文件接管。

    作用域

    pytest 的 fixture 作用域分sessionmoduleclassfunction四个级别。在定义 fixture 函数的时候通过scope参数指定作用范围,默认为function

    • session,每次会话执行一次
    • module,每个测试模块执行一次
    • class,每个测试类执行一次
    • function,每个测试方法执行一次

    注意,对于单独定义的测试函数,class、function 都会起作用,可以从下列示例中看出来。

    测试目录结构如下:

    1. ├─apiAutoTest
    2. │ │ run.py
    3. │ │
    4. │ ├─testcase
    5. │ │ │ conftest.py
    6. │ │ │
    7. │ │ ├─test_module_02
    8. │ │ │ │ conftest.py
    9. │ │ │ │ test_case_3.py
    10. │ │ │ │ test_case_4.py

    其中conftest.py代码如下:

    1. import pytest
    2. @pytest.fixture(scope="session", autouse=True)
    3. def session_fixture():
    4. print("这是一个作用于session的fixture")
    5. @pytest.fixture(scope="module", autouse=True)
    6. def module_fixture():
    7. print("这是一个作用于module的fixture")
    8. @pytest.fixture(scope="class", autouse=True)
    9. def class_fixture():
    10. print("这是一个作用于class的fixture")
    11. @pytest.fixture(scope="function", autouse=True)
    12. def function_fixture():
    13. print("这是一个作用于function的fixture")

    test_case_3.py代码如下:

    1. import pytest
    2. class TestOrder:
    3. def test_a(self):
    4. print("test_a")
    5. def test_b(self):
    6. print("test_b")
    7. def test_c():
    8. print("test_c")

    test_case_4.py代码如下:

    1. def test_e():
    2. print("test_e")

    run.py代码如下:

    1. import pytest
    2. if __name__ == '__main__':
    3. pytest.main(["-s"])

    运行run.py,结果如下:

    1. collected 4 items
    2. testcase\test_module_02\test_case_3.py
    3. 这是一个作用于session的fixture
    4. 这是一个作用于module的fixture
    5. 这是一个作用于class的fixture
    6. 这是一个作用于function的fixture
    7. test_a
    8. .这是一个作用于function的fixture
    9. test_b
    10. .这是一个作用于class的fixture
    11. 这是一个作用于function的fixture
    12. test_c
    13. .
    14. testcase\test_module_02\test_case_4.py
    15. 这是一个作用于module的fixture
    16. 这是一个作用于class的fixture
    17. 这是一个作用于function的fixture
    18. test_e
    19. .
    20. ============================== 4 passed in 0.04s ==============================

    从结果可以看出来:

    • 作用于session的fixture函数只在所有测试用例执行之前调用了一次
    • 作用于module的fixture函数在每个测试模块执行之前调用了一次
    • 作用于class的fixture函数在每个测试类执行之前调用了一次
    • 作用于function的fixture函数在每个测试方法/测试函数执行之前调用了一次

    注意,在定义的测试函数(如test_c()test_e())执行之前也会调用scope=class的fixture函数。

    总结

    与 unittest 框架比较,pytest 中的Fixture更加丰富,可扩展性更高。

    Fixture还有很多更加优雅的用法用于自动化测试项目中,本文只是以最简单的示例进行说明。

     重点:学习资料学习当然离不开资料,这里当然也给你们准备了600G的学习资料

    【需要的可以扫描文章末尾的qq群二维码自助拿走】

    【记得(备注“csdn000”)】

    【或私信000】

    群里的免费资料都是笔者十多年测试生涯的精华。还有同行大神一起交流技术哦。

    项目实战:

    大型电商平台:

    全套软件测试自动化测试教学视频

    300G教程资料下载【视频教程+PPT+项目源码】

    全套软件测试自动化测试大厂面经

    python自动化测试++全套模板+性能测试

    听说关注我并三连的铁汁都已经升职加薪暴富了哦!!!!

     

  • 相关阅读:
    概率论的学习和整理9:超几何分布--未完成
    gin 集成 Swagger
    【MyBatis框架】关联映射
    Spring配置那些事
    Stable Diffusion 模型下载:A-Zovya RPG Artist Tools(RPG 大师工具箱)
    创建内核线程模拟 cpu 占用率 100% 以触发系统异常
    位域的应用(花费时间过长,暂时放弃了,大小端的同步一半也不需要个人去考虑,但是可以作为debug的可能方向)
    内核对设备树的处理__从源头分析__内核head.S对dtb的简单处理
    Levels - UE5中的暴雨效果
    SpringBoot的Profile配置
  • 原文地址:https://blog.csdn.net/m0_60054525/article/details/126592717