diff --git a/doc/example/assertion/failure_demo.py b/doc/example/assertion/failure_demo.py index 7817ed123..c265bd4e8 100644 --- a/doc/example/assertion/failure_demo.py +++ b/doc/example/assertion/failure_demo.py @@ -159,5 +159,35 @@ class TestSpecialisedExplanations(object): assert 1 in [0, 2, 3, 4, 5] +def test_attribute(): + class Foo(object): + b = 1 + i = Foo() + assert i.b == 2 + + +def test_attribute_instance(): + class Foo(object): + b = 1 + assert Foo().b == 2 + + +def test_attribute_failure(): + class Foo(object): + def _get_b(self): + raise Exception('Failed to get attrib') + b = property(_get_b) + i = Foo() + assert i.b == 2 + + +def test_attribute_multiple(): + class Foo(object): + b = 1 + class Bar(object): + b = 2 + assert Foo().b == Bar().b + + def globf(x): return x+1 diff --git a/doc/example/assertion/test_failures.py b/doc/example/assertion/test_failures.py index e925d6c1f..336d5b586 100644 --- a/doc/example/assertion/test_failures.py +++ b/doc/example/assertion/test_failures.py @@ -10,6 +10,6 @@ def test_failure_demo_fails_properly(testdir): failure_demo.copy(testdir.tmpdir.join(failure_demo.basename)) result = testdir.runpytest(target) result.stdout.fnmatch_lines([ - "*31 failed*" + "*35 failed*" ]) assert result.ret != 0 diff --git a/py/_code/_assertionnew.py b/py/_code/_assertionnew.py index c3d8df6aa..6a930d55c 100644 --- a/py/_code/_assertionnew.py +++ b/py/_code/_assertionnew.py @@ -298,6 +298,9 @@ class DebugInterpreter(ast.NodeVisitor): result = self.frame.eval(co, __exprinfo_expr=source_result) except Exception: raise Failure(explanation) + explanation = "%s\n{%s = %s.%s\n}" % (self.frame.repr(result), + self.frame.repr(result), + source_explanation, attr.attr) # Check if the attr is from an instance. source = "%r in getattr(__exprinfo_expr, '__dict__', {})" source = source % (attr.attr,) diff --git a/testing/code/test_assertion.py b/testing/code/test_assertion.py index 4904f7fb1..2e8f4d644 100644 --- a/testing/code/test_assertion.py +++ b/testing/code/test_assertion.py @@ -81,6 +81,27 @@ def test_is(): s = str(e) assert s.startswith("assert 1 is 2") +def test_attrib(): + class Foo(object): + b = 1 + i = Foo() + try: + assert i.b == 2 + except AssertionError: + e = exvalue() + s = str(e) + assert s.startswith("assert 1 == 2") + +def test_attrib_inst(): + class Foo(object): + b = 1 + try: + assert Foo().b == 2 + except AssertionError: + e = exvalue() + s = str(e) + assert s.startswith("assert 1 == 2") + def test_assert_non_string_message(): class A: def __str__(self):