sources for test_assertion.py [rev. unknown]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import py
def setup_module(mod):
    py.magic.invoke(assertion=1)
def teardown_module(mod):
    py.magic.revoke(assertion=1)
def f():
    return 2
def test_assert():
    try:
        assert f() == 3
    except AssertionError, e:
        s = str(e)
        assert s.startswith('assert 2 == 3\n')
def test_assert_with_explicit_message():
    try:
        assert f() == 3, "hello"
    except AssertionError, e:
        assert e.msg == 'hello'
def test_assert_within_finally():
    class A:
        def f():
            pass
    excinfo = py.test.raises(TypeError, """
        try:
            A().f()
        finally:
            i = 42
    """)
    s = excinfo.exconly() 
    assert s.find("takes no argument") != -1
    #def g():
    #    A.f()
    #excinfo = getexcinfo(TypeError, g)
    #msg = getmsg(excinfo)
    #assert msg.find("must be called with A") != -1
def test_assert_multiline_1():
    try:
        assert (f() ==
                3)
    except AssertionError, e:
        s = str(e)
        assert s.startswith('assert 2 == 3\n')
def test_assert_multiline_2():
    try:
        assert (f() == (4,
                   3)[-1])
    except AssertionError, e:
        s = str(e)
        assert s.startswith('assert 2 ==')
def test_assert_non_string_message(): 
    class A: 
        def __str__(self): 
            return "hello"
    try:
        assert 0 == 1, A()
    except AssertionError, e: 
        assert e.msg == "hello"
# These tests should both fail, but should fail nicely...
class WeirdRepr:
    def __repr__(self):
        return '<WeirdRepr\nsecond line>'
            
def bug_test_assert_repr():
    v = WeirdRepr()
    try: 
        assert v == 1
    except AssertionError, e: 
        assert e.msg.find('WeirdRepr') != -1
        assert e.msg.find('second line') != -1
        assert 0
        
def test_assert_non_string():
    try: 
        assert 0, ['list']
    except AssertionError, e: 
        assert e.msg.find("list") != -1 
def test_assert_implicit_multiline():
    try:
        x = [1,2,3]
        assert x != [1,
           2, 3]
    except AssertionError, e:
        assert e.msg.find('assert [1, 2, 3] !=') != -1