Skip to content

Commit

Permalink
Merge pull request #23 from renanivo/ro-mark-it-parametrize-fix
Browse files Browse the repository at this point in the history
Fix mark.it titles when used with mark.parametrize
  • Loading branch information
RomuloOliveira authored Apr 18, 2019
2 parents 4778827 + c8f06de commit d8df2bf
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
17 changes: 17 additions & 0 deletions pytest_testdox/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ def pad_text_to_characters(characters, text):
return os.linesep.join(result)


def include_parametrized(title, original_title):
first_bracket = original_title.find('[')
last_bracket = original_title.rfind(']')

has_parameters = last_bracket > first_bracket

if not has_parameters:
return title

parameters = original_title[first_bracket + 1:last_bracket]

return '{title}[{parameters}]'.format(
title=title,
parameters=parameters
)


def _remove_patterns(statement, patterns):
for glob_pattern in patterns:
pattern = glob_pattern.replace('*', '')
Expand Down
5 changes: 4 additions & 1 deletion pytest_testdox/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ def parse(cls, nodeid, pattern_config, title=None, class_name=None):
node_parts = nodeid.split('::')

if title:
title = formatters.format_multi_line_text(title)
title = formatters.include_parametrized(
formatters.format_multi_line_text(title),
node_parts[-1]
)
else:
title = formatters.format_title(
node_parts[-1],
Expand Down
21 changes: 21 additions & 0 deletions tests/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,24 @@ def test_should_pad_the_following_lines_to_the_width_of_given_characters(
os.linesep
)
)


class TestIncludeParametrized(object):

def test_should_return_title_when_no_parameters_are_found(self):
assert formatters.include_parametrized(
title='Should return value',
original_title='test_should_return_value'
) == 'Should return value'

def test_should_return_parameters_in_title(self):
assert formatters.include_parametrized(
title='A title',
original_title='test_should_return_value[params]'
) == 'A title[params]'

def test_should_return_parameters_containing_brackets(self):
assert formatters.include_parametrized(
title='A title',
original_title='test_should_return_value[[[[params]]]]'
) == 'A title[[[[params]]]]'
40 changes: 40 additions & 0 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,43 @@ def test_foo(self):
result = testdir.runpytest('--testdox')

assert 'My Class\nMy precious class' in result.stdout.str()

def test_should_override_test_titles_with_title_mark_parametrize(
self,
testdir
):
testdir.makefile('.py', test_module_name="""
import pytest
@pytest.mark.parametrize('par', ['param1', 'param2'])
@pytest.mark.{}('should pass with parameters')
def test_a_passing_test(par):
assert True
""".format(
constants.TITLE_MARK
))

result = testdir.runpytest('--testdox')

assert 'should pass with parameters[param1]' in result.stdout.str()
assert 'should pass with parameters[param2]' in result.stdout.str()

def test_decorator_order_should_not_affect_parametrize(
self,
testdir
):
testdir.makefile('.py', test_module_name="""
import pytest
@pytest.mark.{}('should pass with parameters')
@pytest.mark.parametrize('par', ['param1', 'param2'])
def test_a_passing_test(par):
assert True
""".format(
constants.TITLE_MARK
))

result = testdir.runpytest('--testdox')

assert 'should pass with parameters[param1]' in result.stdout.str()
assert 'should pass with parameters[param2]' in result.stdout.str()

0 comments on commit d8df2bf

Please sign in to comment.