In depth interpretation of Pytest official documents (26) custom Pytest assertion error information

catalogue

text

For example, write the following test cases

def test_01():
    assert 1==1

def test_02():
    assert 1==2

def test_03():
    assert "1"==1

The results are as follows:

$ pytest
============================== test session starts ===============================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\summer_ospp\summer_ospp_autotest\function_tests, configfile: pytest.ini
, testpaths: tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailure
s-10.1, xdist-2.3.0
collected 3 items                                                                 

tests/test_demo.py::test_01 PASSED                                          [ 33%]
tests/test_demo.py::test_02 FAILED                                          [ 66%]
tests/test_demo.py::test_03 FAILED                                          [100%]

==================================== FAILURES ====================================
____________________________________ test_02 _____________________________________

    def test_02():
>       assert 1==2
E       assert 1 == 2
E         +1
E         -2

tests\test_demo.py:7: AssertionError
____________________________________ test_03 _____________________________________

    def test_03():
>       assert "1"==1
E       AssertionError: assert '1' == 1
E         +'1'
E         -1

tests\test_demo.py:10: AssertionError
============================ short test summary info =============================
FAILED tests/test_demo.py::test_02 - assert 1 == 2
FAILED tests/test_demo.py::test_03 - AssertionError: assert '1' == 1
========================== 2 failed, 1 passed in 0.18s ===========================

For many testers, they may be used to reading Chinese descriptions or more accurate Chinese descriptions. When they see the above error reporting, although it is clear from the error reporting description, it is found from many years of automatic testing experience that many testers or testers who write automatic test scripts, When debugging or executing automated use cases, they are still at a loss when they see the above error reports. They prefer to see the Chinese description, or even a more humanized Chinese description. Of course, the assertion of pytest also provides a way to customize the error information in the script, such as the following methods, but it is found in the actual test development, Although this method gives automatic script developers the right to define themselves, in the process of script development, if each assertion adds assertion error information in the following way, it takes up a lot of time and feels inconvenient. Even many assertion error information are similar, such as judging whether the two variables are equal, in fact, the assertion information is similar, In the script, each assertion is added with assertion error information, which will appear very redundant. Therefore, the other is in confitest The processing method of rewriting assertion error information in py is very easy to use

def test_03():
    assert "1"==1,f"expect '1'Equal to 1, actually not equal"

In the root directory of the test case The following code is written in py: the error information of = = operator is rewritten here. For example, first judge whether it is a type. If it is not a type, it will directly prompt that the two data are not of the same type. If it is a type, it is comparing the values, and the error information is described in Chinese

def pytest_assertrepr_compare(op, left, right):
    if op == "==":
        if not isinstance(right,type(left)):
            return [
                f"Assertion error. The expected value is inconsistent with the actual value. The expected value data type is:{type(left)}, The actual value is:{type(right)}",
                f"The expected value is:{left}, The actual value is:{right}",
            ]
        else:
            return [
                f"Assertion error. The expected value is inconsistent with the actual value. The expected value is:{left}, The actual value is:{right}",
            ]

At this time, write the following test cases. The assertion also adopts the following simple method, that is, do not add the error information yourself

def test_01():
    assert 1==1

def test_02():
    assert 1==2

def test_03():
    assert "1"==1

The execution results are as follows: it can be seen that although there is no user-defined error reporting information in the use case, the error reporting information described in Chinese is still printed in the execution result. For such Chinese error reporting information, I believe that any tester can know why the error is reported and will not be confused by the full screen English error reporting

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\summer_ospp\summer_ospp_autotest\function_tests, configfile: pytest.ini, testpaths: tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 3 items                                                                                                                                                       

tests/test_demo.py::test_01 PASSED                                                                                                                                [ 33%]
tests/test_demo.py::test_02 FAILED                                                                                                                                [ 66%]
tests/test_demo.py::test_03 FAILED                                                                                                                                [100%]

=============================================================================== FAILURES ===============================================================================
_______________________________________________________________________________ test_02 ________________________________________________________________________________

    def test_02():
>       assert 1==2
E       assert Assertion error, expected value is inconsistent with actual value, expected value: 1, actual value: 2

tests\test_demo.py:7: AssertionError
_______________________________________________________________________________ test_03 ________________________________________________________________________________

    def test_03():
