diff --git a/pytest_testdox/formatters.py b/pytest_testdox/formatters.py index d6f66f4..b0c6b81 100644 --- a/pytest_testdox/formatters.py +++ b/pytest_testdox/formatters.py @@ -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('*', '') diff --git a/pytest_testdox/models.py b/pytest_testdox/models.py index 218307f..02e101f 100644 --- a/pytest_testdox/models.py +++ b/pytest_testdox/models.py @@ -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], diff --git a/tests/test_formatters.py b/tests/test_formatters.py index c5ae077..1993825 100644 --- a/tests/test_formatters.py +++ b/tests/test_formatters.py @@ -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]]]]' diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 0ff7a60..d344aca 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -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()