fixture 可以让我们自定义测试用例的前置条件

1、fixture 的优势

  • 命名方式灵活,不局限于 setup 和teardown 这几个命名
  • conftest.py 配置里可以实现数据共享,不需要 import 就能自动找到fixture
  • scope=”module” 可以实现多个.py 跨文件共享前置
  • scope=”session” 以实现多个.py 跨文件使用一个 session 来完成多个用例

    2、fixture 参数列表

    1. @pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
    2. def test():
    3. print("fixture初始化的参数列表")

    2.1 参数列表

    ● scope:可以理解成 fixture 的作用域,默认:function,还有 class、module、package、session 四个【常用】
    ● autouse:默认:False,需要用例手动调用该 fixture;如果是 True,所有作用域内的测试用例都会自动调用该 fixture
    ● name:默认:装饰器的名称,同一模块的 fixture 相互调用建议写个不同的 name

2.2 注意

session 的作用域:是整个测试会话,即开始执行 Pytest 到结束测试

3、测试用例如何调用 fixture

三种方式:

①将 fixture 名称作为测试用例函数的输入参数 ②测试用例加上装饰器:@pytest.mark.usefixtures(fixture_name) ③fixture 设置 autouse=True

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import pytest
  4. # 调用方式一
  5. @pytest.fixture
  6. def login():
  7. print("输入账号,密码先登录")
  8. def test_s1(login):
  9. print("用例 1:登录之后其它动作 111")
  10. def test_s2(): # 不传 login
  11. print("用例 2:不需要登录,操作 222")
  12. # 调用方式二
  13. @pytest.fixture
  14. def login2():
  15. print("please输入账号,密码先登录")
  16. @pytest.mark.usefixtures("login2", "login")
  17. def test_s11():
  18. print("用例 11:登录之后其它动作 111")
  19. # 调用方式三
  20. @pytest.fixture(autouse=True)
  21. def login3():
  22. print("====auto===")
  23. # 不是test开头,加了装饰器也不会执行fixture
  24. @pytest.mark.usefixtures("login2")
  25. def loginss():
  26. print(123)

3.1 执行结果

image.png

3.2 知识点

● 在类声明上面加@pytest.mark.usefixtures(),代表这个类里面所有测试用例都会调用该 fixture
● 可以叠加多个@pytest.mark.usefixtures(),先执行的放底层,后执行的放上层
● 可以传多个 fixture 参数,先执行的放前面,后执行的放后面
● 如果 fixture 有返回值,用@pytest.mark.usefixtures()是无法获取到返回值的,必须用传参的方式(方式一)

4、fixture 的实例化顺序

● 较高 scope 范围的 fixture(session)在较低 scope 范围的 fixture( function 、 class )之前实例化【session > package > module > class > function】
● 具有相同作用域的 fixture 遵循测试函数中声明的顺序,并遵循 fixture 之间的依赖关系【在 fixture_A 里面依赖的 fixture_B 优先实例化,然后到 fixture_A 实例化】
● 自动使用(autouse=True)的 fixture 将在显式使用(传参或装饰器)的 fixture 之前实例化

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import pytest
  4. order = []
  5. @pytest.fixture(scope="session")
  6. def s1():
  7. order.append("s1")
  8. @pytest.fixture(scope="module")
  9. def m1():
  10. order.append("m1")
  11. @pytest.fixture
  12. def f1(f3, a1):
  13. # 先实例化f3, 再实例化a1, 最后实例化f1
  14. order.append("f1")
  15. assert f3 == 123
  16. @pytest.fixture
  17. def f3():
  18. order.append("f3")
  19. a = 123
  20. yield a
  21. @pytest.fixture
  22. def a1():
  23. order.append("a1")
  24. @pytest.fixture
  25. def f2():
  26. order.append("f2")
  27. def test_order(f1, m1, f2, s1):
  28. # m1、s1在f1后,但因为scope范围大,所以会优先实例化
  29. assert order == ["s1", "m1", "f3", "a1", "f1", "f2"]

image.png

5、关于 fixture 的注意点

添加了 @pytest.fixture ,如果 fixture 还想依赖其他 fixture,需要用函数传参的方式,不能用@pytest.mark.usefixtures()的方式,否则会不生效

  1. @pytest.fixture(scope="session")
  2. def open():
  3. print("===打开浏览器===")
  4. @pytest.fixture
  5. # @pytest.mark.usefixtures("open") 不可取!!!不生效!!!
  6. def login(open):
  7. # 方法级别前置操作setup
  8. print(f"输入账号,密码先登录{open}")

6、fixture 之 yield 实现 teardown

用 fixture 实现 teardown 并不是一个独立的函数,而是用 yield 关键字来开启 teardown 操作

  1. import pytest
  2. @pytest.fixture(scope="session")
  3. def open():
  4. # 会话前置操作setup
  5. print("===打开浏览器===")
  6. test = "测试变量是否返回"
  7. yield test
  8. # 会话后置操作teardown
  9. print("==关闭浏览器==")
  10. @pytest.fixture
  11. def login(open):
  12. # 方法级别前置操作setup
  13. print(f"输入账号,密码先登录{open}")
  14. name = "==我是账号=="
  15. pwd = "==我是密码=="
  16. age = "==我是年龄=="
  17. # 返回变量
  18. yield name, pwd, age
  19. # 方法级别后置操作teardown
  20. print("登录成功")
  21. def test_s1(login):
  22. print("==用例1==")
  23. # 返回的是一个元组
  24. print(login)
  25. # 分别赋值给不同变量
  26. name, pwd, age = login
  27. print(name, pwd, age)
  28. assert "账号" in name
  29. assert "密码" in pwd
  30. assert "年龄" in age
  31. def test_s2(login):
  32. print("==用例2==")
  33. print(login)

● 如果 yield 前面的代码,即 setup 部分已经抛出异常了,则不会执行 yield 后面的 teardown 内容
● 如果测试用例抛出异常,yield 后面的 teardown 内容还是会正常执行

7、yield + with 的结合

  1. # 官方例子
  2. @pytest.fixture(scope="module")
  3. def smtp_connection():
  4. with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp_connection:
  5. yield smtp_connection # provide the fixture value
  6. # 该smtp_connection 连接将测试完成执行后已经关闭,因为 smtp_connection 对象自动关闭时, with 语句结束

8、addfinalizer 终结函数

  1. @pytest.fixture(scope="module")
  2. def test_addfinalizer(request):
  3. # 前置操作setup
  4. print("==再次打开浏览器==")
  5. test = "test_addfinalizer"
  6. def fin():
  7. # 后置操作teardown
  8. print("==再次关闭浏览器==")
  9. request.addfinalizer(fin)
  10. # 返回前置操作的变量
  11. return test
  12. def test_anthor(test_addfinalizer):
  13. print("==最新用例==", test_addfinalizer)

注意事项
● 如果 request.addfinalizer() 前面的代码,即 setup 部分已经抛出异常了,则不会执行 request.addfinalizer() 的 teardown 内容(和 yield 相似,应该是最近新版本改成一致了)
● 可以声明多个终结函数并调用