pythonassert

What does this code mean: "assert result == repeat, (result, repeat)"?


From here:

from hypothesis import given, strategies as st

@given(seq=st.one_of(st.binary(), st.binary().map(bytearray), st.lists(st.integers())))
def test_idempotent_timsort(seq):
    result = timsort(seq=seq)
    repeat = timsort(seq=result)
    assert result == repeat, (result, repeat)

What does the last assert mean? I understand result == repeat but what's the rest?


Solution

  • The second "argument" to assert is the message printed out if the assertion fails. The idea here is to be able to easily see the expected and actual value for debugging, especially since hypothesis here will generate a whole bunch of tests.

    >>> a = 1
    >>> b = 2
    >>> assert a == b, (a, b)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AssertionError: (1, 2)
    >>>