>       assert "1"==1
E       AssertionError: assert Assertion error. The expected value is inconsistent with the actual value. The expected value data type is:<class 'str'>, The actual value is:<class 'int'>
E         Expected value: 1, actual value: 1

tests\test_demo.py:10: AssertionError
======================================================================= short test summary info ========================================================================
FAILED tests/test_demo.py::test_02 - assert Assertion error. The expected value is inconsistent with the actual value. The expected value is: 1 and the actual value is: 2
FAILED tests/test_demo.py::test_03 - AssertionError: assert Assertion error. The expected value is inconsistent with the actual value. The expected value data type is:<class 'str'>, The actual value is:<class 'int'>
===================================================================== 2 failed, 1 passed in 0.18s ======================================================================

As follows, the assertion error information of = =, in and not in is rewritten. Other assertions, such as >, <, > =, < == The operator rewriting method is similar to the operator rewriting method. It is not listed one by one here. Conf test Py, write the following code:

def pytest_assertrepr_compare(op, left, right):
    if op == "==":
        if not isinstance(right,type(left)):
            return [
                f"Assertion error. The expected value is inconsistent with the actual value. The expected value data type is:{type(left)}, The actual value is:{type(right)}",
                f"The expected value is:{left}, Actual value:{right}",
            ]
        else:
            return [
                f"Assertion error. The expected value is inconsistent with the actual value. The expected value is:{left}, The actual value is:{right}",
            ]
    if op == "in":
        if isinstance(left,str) and isinstance(right,str):
            return [
                f"expect {left} yes {right} Substring of, actual {left} no {right} Substring of,"
            ]

        elif isinstance(right,list) or isinstance(right,set) or isinstance(right,tuple):
            return [
                f"expect {left} It's a collection {right} An element in the actual collection {right} Not in {left} element"
            ]
        elif isinstance(right,dict):
            return [
                f"expect {left} It's a dictionary {right} One of key,Actual dictionary {right} No value in is {left} of key"
            ]
        else:
            return [
                f"expect {left} yes {right} Part of it, actually {left} Not at all {right} Part of"
            ]
    if op == "not in":
        if isinstance(left, str) and isinstance(right, str):
            return [
                f"expect {left} no {right} Substring of, actual {left} yes {right} Substring of,"
            ]

        elif isinstance(right, list) or isinstance(right, set) or isinstance(right, tuple):
            return [
                f"expect {left} Not a collection {right} An element in the actual collection {right} Zhongyou {left} element"
            ]
        elif isinstance(right, dict):
            return [
                f"expect {left} Not a dictionary {right} One of key,Actual dictionary{right}The value in is {left} of key"
            ]
        else:
            return [
                f"expect {left} no {right} Part of it, actually {left} yes {right} Part of"
            ]

The following is an example script

def test_01():
    assert 1==1

def test_02():
    assert 1==2

def test_03():
    assert "1"==1

def test_04():
    assert "aa" in "bbaa"

def test_05():
    assert "aa" in "bba"

def test_06():
    assert "aa" in ["aa","bb"]

def test_07():
    assert "aa" in ("aa","bb")

def test_08():
    assert "aa" in {"aa","bb"}


def test_09():
    assert "ab" in ["aa", "bb"]


def test_10():
    assert "ab" in ("aa", "bb")


def test_11():
    assert "ab" in {"aa", "bb"}

def test_12():
    assert "name" in {"name":"Zhang Sanfeng","age":100}

def test_13():
    assert "gender" in {"name":"Zhang Sanfeng","age":100}

def test_14():
    assert "aa" not in "bbaa"

def test_15():
    assert "aa" not in "bba"

def test_16():
    assert "aa" not in ["aa","bb"]

def test_17():
    assert "aa" not in ("aa","bb")

def test_18():
    assert "aa" not in {"aa","bb"}


def test_19():
    assert "ab" not in ["aa", "bb"]


def test_20():
    assert "ab" not in ("aa", "bb")


def test_21():
    assert "ab" not in {"aa", "bb"}

def test_22():
    assert "name" not in {"name":"Zhang Sanfeng","age":100}

def test_23():
    assert "gender" not in {"name":"Zhang Sanfeng","age":100}

The results are as follows:

