File: test_appsupport.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (160 lines) | stat: -rw-r--r-- 4,719 bytes parent folder | download | duplicates (3)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import pytest
import sys
import py
import pypy

pytest_plugins = "pytester"

def setpypyconftest(testdir):
    path = str(py.path.local(pypy.__file__).dirpath().dirpath())
    testdir.makeconftest("""
        import sys
        sys.path.insert(0, %r)
        from pypy.conftest import *
    """ % path)

def test_pypy_collection(testdir):
    testdir.makepyfile("""
        def test_func():
            pass
        class TestClassInt:
            def test_method(self, space):
                pass
        class AppTestClass:
            def test_method(self):
                pass
    """)
    testdir.makepyfile(apptest_collection="""
        def test_app():
            pass
    """)
    setpypyconftest(testdir)
    result = testdir.runpytest("--collectonly")
    assert result.ret == 0
    result.stdout.fnmatch_lines([
        "*AppTestModule*apptest_collection*",
        "*AppTestFunction*test_app*",
        "*Function*test_func*",
        "*Class*TestClassInt*",
        "*Function*test_method*",
        "*AppClassCollector*AppTestClass*",
        "*AppTestMethod*",
    ])

def test_interp_spaceconfig(testdir):
    setpypyconftest(testdir)
    p = testdir.makepyfile("""
        class TestClass:
            spaceconfig = {"objspace.usemodules._random": False}
            def setup_class(cls):
                assert not cls.space.config.objspace.usemodules._random
            def test_interp(self, space):
                assert self.space is space
            def test_interp2(self, space):
                assert self.space is space
    """)
    result = testdir.runpytest(p)
    assert result.ret == 0
    result.stdout.fnmatch_lines(["*2 passed*"])

def test_spaceconfig_param(testdir):
    setpypyconftest(testdir)
    p = testdir.makepyfile("""
        import pytest

        @pytest.mark.parametrize('spaceconfig',
            [{"objspace.usemodules._random": False}])
        def test_interp(space):
            assert not space.config.objspace.usemodules._random

        def test_interp2(space):
            assert space.config.objspace.usemodules._random
    """)
    result = testdir.runpytest(p)
    assert result.ret == 0
    result.stdout.fnmatch_lines(["*2 passed*"])

def test_applevel_raises_simple_display(testdir):
    setpypyconftest(testdir)
    p = testdir.makepyfile("""
        class AppTestRaises:
            def test_func(self):
                raises (ValueError, x)
        #
    """)
    result = testdir.runpytest(p, "-s")
    assert result.ret == 1
    result.stdout.fnmatch_lines([
        "*test_func(self)*",
        ">*raises*ValueError*",
        "*E*application-level*NameError*x*not defined",
        "*test_applevel_raises_simple_display*",
    ])
    result = testdir.runpytest(p) # this time we may run the pyc file
    assert result.ret == 1
    result.stdout.fnmatch_lines([
        "*E*application-level*NameError*x*not defined",
    ])

def test_apptest_raise(testdir):
    setpypyconftest(testdir)
    p = testdir.makepyfile(apptest_raise="""
        def test_raise():
            raise KeyError(42)
    """)
    result = testdir.runpytest(p)
    assert result.ret == 1
    result.stdout.fnmatch_lines([
        "*E*application-level*KeyError*42*",
    ])

def test_apptest_fail_plain(testdir):
    setpypyconftest(testdir)
    p = testdir.makepyfile(apptest_fail="""
        def test_fail():
            x = 'foo'
            assert x == 'bar'
    """)
    result = testdir.runpytest(p)
    assert result.ret == 1
    result.stdout.fnmatch_lines([
        "*E*(application-level) AssertionError*",
    ])

def test_apptest_spaceconfig(testdir):
    setpypyconftest(testdir)
    p = testdir.makepyfile(apptest_raise="""
        # spaceconfig = {"usemodules":["array"]}
        import array
        def test_array():
            a = array.array('i', [1,2,3])
            assert len(a) == 3
            assert a[2] == 3
    """)
    result = testdir.runpytest(p)
    assert result.ret == 0

def test_rename_module():
    from pypy.tool.pytest.apptest import _rename_module
    assert _rename_module("sys") == "sys"
    if sys.platform == "win32":
        assert _rename_module("_winreg") == "winreg"
    assert _rename_module("struct") == "_struct"
    assert _rename_module("operator") == "_operator"
    assert _rename_module("signal") == "_signal"

def test_apptest_skipif(testdir):
    setpypyconftest(testdir)
    p = testdir.makepyfile(apptest_raise="""
        import pytest

        @pytest.mark.skipif(True, reason="Bad test")
        def test_bad():
            assert False

        def test_success():
            assert True
    """)
    result = testdir.runpytest(p)
    assert result.ret == 0
    result.assert_outcomes(passed=1, skipped=1)