Home > Software engineering >  Pytest Parametrize fixture not found
Pytest Parametrize fixture not found

Time:12-20

Trying to do test files in PyCharm with pytest and I repeatedly get the "fixture [variable name] not found. All that I could find regarding this issue are cases of misspelling parametrize.


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 

When I run it I get:

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.

Final set of data should fail to pass. (this is intentional)

CodePudding user response:

You must use it as a decorator, i.e. use the @ syntax:

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 

a pytest run will then produce:

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

Note that I've changed the code to use the return value, since the assignment to liste_paie in calculer_paie_employe doesn't change the global variable (because you're missing the global keyword - but using the return value is better practice anyways...)

  • Related