File: test_appsupport.py

package info (click to toggle)
pypy3 7.0.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 111,848 kB
  • sloc: python: 1,291,746; ansic: 74,281; asm: 5,187; cpp: 3,017; sh: 2,533; makefile: 544; xml: 243; lisp: 45; csh: 21; awk: 4
file content (148 lines) | stat: -rw-r--r-- 4,796 bytes parent folder | download
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
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
    """)
    setpypyconftest(testdir)
    result = testdir.runpytest("--collectonly")
    assert result.ret == 0
    result.stdout.fnmatch_lines([
        "*Function*test_func*",
        "*Class*TestClassInt*",
        "*Function*test_method*",
        "*AppClassCollector*AppTestClass*",
        "*AppTestMethod*",
    ])

class TestSpaceConfig:
    @pytest.mark.xfail(reason="Can't check config with -A in pypy3")
    def test_applevel_skipped_on_cpython_and_spaceconfig(self, testdir):
        setpypyconftest(testdir)
        testdir.makepyfile("""
            class AppTestClass:
                spaceconfig = {"objspace.usemodules._random": True}
                def setup_class(cls):
                    assert 0
                def test_applevel(self):
                    pass
        """)
        result = testdir.runpytest("-A")
        assert result.ret == 0
        if hasattr(sys, 'pypy_translation_info') and \
           sys.pypy_translation_info.get('objspace.usemodules._random'):
            result.stdout.fnmatch_lines(["*1 error*"])
        else:
            # setup_class didn't get called, otherwise it would error
            result.stdout.fnmatch_lines(["*1 skipped*"])

    def test_interp_spaceconfig(self, 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_applevel_raises_simple_display(testdir):
    setpypyconftest(testdir)
    p = testdir.makepyfile("""
        def app_test_raises():
            raises(ValueError, x)
        class AppTestRaises:
            def test_func(self):
                raises (ValueError, x)
        #
    """)
    result = testdir.runpytest(p, "-s")
    assert result.ret == 1
    result.stdout.fnmatch_lines([
        "*E*application-level*NameError*x*not defined",
        "*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_applevel_raises_display(testdir):
    setpypyconftest(testdir)
    p = testdir.makepyfile("""
        def app_test_raises():
            raises(ValueError, "x")
            pass
    """)
    result = testdir.runpytest(p, "-s")
    assert result.ret == 1
    result.stdout.fnmatch_lines([
        "*E*application-level*NameError*x*not defined",
    ])
    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_applevel_raise_keyerror(testdir):
    setpypyconftest(testdir)
    p = testdir.makepyfile("""
        def app_test_raises():
            raise KeyError(42)
            pass
    """)
    result = testdir.runpytest(p, "-s")
    assert result.ret == 1
    result.stdout.fnmatch_lines([
        "*E*application-level*KeyError*42*",
    ])

def app_test_raises():
    info = raises(TypeError, id)
    assert info.type is TypeError
    assert isinstance(info.value, TypeError)

    x = 43
    info = raises(ZeroDivisionError, "x/0")
    assert info.type is ZeroDivisionError
    assert isinstance(info.value, ZeroDivisionError)

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"