当我们想在每个测试方法之前运行一些代码时,将使用夹具。因此,我们定义夹具而不是在每个测试中都重复相同的代码。通常,固定装置用于初始化数据库连接,传递基数等 通过将标记为
通过提及固定装置作为输入参数,测试方法可以使用固定装置。 使用以下代码创建一个新文件test_basic_fixture.py import pytest
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" 这里
每个测试函数都有一个输入自变量,其名称与可用的夹具匹配。然后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 |
|