前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pytest fixtures使用详解

Pytest fixtures使用详解

作者头像
用户7466307
发布2020-06-16 17:09:56
5540
发布2020-06-16 17:09:56
举报

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

通过将标记为

@ 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
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-03-20,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

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