Skip to content

Commit ebdf341

Browse files
Code0x58nicoddemus
andauthored
Convert self.fail(…) to pytest.fail(…) (#75)
Fix #39. --------- Co-authored-by: Bruno Oliveira <bruno@pytest.org>
1 parent 82d7ab7 commit ebdf341

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ Changelog
44
0.5 (unreleased)
55
----------------
66

7+
8+
- Convert ``self.fail()`` to ``pytest.fail()`` (`#39`__).
9+
710
- Python >=3.9 is now required.
811

912
- Allow non-string keys when translating ``assertDictContainsSubset`` (`#54`_).
1013

14+
.. _#39: https://github.com/pytest-dev/unittest2pytest/issues/39
1115
.. _#54: https://github.com/pytest-dev/unittest2pytest/issues/54
1216

1317

18+
1419
0.4 (2019-06-30)
1520
----------------
1621

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
@@ -200,6 +200,10 @@ def RaisesRegexOp(context, designator, exceptionClass, expected_regex,
200200
else:
201201
return Node(syms.suite, [with_stmt])
202202

203+
def FailOp(indent, kws, arglist, node):
204+
new = node.clone()
205+
new.set_child(0, Name('pytest'))
206+
return new
203207

204208
def add_import(import_name, node):
205209
suite = get_parent_of_type(node, syms.suite)
@@ -290,6 +294,8 @@ def get_import_nodes(node):
290294
'assertRaisesRegex': partial(RaisesRegexOp, 'pytest.raises', 'excinfo'),
291295
'assertWarnsRegex': partial(RaisesRegexOp, 'pytest.warns', 'record'),
292296

297+
'fail': FailOp,
298+
293299
#'assertLogs': -- not to be handled here, is an context handler only
294300
}
295301

@@ -376,7 +382,7 @@ class FixSelfAssert(BaseFix):
376382
PATTERN = """
377383
power< 'self'
378384
trailer< '.' method=( %s ) >
379-
trailer< '(' arglist=any ')' >
385+
trailer< '(' [arglist=any] ')' >
380386
>
381387
""" % ' | '.join(map(repr,
382388
(set(_method_map.keys()) | set(_method_aliases.keys()))))
@@ -422,8 +428,10 @@ def process_arg(arg):
422428
posargs = []
423429
kwargs = {}
424430

425-
# This is either a "arglist" or a single argument
426-
if results['arglist'].type == syms.arglist:
431+
# This is either empty, an "arglist", or a single argument
432+
if 'arglist' not in results:
433+
pass
434+
elif results['arglist'].type == syms.arglist:
427435
for arg in results['arglist'].children:
428436
process_arg(arg)
429437
else:
@@ -437,17 +445,17 @@ def process_arg(arg):
437445

438446
required_args, argsdict = utils.resolve_func_args(test_func, posargs, kwargs)
439447

440-
if method.startswith(('assertRaises', 'assertWarns')):
448+
if method.startswith(('assertRaises', 'assertWarns')) or method == 'fail':
441449
n_stmt = _method_map[method](*required_args,
442450
indent=find_indentation(node),
443451
kws=argsdict,
444-
arglist=results['arglist'],
452+
arglist=results.get('arglist'),
445453
node=node)
446454
else:
447455
n_stmt = Node(syms.assert_stmt,
448456
[Name('assert'),
449457
_method_map[method](*required_args, kws=argsdict)])
450-
if argsdict.get('msg', None) is not None:
458+
if argsdict.get('msg', None) is not None and method != 'fail':
451459
n_stmt.children.extend((Name(','), argsdict['msg']))
452460

453461
def fix_line_wrapping(x):
@@ -463,7 +471,7 @@ def fix_line_wrapping(x):
463471
n_stmt.prefix = node.prefix
464472

465473
# add necessary imports
466-
if 'Raises' in method or 'Warns' in method:
474+
if 'Raises' in method or 'Warns' in method or method == 'fail':
467475
add_import('pytest', node)
468476
if ('Regex' in method and not 'Raises' in method and
469477
not 'Warns' in method):

0 commit comments

Comments
 (0)