Debian Codegraph @

pypy/tool/pytest/test/test_appsupport.py

raw symbols SCIP
1 import pytest
2 import sys
3 import py
4 import pypy
5
6 pytest_plugins = "pytester"
7
8 def setpypyconftest(testdir):
9 path = str(py.path.local(pypy.__file__).dirpath().dirpath())
10 testdir.makeconftest("""
11 import sys
12 sys.path.insert(0, %r)
13 from pypy.conftest import *
14 """ % path)
15
16 def test_pypy_collection(testdir):
17 testdir.makepyfile("""
18 def test_func():
19 pass
20 class TestClassInt:
21 def test_method(self, space):
22 pass
23 class AppTestClass:
24 def test_method(self):
25 pass
26 """)
27 testdir.makepyfile(apptest_collection="""
28 def test_app():
29 pass
30 """)
31 setpypyconftest(testdir)
32 result = testdir.runpytest("--collectonly")
33 assert result.ret == 0
34 result.stdout.fnmatch_lines([
35 "*AppTestModule*apptest_collection*",
36 "*AppTestFunction*test_app*",
37 "*Function*test_func*",
38 "*Class*TestClassInt*",
39 "*Function*test_method*",
40 "*AppClassCollector*AppTestClass*",
41 "*AppTestMethod*",
42 ])
43
44 def test_interp_spaceconfig(testdir):
45 setpypyconftest(testdir)
46 p = testdir.makepyfile("""
47 class TestClass:
48 spaceconfig = {"objspace.usemodules._random": False}
49 def setup_class(cls):
50 assert not cls.space.config.objspace.usemodules._random
51 def test_interp(self, space):
52 assert self.space is space
53 def test_interp2(self, space):
54 assert self.space is space
55 """)
56 result = testdir.runpytest(p)
57 assert result.ret == 0
58 result.stdout.fnmatch_lines(["*2 passed*"])
59
60 def test_spaceconfig_param(testdir):
61 setpypyconftest(testdir)
62 p = testdir.makepyfile("""
63 import pytest
64
65 @pytest.mark.parametrize('spaceconfig',
66 [{"objspace.usemodules._random": False}])
67 def test_interp(space):
68 assert not space.config.objspace.usemodules._random
69
70 def test_interp2(space):
71 assert space.config.objspace.usemodules._random
72 """)
73 result = testdir.runpytest(p)
74 assert result.ret == 0
75 result.stdout.fnmatch_lines(["*2 passed*"])
76
77 def test_applevel_raises_simple_display(testdir):
78 setpypyconftest(testdir)
79 p = testdir.makepyfile("""
80 class AppTestRaises:
81 def test_func(self):
82 raises (ValueError, x)
83 #
84 """)
85 result = testdir.runpytest(p, "-s")
86 assert result.ret == 1
87 result.stdout.fnmatch_lines([
88 "*test_func(self)*",
89 ">*raises*ValueError*",
90 "*E*application-level*NameError*x*not defined",
91 "*test_applevel_raises_simple_display*",
92 ])
93 result = testdir.runpytest(p) # this time we may run the pyc file
94 assert result.ret == 1
95 result.stdout.fnmatch_lines([
96 "*E*application-level*NameError*x*not defined",
97 ])
98
99 def test_apptest_raise(testdir):
100 setpypyconftest(testdir)
101 p = testdir.makepyfile(apptest_raise="""
102 def test_raise():
103 raise KeyError(42)
104 """)
105 result = testdir.runpytest(p)
106 assert result.ret == 1
107 result.stdout.fnmatch_lines([
108 "*E*application-level*KeyError*42*",
109 ])
110
111 def test_apptest_fail_plain(testdir):
112 setpypyconftest(testdir)
113 p = testdir.makepyfile(apptest_fail="""
114 def test_fail():
115 x = 'foo'
116 assert x == 'bar'
117 """)
118 result = testdir.runpytest(p)
119 assert result.ret == 1
120 result.stdout.fnmatch_lines([
121 "*E*(application-level) AssertionError*",
122 ])
123
124 def test_apptest_spaceconfig(testdir):
125 setpypyconftest(testdir)
126 p = testdir.makepyfile(apptest_raise="""
127 # spaceconfig = {"usemodules":["array"]}
128 import array
129 def test_array():
130 a = array.array('i', [1,2,3])
131 assert len(a) == 3
132 assert a[2] == 3
133 """)
134 result = testdir.runpytest(p)
135 assert result.ret == 0
136
137 def test_rename_module():
138 from pypy.tool.pytest.apptest import _rename_module
139 assert _rename_module("sys") == "sys"
140 if sys.platform == "win32":
141 assert _rename_module("_winreg") == "winreg"
142 assert _rename_module("struct") == "_struct"
143 assert _rename_module("operator") == "_operator"
144 assert _rename_module("signal") == "_signal"
145
146 def test_apptest_skipif(testdir):
147 setpypyconftest(testdir)
148 p = testdir.makepyfile(apptest_raise="""
149 import pytest
150
151 @pytest.mark.skipif(True, reason="Bad test")
152 def test_bad():
153 assert False
154
155 def test_success():
156 assert True
157 """)
158 result = testdir.runpytest(p)
159 assert result.ret == 0
160 result.assert_outcomes(passed=1, skipped=1)