找回密码
 立即注册
首页 业界区 业界 接口测试——pytset框架续集

接口测试——pytset框架续集

驼娑 3 天前
pytest框架——fixture

pytest 中的fixture 是⼀种强大的机制,用于提供测试函数所需的资源或上下文。它可以用于设置测试环境、准备数据等。以下是fixture 的⼀些核心概念和使用场景.


  • 示例1:未标记fixture⽅法的调用
  1. def fixture_01():
  2. print("第⼀个fixture标记的方法")
  3. def test_01():
  4. fixture_01()
  5. print("第⼀个测试用例")
复制代码
  1. collected 1 item
  2. test_xxx.py::test_01 第一个fixture标记的方法
  3. 第一个测试用例
  4. PASSED
复制代码

  • fixture标记的方法调用
  1. import pytest
  2. @pytest.fixture
  3. def fixture_01():
  4. print("第⼀个fixture标记的方法")
  5. def test_01(fixture_01):
  6. print("第⼀个测试⽤例")
复制代码
  1. collected 1 item
  2. test_xxx.py::test_01 第一个fixture标记的方法
  3. 第一个测试用例
  4. PASSED
复制代码

  • 未标记 fixture ⽅法的调用与 fixture 标记的方法调⽤完全不⼀样,前者需要在⽅法体中调用,而后者可以将函数名作为参数进行调⽤。
  • 示例2:访问列表页和详情页之前都需要执行登录操作
  1. import pytest
  2. @pytest.fixture
  3. def login():
  4. print("---执⾏登陆操作-----")
  5. def test_list(login):
  6. print("---访问列表⻚")
  7. def test_detail(login):
  8. print("---访问详情⻚")
复制代码

  • 运行结果:
    1.png

  • 通过使用@pytest.fixture 装饰器来告诉pytest⼀个特定函数是⼀个fixture,通过运行结果可见,在执行列表页和详情页之前都会先执行 login 方法。
fixture嵌套
  1. import pytest
  2. @pytest.fixture
  3. def first_entry():
  4. return "a"
  5. @pytest.fixture
  6. def order(first_entry):
  7. return [first_entry]
  8. def test_string(order):
  9. order.append("b")
  10. assert order == ["a", "b"]
复制代码

  • 测试不必局限于单个fixture ,它们可以依赖于想要的任意数量的fixture ,并且fixture 也可以使⽤其他fixture。
请求多个fixture
  1. import pytest
  2. class Fruit:
  3. def __init__(self, name):
  4. self.name = name
  5. def __eq__(self, other):
  6. return self.name == other.name
  7. @pytest.fixture
  8. def my_fruit():
  9. return Fruit("apple")
  10. @pytest.fixture
  11. def fruit_basket(my_fruit):
  12. return [Fruit("banana"), my_fruit]
  13. def test_my_fruit_in_basket(my_fruit, fruit_basket):
  14. assert my_fruit in fruit_basket
复制代码

  • 测试和 fixture 不仅限于⼀次请求单个 fixture ,它们可以请求任意多个。
yield fixture


  • 当运行测试时,我们希望确保它们能够自己清理,以便它们不会⼲扰其他测试(同时也避免留下⼤量测试数据来膨胀系统)。pytest中的 fixture 提供了⼀个⾮常有用拆卸系统,它允许我们为每个 fixture 定义具体的清理步骤。
  • “Yield” fixture 使用 yield 而不是 return 。有了这些 fixture ,我们可以运行⼀些代码,并将对象返回给请求的 fixture/test ,就像其他 fixture ⼀样。唯⼀的不同是:

    • return 被替换为 yield 。
    • 该 fixture 的任何拆卸代码放置在 yield 之后。

  • ⼀旦 pytest 确定了 fixture 的线性顺序,它将运行每个 fixture 直到它返回或 yield ,然后继续执行列表中的下⼀个 fixture 做同样的事情。测试完成后,pytest 将逆向遍历 fixture 列表,对于每个 yield 的 fixture ,运行 yield语句之后的代码。
  • 示例:
  1. import pytest
  2. @pytest.fixture
  3. def open_close():
  4. print("前置操作,初始化.....")
  5. yield
  6. print("后置操作,清理数据.....")
  7. def test_01(open_close):
  8. print("第⼀个测试⽤例")
复制代码
2.png

带参数的fixture
  1. pytest.fixture(scope='', params='', autouse='', ids='', name='')
复制代码

  • scope 参数用于控制fixture的作⽤范围,决定了fixture的⽣命周期。可选值有:

    • function (默认):每个测试函数都会调⽤⼀次fixture。
    • class :在同⼀个测试类中共享这个fixture。
    • module :在同⼀个测试模块中共享这个fixture。(⼀个⽂件⾥)
    • package:每个包执行一次
    • session :整个测试会话中共享这个fixture。

  • autouse 参数默认为 False 。如果设置为 True ,则每个测试函数都会⾃动调⽤该fixture,⽆需显式传⼊
  • params 参数⽤于参数化fixture,⽀持列表传⼊。每个参数值都会使fixture执行⼀次,类似于for循环
  1. @pytest.fixture(params=["张三", "李四", "王五"])
  2. def user(request):  # 固定用 request 内置夹具获取参数
  3.     return request.param
复制代码

  • 效果:用例跑 3 次,分别传入 3 个名字。
  • ids 参数与 params 配合使⽤,为每个参数化实例指定可读的标识符(给参数取名字)
  1. @pytest.fixture(params=["张三", "李四"], ids=["user1", "user2"])
  2. def user(request):
  3.     return request.param
复制代码

  • 效果:运行时显示:test_01[user1]、test_01[user2]
  • name 参数用于为fixture显式设置⼀个名称。如果使用了 name ,则在测试函数中需要使⽤这个名称来引⽤ fixture (给fixture取名字)
  1. @pytest.fixture(name="fix")  # 别名 fix
  2. def fixture_01():
  3.     print("fixture 执行")
  4. def test_01(fix):  # 使用别名
  5.     print("用例执行")
复制代码
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册