Skip to content

Commit 75ac073

Browse files
committed
self.fail(…) → pytest.fail(…)
1 parent 39cdfaa commit 75ac073

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

tests/fixtures/self_assert/fail_in.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# required-method: fail
2+
3+
class TestAssertTrue(TestCase):
4+
def test_me(self):
5+
self.fail(xxx+y)
6+
self.fail(aaa % bbb)
7+
self.fail(ccc or ddd)
8+
9+
def test_everybody(self):
10+
self.fail( 'abc' )
11+
12+
def test_message(self):
13+
self.fail(msg='This is wrong!')
14+
self.fail(error_message)
15+
16+
def test_nothing(self):
17+
self.fail()
18+
self.fail(self.fail())
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# required-method: fail
2+
3+
import pytest
4+
class TestAssertTrue(TestCase):
5+
def test_me(self):
6+
pytest.fail(xxx+y)
7+
pytest.fail(aaa % bbb)
8+
pytest.fail(ccc or ddd)
9+
10+
def test_everybody(self):
11+
pytest.fail( 'abc' )
12+
13+
def test_message(self):
14+
pytest.fail(msg='This is wrong!')
15+
pytest.fail(error_message)
16+
17+
def test_nothing(self):
18+
pytest.fail()
19+
pytest.fail(pytest.fail())

unittest2pytest/fixes/fix_self_assert.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ def RaisesRegexOp(context, designator, exceptionClass, expected_regex,
202202
else:
203203
return Node(syms.suite, [with_stmt])
204204

205+
def FailOp(indent, kws, arglist, node):
206+
new = node.clone()
207+
new.set_child(0, Name('pytest'))
208+
return new
205209

206210
def add_import(import_name, node):
207211
suite = get_parent_of_type(node, syms.suite)
@@ -292,6 +296,8 @@ def get_import_nodes(node):
292296
'assertRaisesRegex': partial(RaisesRegexOp, 'pytest.raises', 'excinfo'),
293297
'assertWarnsRegex': partial(RaisesRegexOp, 'pytest.warns', 'record'),
294298

299+
'fail': FailOp,
300+
295301
#'assertLogs': -- not to be handled here, is an context handler only
296302
}
297303

@@ -378,7 +384,7 @@ class FixSelfAssert(BaseFix):
378384
PATTERN = """
379385
power< 'self'
380386
trailer< '.' method=( %s ) >
381-
trailer< '(' arglist=any ')' >
387+
trailer< '(' [arglist=any] ')' >
382388
>
383389
""" % ' | '.join(map(repr,
384390
(set(_method_map.keys()) | set(_method_aliases.keys()))))
@@ -424,8 +430,10 @@ def process_arg(arg):
424430
posargs = []
425431
kwargs = {}
426432

427-
# This is either a "arglist" or a single argument
428-
if results['arglist'].type == syms.arglist:
433+
# This is either empty, an "arglist", or a single argument
434+
if 'arglist' not in results:
435+
pass
436+
elif results['arglist'].type == syms.arglist:
429437
for arg in results['arglist'].children:
430438
process_arg(arg)
431439
else:
@@ -439,17 +447,17 @@ def process_arg(arg):
439447

440448
required_args, argsdict = utils.resolve_func_args(test_func, posargs, kwargs)
441449

442-
if method.startswith(('assertRaises', 'assertWarns')):
450+
if method.startswith(('assertRaises', 'assertWarns')) or method == 'fail':
443451
n_stmt = _method_map[method](*required_args,
444452
indent=find_indentation(node),
445453
kws=argsdict,
446-
arglist=results['arglist'],
454+
arglist=results.get('arglist'),
447455
node=node)
448456
else:
449457
n_stmt = Node(syms.assert_stmt,
450458
[Name('assert'),
451459
_method_map[method](*required_args, kws=argsdict)])
452-
if argsdict.get('msg', None) is not None:
460+
if argsdict.get('msg', None) is not None and method != 'fail':
453461
n_stmt.children.extend((Name(','), argsdict['msg']))
454462

455463
def fix_line_wrapping(x):
@@ -465,7 +473,7 @@ def fix_line_wrapping(x):
465473
n_stmt.prefix = node.prefix
466474

467475
# add necessary imports
468-
if 'Raises' in method or 'Warns' in method:
476+
if 'Raises' in method or 'Warns' in method or method == "fail":
469477
add_import('pytest', node)
470478
if ('Regex' in method and not 'Raises' in method and
471479
not 'Warns' in method):

0 commit comments

Comments
 (0)