找不到 Pytest 参数化夹具
Pytest Parametrize fixture not found
尝试在 PyCharm 中使用 pytest 测试文件,我反复收到“未找到夹具 [变量名]。关于这个问题,我能找到的都是参数拼写错误的情况。
liste_paie = []
def calculer_paie_employe(tauxh,heures):
total = tauxh * heures
impot = total * 0.20
net = total - impot
liste_paie = [heures, tauxh, total, impot, net]
return liste_paie
pytest.mark.parametrize("var1,var2,expected_1,expected_2,expected_3", [(14.7 , 25,367.5,73.5,294), (20 , 15, 300, 60, 240),
(15.6 , 23.9, 372.84, 75.568, 300)])
def test_calculer_paie_employe(var1,var2, expected_1, expected_2, expected_3):
calculer_paie_employe(var1,var2)
assert liste_paie[2] == expected_1 and liste_paie[3] == expected_2 and liste_paie[4] == expected_3
当我 运行 它时,我得到:
test setup failed
E fixture 'var1' not found
available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
use 'pytest --fixtures [testpath]' for help on them.
最后一组数据应该没有通过。 (这是故意的)
您必须将其用作装饰器,即使用 @
语法:
liste_paie = []
def calculer_paie_employe(tauxh,heures):
total = tauxh * heures
impot = total * 0.20
net = total - impot
liste_paie = [heures, tauxh, total, impot, net]
return liste_paie
import pytest
@pytest.mark.parametrize(
"var1,var2,expected_1,expected_2,expected_3", [
(14.7, 25, 367.5, 73.5, 294),
(20, 15, 300, 60, 240),
(15.6, 23.9, 372.84, 75.568, 300)
])
def test_calculer_paie_employe(var1,var2, expected_1, expected_2, expected_3):
liste_paie = calculer_paie_employe(var1,var2)
assert liste_paie[2] == expected_1 and liste_paie[3] == expected_2 and liste_paie[4] == expected_3
pytest 运行 将生成:
================================================= test session starts =================================================
platform win32 -- Python 3.5.4, pytest-3.10.1, py-1.8.0, pluggy-0.9.0
rootdir: c:\srv\tmp, inifile:
plugins: django-3.10.0, cov-2.6.1
collected 3 items
pytestparm.py ..F [100%]
====================================================== FAILURES =======================================================
_______________________________ test_calculer_paie_employe[15.6-23.9-372.84-75.568-300] _______________________________
var1 = 15.6, var2 = 23.9, expected_1 = 372.84, expected_2 = 75.568, expected_3 = 300
@pytest.mark.parametrize(
"var1,var2,expected_1,expected_2,expected_3", [
(14.7, 25, 367.5, 73.5, 294),
(20, 15, 300, 60, 240),
(15.6, 23.9, 372.84, 75.568, 300)
])
def test_calculer_paie_employe(var1,var2, expected_1, expected_2, expected_3):
liste_paie = calculer_paie_employe(var1,var2)
> assert liste_paie[2] == expected_1 and liste_paie[3] == expected_2 and liste_paie[4] == expected_3
E assert (372.84 == 372.84 and 74.568 == 75.568)
pytestparm.py:19: AssertionError
========================================= 1 failed, 2 passed in 0.04 seconds ==========================================
请注意,我已将代码更改为使用 return 值,因为在 calculer_paie_employe
中对 liste_paie
的赋值不会更改全局变量(因为您缺少 global
关键字 - 但无论如何使用 return 值是更好的做法...)
尝试在 PyCharm 中使用 pytest 测试文件,我反复收到“未找到夹具 [变量名]。关于这个问题,我能找到的都是参数拼写错误的情况。
liste_paie = []
def calculer_paie_employe(tauxh,heures):
total = tauxh * heures
impot = total * 0.20
net = total - impot
liste_paie = [heures, tauxh, total, impot, net]
return liste_paie
pytest.mark.parametrize("var1,var2,expected_1,expected_2,expected_3", [(14.7 , 25,367.5,73.5,294), (20 , 15, 300, 60, 240),
(15.6 , 23.9, 372.84, 75.568, 300)])
def test_calculer_paie_employe(var1,var2, expected_1, expected_2, expected_3):
calculer_paie_employe(var1,var2)
assert liste_paie[2] == expected_1 and liste_paie[3] == expected_2 and liste_paie[4] == expected_3
当我 运行 它时,我得到:
test setup failed E fixture 'var1' not found available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory use 'pytest --fixtures [testpath]' for help on them.
最后一组数据应该没有通过。 (这是故意的)
您必须将其用作装饰器,即使用 @
语法:
liste_paie = []
def calculer_paie_employe(tauxh,heures):
total = tauxh * heures
impot = total * 0.20
net = total - impot
liste_paie = [heures, tauxh, total, impot, net]
return liste_paie
import pytest
@pytest.mark.parametrize(
"var1,var2,expected_1,expected_2,expected_3", [
(14.7, 25, 367.5, 73.5, 294),
(20, 15, 300, 60, 240),
(15.6, 23.9, 372.84, 75.568, 300)
])
def test_calculer_paie_employe(var1,var2, expected_1, expected_2, expected_3):
liste_paie = calculer_paie_employe(var1,var2)
assert liste_paie[2] == expected_1 and liste_paie[3] == expected_2 and liste_paie[4] == expected_3
pytest 运行 将生成:
================================================= test session starts =================================================
platform win32 -- Python 3.5.4, pytest-3.10.1, py-1.8.0, pluggy-0.9.0
rootdir: c:\srv\tmp, inifile:
plugins: django-3.10.0, cov-2.6.1
collected 3 items
pytestparm.py ..F [100%]
====================================================== FAILURES =======================================================
_______________________________ test_calculer_paie_employe[15.6-23.9-372.84-75.568-300] _______________________________
var1 = 15.6, var2 = 23.9, expected_1 = 372.84, expected_2 = 75.568, expected_3 = 300
@pytest.mark.parametrize(
"var1,var2,expected_1,expected_2,expected_3", [
(14.7, 25, 367.5, 73.5, 294),
(20, 15, 300, 60, 240),
(15.6, 23.9, 372.84, 75.568, 300)
])
def test_calculer_paie_employe(var1,var2, expected_1, expected_2, expected_3):
liste_paie = calculer_paie_employe(var1,var2)
> assert liste_paie[2] == expected_1 and liste_paie[3] == expected_2 and liste_paie[4] == expected_3
E assert (372.84 == 372.84 and 74.568 == 75.568)
pytestparm.py:19: AssertionError
========================================= 1 failed, 2 passed in 0.04 seconds ==========================================
请注意,我已将代码更改为使用 return 值,因为在 calculer_paie_employe
中对 liste_paie
的赋值不会更改全局变量(因为您缺少 global
关键字 - 但无论如何使用 return 值是更好的做法...)