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

29 篇文章 8 订阅
28 篇文章 13 订阅

目录

1、error和failed区别

2、fixture的实例化顺序

3、使用多个fixture


1、error和failed区别

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

创建test_fixture_failed.py文件

脚本代码:

#!/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。

966274-20211019171244251-448491561.png (832×420)

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

创建test_fixture_error.py文件

脚本代码:

#!/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文件

脚本代码:

#!/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文件

脚本代码:

#!/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。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangmcn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值