Pytest fixtures使用详解

当我们想在每个测试方法之前运行一些代码时,将使用夹具。因此,我们定义夹具而不是在每个测试中都重复相同的代码。通常,固定装置用于初始化数据库连接,传递基数等

通过将标记为

@ pytest.fixture

通过提及固定装置作为输入参数,测试方法可以使用固定装置。

使用以下代码创建一个新文件test_basic_fixture.py

import pytest
@pytest.fixture
def supply_AA_BB_CC():
  aa=25
  bb =35
  cc=45
  return [aa,bb,cc]


def test_comparewithAA(supply_AA_BB_CC):
  zz=35
  assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed"


def test_comparewithBB(supply_AA_BB_CC):
  zz=35
  assert supply_AA_BB_CC[1]==zz,"bb and zz comparison failed"


def test_comparewithCC(supply_AA_BB_CC):
  zz=35
  assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"

这里

  • 我们有一个名为supply_AA_BB_CC的fixture。此方法将返回3个值的列表。

  • 我们有3种测试方法与每个值进行比较。

每个测试函数都有一个输入自变量,其名称与可用的夹具匹配。然后Pytest调用相应的fixture方法,返回的值将存储在输入参数中,此处为列表[25,35,45]。现在,将列表项用于测试方法中以进行比较。

现在运行测试并查看结果

 py.test test_basic_fixture
test_basic_fixture.py::test_comparewithAA FAILED                                                                                                                                                                                       
test_basic_fixture.py::test_comparewithBB PASSED                                                                                                                                                                                       
test_basic_fixture.py::test_comparewithCC FAILED
                                                                                                                                                                                       
============================================== FAILURES ==============================================
_________________________________________ test_comparewithAA _________________________________________
supply_AA_BB_CC = [25, 35, 45]
    def test_comparewithAA(supply_AA_BB_CC):
      zz=35
>     assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed"
E    AssertionError: aa and zz comparison failed
E    assert 25 == 35
test_basic_fixture.py:10: AssertionError


_________________________________________ test_comparewithCC _________________________________________
supply_AA_BB_CC = [25, 35, 45]
    def test_comparewithCC(supply_AA_BB_CC):
      zz=35
>     assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"
E    AssertionError: cc and zz comparison failed
E    assert 45 == 35
test_basic_fixture.py:16: AssertionError
================================= 2 failed, 1 passed in 0.05 seconds =================================

由于zz = BB = 35,所以通过了test test_comparewithBB,其余2个测试均失败。

Fixture方法仅在定义的测试文件中具有作用域。如果尝试访问其他测试文件中的fixture ,则会收到一条错误消息,提示未在其他文件中的测试方法中找到灯具“ supply_AA_BB_CC”

要对多个测试文件使用相同的fixture ,我们将在名为conftest.py的文件中创建灯具方法。

让我们通过以下示例进行查看。使用以下代码创建3个文件conftest.py,test_basic_fixture.py,test_basic_fixture2.py

conftest.py

import pytest
@pytest.fixture
def supply_AA_BB_CC():
  aa=25
  bb =35
  cc=45
  return [aa,bb,cc]

test_basic_fixture.py

import pytest
def test_comparewithAA(supply_AA_BB_CC):
  zz=35
  assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed"


def test_comparewithBB(supply_AA_BB_CC):
  zz=35
  assert supply_AA_BB_CC[1]==zz,"bb and zz comparison failed"


def test_comparewithCC(supply_AA_BB_CC):
  zz=35
  assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"

test_basic_fixture2.py

import pytest
def test_comparewithAA_file2(supply_AA_BB_CC):
  zz=25
  assert supply_AA_BB_CC[0]==zz,"aa and zz comparison failed"


def test_comparewithBB_file2(supply_AA_BB_CC):
  zz=25
  assert supply_AA_BB_CC[1]==zz,"bb and zz comparison failed"


def test_comparewithCC_file2(supply_AA_BB_CC):
  zz=25
  assert supply_AA_BB_CC[2]==zz,"cc and zz comparison failed"

pytest将首先在测试文件中查找灯具,如果找不到,它将在conftest.py中查找

通过py.test -k test_comparewith -v运行测试以得到如下结果

test_basic_fixture.py::test_comparewithAA FAILED   
test_basic_fixture.py:::test_comparewithBB FASS LED 
test_basic_fixture.py:::test_comparewithCC FAILED 
test_basic_fixture2.py:::test_comparewithAA_file2 PASSED 
test_basic_fixture2.py::test_comparewithAA_file2:PASSED 
test_basic_fixture2.py::test_comparewith

更多精彩推荐,请关注我们

扫码关注更多精彩

你点的每个赞,我都认真当成了喜欢

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

软件测试test

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

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

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

打赏作者

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

抵扣说明:

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

余额充值