pytest框架(2)fixture、conftest、hook、pytest.ini、allure

本文详细介绍了pytest框架的使用,包括fixture的定义和应用,conftest.py文件的作用,pytest插件的hook函数及其执行顺序,pytest.ini配置文件的设置,以及allure报告的安装、特性和使用方法。通过示例展示了如何使用allure进行详细的测试报告记录和管理。
摘要由CSDN通过智能技术生成

Fixture

pytest fixture 官网:
https://docs.pytest.org/en/stable/fixture.html#fixture

Fixture是在测试函数运行前后,由pytest执行的外壳函数,代码可以定制,满足多变的测试需求,
功能包括:

  • 定义传入测试中的数据集
  • 配置测试前系统的初始状态
  • 为批量测试提供数据源等

pytest fixture 使用
方法1:直接通过函数名,作为参数传递到方法中,有返回值

import pytest

@pytest.fixture
def login():
    print("login....")
    return "login success"
    
def test_add_cart2(login):
    print(login)
    print("添加购物车")

方法2:使用 @pytest.mark.usefixtures(‘login’),测试用例前加上装饰器,没有返回值

import pytest
@pytest.fixture
def login():
    print("login....")
    return "login success"
    
@pytest.mark.usefixtures("login")
def test_add_cart():
    print("添加购物车")

pytest fixture参数

  • autouse 自动应用。默认为False,设置为True自动应用。
  • scope指定作用域(session>module>class>function)
  • setup-show 回溯 fixture 的执行过程
import pytest

@pytest.fixture(autouse=True)
def login():
    print("login....")
    return "login success"

@pytest.fixture(scope="class")
def goto_main():
    print("goto mian page...")

@pytest.fixture(scope="function")
def login_out():
    print("login out....")

@pytest.mark.usefixtures("goto_main")
class TestFixture:
    def test_01(self):
        print("test_01")

    def test_02(self):
        print("test_02")

    def test_03(self,login_out):
        print("test_03")

运行结果
在这里插入图片描述

test_fixture.py --setup-show

在这里插入图片描述

conftest.py

  • conftest.py文件名固定
  • conftest.py 文件就近生效
存放fixture

conftest.py

#conftest.py
import pytest
@pytest.fixture(scope="function")
def setup_and_teardown():
    print("计算开始".center(30,"*"))
    yield "计算中..."
    print("计算结束".center(30,"*"))

test_add.py

#test_add.py
import pytest
def add(a,b):
    return a + b

class TestAdd:
    @pytest.mark.parametrize('a,b,expect',[[0.1,0.2,0.3],]
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当我们使用pytest进行测试时,fixture和conftest是非常有用的工具。 Fixture是pytest中的一个装饰器,用于在测试用例执行前后做一些准备和收尾工作。比如说,在测试用例中需要使用数据库,那么我们可以使用fixture在测试用例执行前连接数据库,在测试用例执行后断开连接。这样可以避免在每个测试用例中都重复写相同的代码。 下面是一个使用fixture的例子: ```python import pytest @pytest.fixture def setup_database(): # 连接数据库 yield # 断开连接 ``` 在这个例子中,我们定义了一个名为setup_database的fixture,这个fixture在测试用例执行前连接数据库,在测试用例执行后断开连接。 conftestpytest提供的一个特殊的文件,用于存放fixture。在一个项目中,如果有多个测试用例需要使用同一个fixture,那么我们可以将这个fixture放到conftest.py中,这样所有的测试用例都可以使用这个fixture了。 下面是一个使用conftest的例子: 在conftest.py中定义一个setup_database的fixture: ```python import pytest @pytest.fixture def setup_database(): # 连接数据库 yield # 断开连接 ``` 在测试用例中使用这个fixture: ```python def test_something(setup_database): # 使用数据库进行测试 ``` 这样,我们就可以在测试用例中使用setup_database这个fixture了,不需要重复写连接和断开数据库的代码。 总之,fixture和conftestpytest中非常有用的工具,可以帮助我们在测试用例执行前后做一些准备和收尾工作,避免重复写相同的代码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值