分享

Pytest fixtures使用详解

 软件测试test 2020-05-28

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

通过将标记为

@ pytest.fixture

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

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

import pytest@pytest.fixturedef 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 failedE assert 25 == 35test_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 failedE assert 45 == 35test_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.fixturedef supply_AA_BB_CC(): aa=25 bb =35 cc=45 return [aa,bb,cc]

test_basic_fixture.py

import pytestdef 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 pytestdef 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_file2PASSED test_basic_fixture2.py::test_comparewith

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约