$ pytest
========================================================================= test session starts ==========================================================================
platform win32 -- Python 3.9.7, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\summer_ospp\summer_ospp_autotest\function_tests, configfile: pytest.ini, testpaths: tests
plugins: allure-pytest-2.9.43, caterpillar-pytest-0.0.2, forked-1.3.0, rerunfailures-10.1, xdist-2.3.0
collected 23 items                                                                                                                                                      

tests/test_demo.py::test_01 PASSED                                                                                                                                [  4%]
tests/test_demo.py::test_02 FAILED                                                                                                                                [  8%]
tests/test_demo.py::test_03 FAILED                                                                                                                                [ 13%]
tests/test_demo.py::test_04 PASSED                                                                                                                                [ 17%]
tests/test_demo.py::test_05 FAILED                                                                                                                                [ 21%]
tests/test_demo.py::test_06 PASSED                                                                                                                                [ 26%]
tests/test_demo.py::test_07 PASSED                                                                                                                                [ 30%]
tests/test_demo.py::test_08 PASSED                                                                                                                                [ 34%]
tests/test_demo.py::test_09 FAILED                                                                                                                                [ 39%]
tests/test_demo.py::test_10 FAILED                                                                                                                                [ 43%]
tests/test_demo.py::test_11 FAILED                                                                                                                                [ 47%]
tests/test_demo.py::test_12 PASSED                                                                                                                                [ 52%]
tests/test_demo.py::test_13 FAILED                                                                                                                                [ 56%]
tests/test_demo.py::test_14 FAILED                                                                                                                                [ 60%]
tests/test_demo.py::test_15 PASSED                                                                                                                                [ 65%]
tests/test_demo.py::test_16 FAILED                                                                                                                                [ 69%]
tests/test_demo.py::test_17 FAILED                                                                                                                                [ 73%]
tests/test_demo.py::test_18 FAILED                                                                                                                                [ 78%]
tests/test_demo.py::test_19 PASSED                                                                                                                                [ 82%]
tests/test_demo.py::test_20 PASSED                                                                                                                                [ 86%]
tests/test_demo.py::test_21 PASSED                                                                                                                                [ 91%]
tests/test_demo.py::test_22 FAILED                                                                                                                                [ 95%]
tests/test_demo.py::test_23 PASSED                                                                                                                                [100%]

=============================================================================== FAILURES ===============================================================================
_______________________________________________________________________________ test_02 ________________________________________________________________________________

    def test_02():
>       assert 1==2
E       assert Assertion error. The expected value is inconsistent with the actual value. The expected value is: 1 and the actual value is: 2

tests\test_demo.py:7: AssertionError
_______________________________________________________________________________ test_03 ________________________________________________________________________________

    def test_03():
>       assert "1"==1
E       AssertionError: assert Assertion error. The expected value is inconsistent with the actual value. The expected value data type is:<class 'str'>, The actual value is:<class 'int'>
E         Expected value: 1, actual value: 1

tests\test_demo.py:10: AssertionError
_______________________________________________________________________________ test_05 ________________________________________________________________________________

    def test_05():
>       assert "aa" in "bba"
E       assert expect aa yes bba Substring of, actual aa no bba Substring of,

tests\test_demo.py:16: AssertionError
_______________________________________________________________________________ test_09 ________________________________________________________________________________

    def test_09():
>       assert "ab" in ["aa", "bb"]
E       AssertionError: assert expect ab It's a collection ['aa', 'bb'] An element in the actual collection ['aa', 'bb'] Not in ab element

tests\test_demo.py:29: AssertionError
_______________________________________________________________________________ test_10 ________________________________________________________________________________

    def test_10():
>       assert "ab" in ("aa", "bb")
E       AssertionError: assert expect ab It's a collection ('aa', 'bb') An element in the actual collection ('aa', 'bb') Not in ab element

tests\test_demo.py:33: AssertionError
_______________________________________________________________________________ test_11 ________________________________________________________________________________

    def test_11():
>       assert "ab" in {"aa", "bb"}
E       AssertionError: assert expect ab It's a collection {'aa', 'bb'} An element in the actual collection {'aa', 'bb'} Not in ab element

tests\test_demo.py:37: AssertionError
_______________________________________________________________________________ test_13 ________________________________________________________________________________

    def test_13():
>       assert "gender" in {"name":"Zhang Sanfeng","age":100}
E       AssertionError: assert expect gender It's a dictionary {'name': 'Zhang Sanfeng', 'age': 100} One of key,Actual dictionary {'name': 'Zhang Sanfeng', 'age': 100} No value in is gender of key

