Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

self.fail(…) → pytest.fail(…) #48

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -202,6 +202,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 @@ -292,6 +296,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 @@ -378,7 +384,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 @@ -424,8 +430,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 @@ -439,17 +447,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 @@ -465,7 +473,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