testing-cabal / testtools

Testtools - tasteful testing for python
https://testtools.readthedocs.io/en/latest/
Other
95 stars 88 forks source link

Better error message for MatchesListwise #173

Closed jml closed 8 years ago

jml commented 8 years ago

Given this code:

from testtools import TestCase
from testtools.matchers import Equals, MatchesListwise

class FooTests(TestCase):

    def test_foo(self):
        self.assertThat([1], MatchesListwise([Equals(1), Equals(2)]))

def test_suite():
    return FooTests('test_foo')

On master, we would get this result:

$ python -m testtools.run example.test_suite
Tests running...
======================================================================
FAIL: example.FooTests.test_foo
----------------------------------------------------------------------
Traceback (most recent call last):
  File "example.py", line 8, in test_foo
    self.assertThat([1], MatchesListwise([Equals(1), Equals(2)]))
  File "testtools/testcase.py", line 435, in assertThat
    raise mismatch_error
testtools.matchers._impl.MismatchError: Differences: [
len([1]) != 2: Length mismatch
]

Ran 1 test in 0.002s
FAILED (failures=1)

This result doesn't communicate anything about the object being matched.

This patch changes MatchesListwise to use HasLength for its length matching, which turns the error into:

$ python -m testtools.run example.test_suite
Tests running...
======================================================================
FAIL: example.FooTests.test_foo
----------------------------------------------------------------------
Traceback (most recent call last):
  File "example.py", line 8, in test_foo
    self.assertThat([1], MatchesListwise([Equals(1), Equals(2)]))
  File "testtools/testcase.py", line 435, in assertThat
    raise mismatch_error
testtools.matchers._impl.MismatchError: Differences: [
len([1]) != 2: Length mismatch
]

Ran 1 test in 0.001s
FAILED (failures=1)

Note that the traceback now includes the item being matched ([1]).

Review on Reviewable

jml commented 8 years ago

Merging w/ timeout rule.