首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python测试框架pytest(05)fixture - error和failed、fixture实例化、多个fixture

Python测试框架pytest(05)fixture - error和failed、fixture实例化、多个fixture

作者头像
wangmcn
发布2022-07-26 14:15:03
发布2022-07-26 14:15:03
40200
代码可运行
举报
文章被收录于专栏:AllTests软件测试AllTests软件测试
运行总次数:0
代码可运行

Python测试框架pytest(05)

fixture

error和failed、fixture实例化、多个fixture

目录

  • 1、error和failed区别
  • 2、fixture的实例化顺序
  • 3、使用多个fixture

1、error和failed区别

1、在测试用例里面断言失败,结果为failed。

创建test_fixture_failed.py文件

脚本代码:

代码语言:javascript
代码运行次数:0
运行
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

@pytest.fixture()
def user():
    print("用户名")
    name = "AllTests软件测试"
    return name

def test_case(user):
    assert user == "软件测试"  # 测试用例失败结果为failed

if __name__ == "__main__":
    pytest.main(["-s", "test_fixture_failed.py"])

运行结果:

测试用例失败结果为failed。

2、在fixture里面断言失败,结果为error。

创建test_fixture_error.py文件

脚本代码:

代码语言:javascript
代码运行次数:0
运行
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""

import pytest

@pytest.fixture()
def user():
    print("用户名")
    name = "AllTests软件测试"
    assert name == "软件测试"  # fixture失败结果为error
    return name

def test_case(user):
    assert user == "AllTests软件测试"

if __name__ == "__main__":
    pytest.main(["-s", "test_fixture_error.py"])

运行结果:

fixture失败结果为error。

2、fixture的实例化顺序

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

1、创建test_fixture2.py文件

脚本代码:

代码语言:javascript
代码运行次数:0
运行
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytest

order = []

@pytest.fixture(scope="session")
def s1():
    order.append("s1")

@pytest.fixture(scope="package")
def p1():
    order.append("p1")

@pytest.fixture(scope="module")
def m1():
    order.append("m1")

@pytest.fixture(scope="class")
def c1():
    order.append("c1")

@pytest.fixture(scope="function")
def f0():
    order.append("f0")

@pytest.fixture
def f1(f3, a1):
    # 先实例化f3, 再实例化a1, 最后实例化f1
    order.append("f1")
    assert f3 == 123

@pytest.fixture
def f3():
    order.append("f3")
    a = 123
    yield a

@pytest.fixture
def a1():
    order.append("a1")

@pytest.fixture
def f2():
    order.append("f2")

def test_order(f1, c1, m1, f0, f2, s1, p1):
    # 按scope的优先级,按顺序执行s1,p1,m1,c1,f1(优先执行f3,之后a1,最后f1),f0,f2
    assert order == ["s1", "p1", "m1", "c1", "f3", "a1", "f1", "f0", "f2"]

2、运行结果:断言成功

按 scope 的优先级,按顺序执行 s1,p1,m1,c1,f1(优先执行f3,之后a1,最后f1),f0,f2

3、使用多个fixture

1、创建test_fixture_2.py文件

脚本代码:

代码语言:javascript
代码运行次数:0
运行
复制
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
微信公众号:AllTests软件测试
"""
import pytest

@pytest.fixture()
def fixture_one():
    print("====fixture_one====")

@pytest.fixture()
def fixture_two():
    print("====fixture_two====")

def test_case(fixture_two, fixture_one):
    print("====执行用例====")

2、运行结果:

test_case函数执行前按顺序先执行fixture_two,之后执行fixture_one。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-07-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AllTests软件测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档