Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a description of what the test does using parametrize of Pytest

Tags:

python

pytest

It is possible to add a description in parametrize of what a test is testing for when one of them fails, know quickly why the test is failing.

Sometimes you don't know why a test fails (you have to look in the code). With a description for each test, you could know.

For example.

@pytest.mark.parametrize( "num1, num2, expect", [
    (2, 2, 4), # This test verifies that 2+2 = 4.
])
def test_sum(num1, num2, expect):

    calc = Calc()
    response = calc.sum(num1, num2)
    assert expect == response

If the test failed, the error message would say:

src_test/calc.py::test_sum[19999997-200] FAILED
    assert vo_result.code == expected_code
E   AssertionError: assert 4 == 3
    **The test checks if the numbers add up well.** Message invented

like image 849
RodriKing Avatar asked Oct 17 '25 15:10

RodriKing


1 Answers

You can define id of each test in parameterization. Ids are appended to the test name. By default, the parameterization id is combination of parameters.

@pytest.mark.parametrize( "num1, num2, expect", [
(2, 2, 4)], ids = ["2+2=4"])
def test_sum(num1, num2, expect):
    calc = Calc()
    response = calc.sum(num1, num2)
    assert expect == response

When the test runs, test name would be test_sum[2+2=4]. When the test fails, you can look at test name and find which set of parameters caused the test to fail.

To fail individual tests, you can use pytest.params. e.g.:

@pytest.mark.parametrize( "num1, num2, expect", [
                (2, 2, 4), pytest.param(2,3,9, marks=pytest.mark.xfail)], ids = ["2+2=4", "failing"])
def test_sum(num1, num2, expect):
    calc = Calc()
    response = calc.sum(num1, num2)
    assert expect == response

More about parametrization id from pytest reference doc: https://docs.pytest.org/en/latest/reference.html#pytest-mark-parametrize

like image 83
SilentGuy Avatar answered Oct 20 '25 06:10

SilentGuy