tests\test_demo.py:43: AssertionError
_______________________________________________________________________________ test_14 ________________________________________________________________________________

    def test_14():
>       assert "aa" not in "bbaa"
E       assert expect aa no bbaa Substring of, actual aa yes bbaa Substring of,

tests\test_demo.py:46: AssertionError
_______________________________________________________________________________ test_16 ________________________________________________________________________________

    def test_16():
>       assert "aa" not in ["aa","bb"]
E       AssertionError: assert expect aa Not a collection ['aa', 'bb'] An element in the actual collection ['aa', 'bb'] Zhongyou aa element

tests\test_demo.py:52: AssertionError
_______________________________________________________________________________ test_17 ________________________________________________________________________________

    def test_17():
>       assert "aa" not in ("aa","bb")
E       AssertionError: assert expect aa Not a collection ('aa', 'bb') An element in the actual collection ('aa', 'bb') Zhongyou aa element

tests\test_demo.py:55: AssertionError
_______________________________________________________________________________ test_18 ________________________________________________________________________________

    def test_18():
>       assert "aa" not in {"aa","bb"}
E       AssertionError: assert expect aa Not a collection {'aa', 'bb'} An element in the actual collection {'aa', 'bb'} Zhongyou aa element

tests\test_demo.py:58: AssertionError
_______________________________________________________________________________ test_22 ________________________________________________________________________________

    def test_22():
>       assert "name" not in {"name":"Zhang Sanfeng","age":100}
E       AssertionError: assert expect name Not a dictionary {'name': 'Zhang Sanfeng', 'age': 100} One of key,Actual dictionary{'name': 'Zhang Sanfeng', 'age': 100}The value in is name of key

tests\test_demo.py:73: AssertionError
======================================================================= short test summary info ========================================================================
FAILED tests/test_demo.py::test_02 - assert Assertion error. The expected value is inconsistent with the actual value. The expected value is: 1 and the actual value is: 2
FAILED tests/test_demo.py::test_03 - AssertionError: assert Assertion error. The expected value is inconsistent with the actual value. The expected value data type is:<class 'str'>, The actual value is:<class 'int'>
FAILED tests/test_demo.py::test_05 - assert expect aa yes bba Substring of, actual aa no bba Substring of,
FAILED tests/test_demo.py::test_09 - AssertionError: assert expect ab It's a collection ['aa', 'bb'] An element in the actual collection ['aa', 'bb'] Not in ab element
FAILED tests/test_demo.py::test_10 - AssertionError: assert expect ab It's a collection ('aa', 'bb') An element in the actual collection ('aa', 'bb') Not in ab element
FAILED tests/test_demo.py::test_11 - AssertionError: assert expect ab It's a collection {'aa', 'bb'} An element in the actual collection {'aa', 'bb'} Not in ab element
FAILED tests/test_demo.py::test_13 - AssertionError: assert expect gender It's a dictionary {'name': 'Zhang Sanfeng', 'age': 100} One of key,Actual dictionary {'name': 'Zhang Sanfeng', 'age': 100} in...
FAILED tests/test_demo.py::test_14 - assert expect aa no bbaa Substring of, actual aa yes bbaa Substring of,
FAILED tests/test_demo.py::test_16 - AssertionError: assert expect aa Not a collection ['aa', 'bb'] An element in the actual collection ['aa', 'bb'] Zhongyou aa element
FAILED tests/test_demo.py::test_17 - AssertionError: assert expect aa Not a collection ('aa', 'bb') An element in the actual collection ('aa', 'bb') Zhongyou aa element
FAILED tests/test_demo.py::test_18 - AssertionError: assert expect aa Not a collection {'aa', 'bb'} An element in the actual collection {'aa', 'bb'} Zhongyou aa element
FAILED tests/test_demo.py::test_22 - AssertionError: assert expect name Not a dictionary {'name': 'Zhang Sanfeng', 'age': 100} One of key,Actual dictionary{'name': 'Zhang Sanfeng', 'age': 100}Zhongyou...
==================================================================== 12 failed, 11 passed in 0.25s =====================================================================


Keywords: Python Back-end

Added by HuggieBear on Mon, 14 Feb 2022 10:13:42 +0200