【问题标题】:Pytest Parametrize fixture not found找不到 Pytest 参数化夹具
【发布时间】:2022-01-21 16:38:57
【问题描述】:

尝试在 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 

当我运行它时,我得到:

测试设置失败 E 夹具 'var1' 未找到 可用的固定装置:缓存、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 使用 'pytest --fixtures [testpath]' 获取帮助。

最后一组数据应该无法通过。 (这是故意的)

【问题讨论】:

    标签: python pycharm pytest


    【解决方案1】:

    您必须将其用作装饰器,即使用@ 语法:

    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 ==========================================
    

    请注意,我已更改代码以使用返回值,因为在 calculer_paie_employe 中对 liste_paie 的赋值不会更改全局变量(因为您缺少 global 关键字 - 但使用无论如何,返回值是更好的做法...)

    【讨论】:

      猜你喜欢
      • 2023-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 1970-01-01
      • 2022-12-08
      • 2020-12-17
      相关资源
      最近更新 更多