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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
# -*- coding: utf-8 -*-
pytest_plugins = "pytester"
import pytest
class Option(object):
def __init__(self, verbose=False, quiet=False, n=None):
self.verbose = verbose
self.quiet = quiet
self.n = None
@property
def args(self):
l = ['--instafail']
if self.verbose:
l.append('-v')
if self.quiet:
l.append('-q')
if self.n is not None:
self.args.extend(['-n', str(self.n)])
return l
@pytest.fixture(params=['normal', '1 worker', '2 worker'])
def n(request):
return {
'normal': None,
'1 worker': 1,
'2 worker': 2,
}[request.param]
@pytest.fixture(params=['default', 'verbose', 'quiet'])
def option(request, n):
return {
"default": Option(verbose=False, n=n),
"verbose": Option(verbose=True, n=n),
"quiet": Option(quiet=True, n=n),
}[request.param]
class TestInstafailingTerminalReporter(object):
def test_fail(self, testdir, option):
testdir.makepyfile(
"""
import pytest
def test_func():
assert 0
"""
)
result = testdir.runpytest(*option.args)
if option.verbose:
result.stdout.fnmatch_lines([
"*test_fail.py:*:*test_func*FAIL*",
"* test_func *",
" def test_func():",
"> assert 0",
"E assert 0",
])
elif option.quiet:
result.stdout.fnmatch_lines([
"F",
"* test_func *",
" def test_func():",
"> assert 0",
"E assert 0",
])
else:
result.stdout.fnmatch_lines([
"*test_fail.py F",
"* test_func *",
" def test_func():",
"> assert 0",
"E assert 0",
])
def test_fail_fail(self, testdir, option):
testdir.makepyfile(
"""
import pytest
def test_func():
assert 0
def test_func2():
assert 0
"""
)
result = testdir.runpytest(*option.args)
if option.verbose:
result.stdout.fnmatch_lines([
"*test_fail_fail.py:*:*test_func*FAIL*",
"* test_func *",
" def test_func():",
"> assert 0",
"E assert 0",
"test_fail_fail.py:3: AssertionError",
"",
"*test_fail_fail.py:*:*test_func2*FAIL*",
"* test_func2 *",
" def test_func2():",
"> assert 0",
"E assert 0",
])
elif option.quiet:
result.stdout.fnmatch_lines([
"F",
"* test_func *",
" def test_func():",
"> assert 0",
"E assert 0",
"F",
"* test_func2 *",
" def test_func2():",
"> assert 0",
"E assert 0",
])
else:
result.stdout.fnmatch_lines([
"*test_fail_fail.py F",
"* test_func *",
" def test_func():",
"> assert 0",
"E assert 0",
"*test_fail_fail.py F",
"* test_func2 *",
" def test_func2():",
"> assert 0",
"E assert 0",
])
def test_error_in_setup_then_pass(self, testdir, option):
testdir.makepyfile(
"""
def setup_function(function):
print ("setup func")
if function is test_nada:
assert 0
def test_nada():
pass
def test_zip():
pass
"""
)
result = testdir.runpytest(*option.args)
if option.verbose:
result.stdout.fnmatch_lines([
"*test_error_in_setup_then_pass.py:*:*test_nada*ERROR*",
"*ERROR at setup of test_nada*",
"*setup_function(function):*",
"*setup func*",
"*assert 0*",
"test_error_in_setup_then_pass.py:4: AssertionError",
"",
"*test_error_in_setup_then_pass.py:*:*test_zip*PASSED*",
"*1 error*",
])
elif option.quiet:
result.stdout.fnmatch_lines([
"E",
"*ERROR at setup of test_nada*",
"*setup_function(function):*",
"*setup func*",
"*assert 0*",
"test_error_in_setup_then_pass.py:4: AssertionError",
".*",
])
else:
result.stdout.fnmatch_lines([
"*test_error_in_setup_then_pass.py E",
"*ERROR at setup of test_nada*",
"*setup_function(function):*",
"*setup func*",
"*assert 0*",
"test_error_in_setup_then_pass.py:4: AssertionError",
"",
"*test_error_in_setup_then_pass.py .*",
"*1 error*",
])
assert result.ret != 0
def test_error_in_teardown_then_pass(self, testdir, option):
testdir.makepyfile(
"""
def teardown_function(function):
print ("teardown func")
if function is test_nada:
assert 0
def test_nada():
pass
def test_zip():
pass
"""
)
result = testdir.runpytest(*option.args)
if option.verbose:
result.stdout.fnmatch_lines([
"*test_error_in_teardown_then_pass.py:*:*test_nada*ERROR*",
"*ERROR at teardown of test_nada*",
"*teardown_function(function):*",
"*teardown func*",
"*assert 0*",
"test_error_in_teardown_then_pass.py:4: AssertionError",
"",
"*test_error_in_teardown_then_pass.py:*:*test_zip*PASSED*",
"*1 error*",
])
elif option.quiet:
result.stdout.fnmatch_lines([
".E",
"*ERROR at teardown of test_nada*",
"*teardown_function(function):*",
"*teardown func*",
"*assert 0*",
"test_error_in_teardown_then_pass.py:4: AssertionError",
".*",
])
else:
result.stdout.fnmatch_lines([
"*test_error_in_teardown_then_pass.py .E",
"*ERROR at teardown of test_nada*",
"*teardown_function(function):*",
"*teardown func*",
"*assert 0*",
"test_error_in_teardown_then_pass.py:4: AssertionError",
"",
"*test_error_in_teardown_then_pass.py .*",
"*1 error*",
])
assert result.ret != 0
def test_collect_error(self, testdir, option):
testdir.makepyfile("""raise ValueError(0)""")
result = testdir.runpytest(*option.args)
result.stdout.fnmatch_lines([
"*ERROR collecting test_collect_error.py*",
"test_collect_error.py:1: in <module>",
"* raise ValueError(0)",
"E ValueError: 0",
])
if not option.quiet:
result.stdout.fnmatch_lines([
"collected 0 items / 1 error*",
])
def test_print_stacktrace_once_with_pdb(self, testdir, request, option):
test_file = testdir.makepyfile(
"""
def test_func():
assert 0
"""
)
args = option.args
args.append(" --pdb %s" % test_file)
child = testdir.spawn_pytest(' '.join(args))
child.expect('>+ traceback >+')
assert b'E assert 0' not in child.before
child.expect('(Pdb)')
child.sendeof()
if child.isalive():
child.wait()
def test_xfail_unexpected_success(self, testdir, option):
testdir.makepyfile(
"""
import pytest
@pytest.mark.xfail
def test_func():
pass
"""
)
result = testdir.runpytest(*option.args)
if option.verbose:
result.stdout.fnmatch_lines([
"test_xfail_unexpected_success.py:*:*test_func XPASS*"
])
elif option.quiet:
result.stdout.fnmatch_lines([
"X*"
])
else:
result.stdout.fnmatch_lines([
"test_xfail_unexpected_success.py X*"
])
assert "INTERNALERROR" not in result.stdout.str()
|