Python测试框架pytest05fixture - error和failed、fixture实例化、多个fixture

Python测试框架pytest系列可以查看下列
Python测试框架pytest(01)简介、安装、快速入门_编程简单学的博客-CSDN博客

Python测试框架pytest(02)PyCharm设置运行pytest、pytest.main()_编程简单学的博客-CSDN博客
软件测试资料领取方式 (#1) · Issue · 可可爱爱的程序员 / 软件测试资料合集 · GIT CODE
————————————————

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

 

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。

 

相关文章

目录1、前言2、mark的使用(一)注册自定义标记(二)在测试...
用例执行状态用例执行完成后,每条用例都有自己的状态,常见...
什么是conftest.py可以理解成一个专门存放fixture的配置文件...
前言pytest默认执行用例是根据项目下的文件名称按ascii码去收...
前言:什么是元数据?元数据是关于数据的描述,存储着关于数...