
.. _`tbreportdemo`:
 
Demo of Python failure reports with py.test
==================================================

Here is a nice run of several tens of failures
and how py.test presents things (unfortunately
not showing the nice colors here in the HTML that you
get on the terminal - we are working on that):

.. code-block:: python

    assertion $ py.test failure_demo.py
    =========================== test session starts ============================
    platform linux2 -- Python 2.6.5 -- pytest-2.0.0.dev38
    collecting ...
    collected 35 items
    
    failure_demo.py FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
    
    ================================= FAILURES =================================
    ____________________________ test_generative[0] ____________________________
    
    param1 = 3, param2 = 6
    
        def test_generative(param1, param2):
    >       assert param1 * 2 < param2
    E       assert (3 * 2) < 6
    
    failure_demo.py:15: AssertionError
    _________________________ TestFailing.test_simple __________________________
    
    self = <failure_demo.TestFailing object at 0x2c9da90>
    
        def test_simple(self):
            def f():
                return 42
            def g():
                return 43
        
    >       assert f() == g()
    E       assert 42 == 43
    E        +  where 42 = <function f at 0x2c447d0>()
    E        +  and   43 = <function g at 0x2c44cf8>()
    
    failure_demo.py:28: AssertionError
    ____________________ TestFailing.test_simple_multiline _____________________
    
    self = <failure_demo.TestFailing object at 0x2c9dc90>
    
        def test_simple_multiline(self):
            otherfunc_multi(
                      42,
    >                 6*9)
    
    failure_demo.py:33:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    
    a = 42, b = 54
    
        def otherfunc_multi(a,b):
            assert (a ==
    >               b)
    E       assert 42 == 54
    
    failure_demo.py:12: AssertionError
    ___________________________ TestFailing.test_not ___________________________
    
    self = <failure_demo.TestFailing object at 0x2c93f10>
    
        def test_not(self):
            def f():
                return 42
    >       assert not f()
    E       assert not 42
    E        +  where 42 = <function f at 0x2ca1050>()
    
    failure_demo.py:38: AssertionError
    _________________ TestSpecialisedExplanations.test_eq_text _________________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2c9d9d0>
    
        def test_eq_text(self):
    >       assert 'spam' == 'eggs'
    E       assert 'spam' == 'eggs'
    E         - spam
    E         + eggs
    
    failure_demo.py:42: AssertionError
    _____________ TestSpecialisedExplanations.test_eq_similar_text _____________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2a04e90>
    
        def test_eq_similar_text(self):
    >       assert 'foo 1 bar' == 'foo 2 bar'
    E       assert 'foo 1 bar' == 'foo 2 bar'
    E         - foo 1 bar
    E         ?     ^
    E         + foo 2 bar
    E         ?     ^
    
    failure_demo.py:45: AssertionError
    ____________ TestSpecialisedExplanations.test_eq_multiline_text ____________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2c9d710>
    
        def test_eq_multiline_text(self):
    >       assert 'foo\nspam\nbar' == 'foo\neggs\nbar'
    E       assert 'foo\nspam\nbar' == 'foo\neggs\nbar'
    E           foo
    E         - spam
    E         + eggs
    E           bar
    
    failure_demo.py:48: AssertionError
    ______________ TestSpecialisedExplanations.test_eq_long_text _______________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2c9db10>
    
        def test_eq_long_text(self):
            a = '1'*100 + 'a' + '2'*100
            b = '1'*100 + 'b' + '2'*100
    >       assert a == b
    E       assert '111111111111...2222222222222' == '111111111111...2222222222222'
    E         Skipping 90 identical leading characters in diff
    E         Skipping 91 identical trailing characters in diff
    E         - 1111111111a222222222
    E         ?           ^
    E         + 1111111111b222222222
    E         ?           ^
    
    failure_demo.py:53: AssertionError
    _________ TestSpecialisedExplanations.test_eq_long_text_multiline __________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2caf950>
    
        def test_eq_long_text_multiline(self):
            a = '1\n'*100 + 'a' + '2\n'*100
            b = '1\n'*100 + 'b' + '2\n'*100
    >       assert a == b
    E       assert '1\n1\n1\n1\n...n2\n2\n2\n2\n' == '1\n1\n1\n1\n...n2\n2\n2\n2\n'
    E         Skipping 190 identical leading characters in diff
    E         Skipping 191 identical trailing characters in diff
    E           1
    E           1
    E           1
    E           1
    E           1
    E         - a2
    E         + b2
    E           2
    E           2
    E           2
    E           2
    
    failure_demo.py:58: AssertionError
    _________________ TestSpecialisedExplanations.test_eq_list _________________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2caf590>
    
        def test_eq_list(self):
    >       assert [0, 1, 2] == [0, 1, 3]
    E       assert [0, 1, 2] == [0, 1, 3]
    E         At index 2 diff: 2 != 3
    
    failure_demo.py:61: AssertionError
    ______________ TestSpecialisedExplanations.test_eq_list_long _______________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2c9e310>
    
        def test_eq_list_long(self):
            a = [0]*100 + [1] + [3]*100
            b = [0]*100 + [2] + [3]*100
    >       assert a == b
    E       assert [0, 0, 0, 0, 0, 0, ...] == [0, 0, 0, 0, 0, 0, ...]
    E         At index 100 diff: 1 != 2
    
    failure_demo.py:66: AssertionError
    _________________ TestSpecialisedExplanations.test_eq_dict _________________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2c9dc50>
    
        def test_eq_dict(self):
    >       assert {'a': 0, 'b': 1} == {'a': 0, 'b': 2}
    E       assert {'a': 0, 'b': 1} == {'a': 0, 'b': 2}
    E         - {'a': 0, 'b': 1}
    E         ?               ^
    E         + {'a': 0, 'b': 2}
    E         ?               ^
    
    failure_demo.py:69: AssertionError
    _________________ TestSpecialisedExplanations.test_eq_set __________________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2cafc10>
    
        def test_eq_set(self):
    >       assert set([0, 10, 11, 12]) == set([0, 20, 21])
    E       assert set([0, 10, 11, 12]) == set([0, 20, 21])
    E         Extra items in the left set:
    E         10
    E         11
    E         12
    E         Extra items in the right set:
    E         20
    E         21
    
    failure_demo.py:72: AssertionError
    _____________ TestSpecialisedExplanations.test_eq_longer_list ______________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2cba890>
    
        def test_eq_longer_list(self):
    >       assert [1,2] == [1,2,3]
    E       assert [1, 2] == [1, 2, 3]
    E         Right contains more items, first extra item: 3
    
    failure_demo.py:75: AssertionError
    _________________ TestSpecialisedExplanations.test_in_list _________________
    
    self = <failure_demo.TestSpecialisedExplanations object at 0x2cba6d0>
    
        def test_in_list(self):
    >       assert 1 in [0, 2, 3, 4, 5]
    E       assert 1 in [0, 2, 3, 4, 5]
    
    failure_demo.py:78: AssertionError
    ______________________________ test_attribute ______________________________
    
        def test_attribute():
            class Foo(object):
                b = 1
            i = Foo()
    >       assert i.b == 2
    E       assert 1 == 2
    E        +  where 1 = <failure_demo.Foo object at 0x2c9d750>.b
    
    failure_demo.py:85: AssertionError
    _________________________ test_attribute_instance __________________________
    
        def test_attribute_instance():
            class Foo(object):
                b = 1
    >       assert Foo().b == 2
    E       assert 1 == 2
    E        +  where 1 = <failure_demo.Foo object at 0x2cafdd0>.b
    E        +    where <failure_demo.Foo object at 0x2cafdd0> = <class 'failure_demo.Foo'>()
    
    failure_demo.py:91: AssertionError
    __________________________ test_attribute_failure __________________________
    
        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
    
    failure_demo.py:100:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    
    self = <failure_demo.Foo object at 0x2cba790>
    
        def _get_b(self):
    >       raise Exception('Failed to get attrib')
    E       Exception: Failed to get attrib
    
    failure_demo.py:97: Exception
    _________________________ test_attribute_multiple __________________________
    
        def test_attribute_multiple():
            class Foo(object):
                b = 1
            class Bar(object):
                b = 2
    >       assert Foo().b == Bar().b
    E       assert 1 == 2
    E        +  where 1 = <failure_demo.Foo object at 0x2cba210>.b
    E        +    where <failure_demo.Foo object at 0x2cba210> = <class 'failure_demo.Foo'>()
    E        +  and   2 = <failure_demo.Bar object at 0x2cba850>.b
    E        +    where <failure_demo.Bar object at 0x2cba850> = <class 'failure_demo.Bar'>()
    
    failure_demo.py:108: AssertionError
    __________________________ TestRaises.test_raises __________________________
    
    self = <failure_demo.TestRaises instance at 0x2cc2560>
    
        def test_raises(self):
            s = 'qwe'
    >       raises(TypeError, "int(s)")
    
    failure_demo.py:117:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    
    >   int(s)
    E   ValueError: invalid literal for int() with base 10: 'qwe'
    
    <0-codegen /home/hpk/p/pytest/_pytest/python.py:819>:1: ValueError
    ______________________ TestRaises.test_raises_doesnt _______________________
    
    self = <failure_demo.TestRaises instance at 0x2cb6bd8>
    
        def test_raises_doesnt(self):
    >       raises(IOError, "int('3')")
    E       Failed: DID NOT RAISE
    
    failure_demo.py:120: Failed
    __________________________ TestRaises.test_raise ___________________________
    
    self = <failure_demo.TestRaises instance at 0x2cc4830>
    
        def test_raise(self):
    >       raise ValueError("demo error")
    E       ValueError: demo error
    
    failure_demo.py:123: ValueError
    ________________________ TestRaises.test_tupleerror ________________________
    
    self = <failure_demo.TestRaises instance at 0x2cc5560>
    
        def test_tupleerror(self):
    >       a,b = [1]
    E       ValueError: need more than 1 value to unpack
    
    failure_demo.py:126: ValueError
    ______ TestRaises.test_reinterpret_fails_with_print_for_the_fun_of_it ______
    
    self = <failure_demo.TestRaises instance at 0x2cc6248>
    
        def test_reinterpret_fails_with_print_for_the_fun_of_it(self):
            l = [1,2,3]
            print ("l is %r" % l)
    >       a,b = l.pop()
    E       TypeError: 'int' object is not iterable
    
    failure_demo.py:131: TypeError
    ----------------------------- Captured stdout ------------------------------
    l is [1, 2, 3]
    ________________________ TestRaises.test_some_error ________________________
    
    self = <failure_demo.TestRaises instance at 0x2cc6f38>
    
        def test_some_error(self):
    >       if namenotexi:
    E   NameError: global name 'namenotexi' is not defined
    
    failure_demo.py:134: NameError
    ____________________ test_dynamic_compile_shows_nicely _____________________
    
        def test_dynamic_compile_shows_nicely():
            src = 'def foo():\n assert 1 == 0\n'
            name = 'abc-123'
            module = py.std.imp.new_module(name)
            code = py.code.compile(src, name, 'exec')
            py.builtin.exec_(code, module.__dict__)
            py.std.sys.modules[name] = module
    >       module.foo()
    
    failure_demo.py:149:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    
        def foo():
    >    assert 1 == 0
    E    assert 1 == 0
    
    <2-codegen 'abc-123' /home/hpk/p/pytest/doc/example/assertion/failure_demo.py:146>:2: AssertionError
    ____________________ TestMoreErrors.test_complex_error _____________________
    
    self = <failure_demo.TestMoreErrors instance at 0x2cc4050>
    
        def test_complex_error(self):
            def f():
                return 44
            def g():
                return 43
    >       somefunc(f(), g())
    
    failure_demo.py:159:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    
    x = 44, y = 43
    
        def somefunc(x,y):
    >       otherfunc(x,y)
    
    failure_demo.py:8:
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    
    a = 44, b = 43
    
        def otherfunc(a,b):
    >       assert a==b
    E       assert 44 == 43
    
    failure_demo.py:5: AssertionError
    ___________________ TestMoreErrors.test_z1_unpack_error ____________________
    
    self = <failure_demo.TestMoreErrors instance at 0x2cc7ab8>
    
        def test_z1_unpack_error(self):
            l = []
    >       a,b  = l
    E       ValueError: need more than 0 values to unpack
    
    failure_demo.py:163: ValueError
    ____________________ TestMoreErrors.test_z2_type_error _____________________
    
    self = <failure_demo.TestMoreErrors instance at 0x2ccb8c0>
    
        def test_z2_type_error(self):
            l = 3
    >       a,b  = l
    E       TypeError: 'int' object is not iterable
    
    failure_demo.py:167: TypeError
    ______________________ TestMoreErrors.test_startswith ______________________
    
    self = <failure_demo.TestMoreErrors instance at 0x2ccd5f0>
    
        def test_startswith(self):
            s = "123"
            g = "456"
    >       assert s.startswith(g)
    E       assert <built-in method startswith of str object at 0x2c321b0>('456')
    E        +  where <built-in method startswith of str object at 0x2c321b0> = '123'.startswith
    
    failure_demo.py:172: AssertionError
    __________________ TestMoreErrors.test_startswith_nested ___________________
    
    self = <failure_demo.TestMoreErrors instance at 0x2ccbc20>
    
        def test_startswith_nested(self):
            def f():
                return "123"
            def g():
                return "456"
    >       assert f().startswith(g())
    E       assert <built-in method startswith of str object at 0x2c321b0>('456')
    E        +  where <built-in method startswith of str object at 0x2c321b0> = '123'.startswith
    E        +    where '123' = <function f at 0x2c2d140>()
    E        +  and   '456' = <function g at 0x2cb00c8>()
    
    failure_demo.py:179: AssertionError
    _____________________ TestMoreErrors.test_global_func ______________________
    
    self = <failure_demo.TestMoreErrors instance at 0x2cb69e0>
    
        def test_global_func(self):
    >       assert isinstance(globf(42), float)
    E       assert isinstance(43, float)
    E        +  where 43 = globf(42)
    
    failure_demo.py:182: AssertionError
    _______________________ TestMoreErrors.test_instance _______________________
    
    self = <failure_demo.TestMoreErrors instance at 0x2cc6440>
    
        def test_instance(self):
            self.x = 6*7
    >       assert self.x != 42
    E       assert 42 != 42
    E        +  where 42 = 42
    E        +    where 42 = <failure_demo.TestMoreErrors instance at 0x2cc6440>.x
    
    failure_demo.py:186: AssertionError
    _______________________ TestMoreErrors.test_compare ________________________
    
    self = <failure_demo.TestMoreErrors instance at 0x2dcc200>
    
        def test_compare(self):
    >       assert globf(10) < 5
    E       assert 11 < 5
    E        +  where 11 = globf(10)
    
    failure_demo.py:189: AssertionError
    _____________________ TestMoreErrors.test_try_finally ______________________
    
    self = <failure_demo.TestMoreErrors instance at 0x2dce0e0>
    
        def test_try_finally(self):
            x = 1
            try:
    >           assert x == 0
    E           assert 1 == 0
    
    failure_demo.py:194: AssertionError
    ======================== 35 failed in 0.19 seconds =========================
