Skip to content

Commit

Permalink
Convert self.fail(…) to pytest.fail(…) (#75)
Browse files Browse the repository at this point in the history
Fix #39.

---------

Co-authored-by: Bruno Oliveira <[email protected]>
  • Loading branch information
Code0x58 and nicoddemus authored Dec 10, 2024
1 parent 82d7ab7 commit ebdf341
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ Changelog
0.5 (unreleased)
----------------


- Convert ``self.fail()`` to ``pytest.fail()`` (`#39`__).

- Python >=3.9 is now required.

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

.. _#39: https://github.com/pytest-dev/unittest2pytest/issues/39
.. _#54: https://github.com/pytest-dev/unittest2pytest/issues/54



0.4 (2019-06-30)
----------------

Expand Down
18 changes: 18 additions & 0 deletions tests/fixtures/self_assert/fail_in.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# required-method: fail

class TestAssertTrue(TestCase):
def test_me(self):
self.fail(xxx+y)
self.fail(aaa % bbb)
self.fail(ccc or ddd)

def test_everybody(self):
self.fail( 'abc' )

def test_message(self):
self.fail(msg='This is wrong!')
self.fail(error_message)

def test_nothing(self):
self.fail()
self.fail(self.fail())
19 changes: 19 additions & 0 deletions tests/fixtures/self_assert/fail_out.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# required-method: fail

import pytest
class TestAssertTrue(TestCase):
def test_me(self):
pytest.fail(xxx+y)
pytest.fail(aaa % bbb)
pytest.fail(ccc or ddd)

def test_everybody(self):
pytest.fail( 'abc' )

def test_message(self):
pytest.fail(msg='This is wrong!')
pytest.fail(error_message)

def test_nothing(self):
pytest.fail()
pytest.fail(pytest.fail())
22 changes: 15 additions & 7 deletions unittest2pytest/fixes/fix_self_assert.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ def RaisesRegexOp(context, designator, exceptionClass, expected_regex,
else:
return Node(syms.suite, [with_stmt])

def FailOp(indent, kws, arglist, node):
new = node.clone()
new.set_child(0, Name('pytest'))
return new

def add_import(import_name, node):
suite = get_parent_of_type(node, syms.suite)
Expand Down Expand Up @@ -290,6 +294,8 @@ def get_import_nodes(node):
'assertRaisesRegex': partial(RaisesRegexOp, 'pytest.raises', 'excinfo'),
'assertWarnsRegex': partial(RaisesRegexOp, 'pytest.warns', 'record'),

'fail': FailOp,

#'assertLogs': -- not to be handled here, is an context handler only
}

Expand Down Expand Up @@ -376,7 +382,7 @@ class FixSelfAssert(BaseFix):
PATTERN = """
power< 'self'
trailer< '.' method=( %s ) >
trailer< '(' arglist=any ')' >
trailer< '(' [arglist=any] ')' >
>
""" % ' | '.join(map(repr,
(set(_method_map.keys()) | set(_method_aliases.keys()))))
Expand Down Expand Up @@ -422,8 +428,10 @@ def process_arg(arg):
posargs = []
kwargs = {}

# This is either a "arglist" or a single argument
if results['arglist'].type == syms.arglist:
# This is either empty, an "arglist", or a single argument
if 'arglist' not in results:
pass
elif results['arglist'].type == syms.arglist:
for arg in results['arglist'].children:
process_arg(arg)
else:
Expand All @@ -437,17 +445,17 @@ def process_arg(arg):

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

if method.startswith(('assertRaises', 'assertWarns')):
if method.startswith(('assertRaises', 'assertWarns')) or method == 'fail':
n_stmt = _method_map[method](*required_args,
indent=find_indentation(node),
kws=argsdict,
arglist=results['arglist'],
arglist=results.get('arglist'),
node=node)
else:
n_stmt = Node(syms.assert_stmt,
[Name('assert'),
_method_map[method](*required_args, kws=argsdict)])
if argsdict.get('msg', None) is not None:
if argsdict.get('msg', None) is not None and method != 'fail':
n_stmt.children.extend((Name(','), argsdict['msg']))

def fix_line_wrapping(x):
Expand All @@ -463,7 +471,7 @@ def fix_line_wrapping(x):
n_stmt.prefix = node.prefix

# add necessary imports
if 'Raises' in method or 'Warns' in method:
if 'Raises' in method or 'Warns' in method or method == 'fail':
add_import('pytest', node)
if ('Regex' in method and not 'Raises' in method and
not 'Warns' in method):
Expand Down

0 comments on commit ebdf341

Please sign in to comment.