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

你。 提交于 2021-01-29 22:37:11

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/57662591/add-a-description-of-what-the-test-does-using-parametrize-of-pytest

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!