From b410ed7d09ce6b8c0b7caec600a55341995f5f69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Oliveira?= Date: Wed, 17 Apr 2019 19:56:54 -0300 Subject: [PATCH 1/5] Add a function to return a custom title with parametrized values --- pytest_testdox/formatters.py | 17 +++++++++++++++++ tests/test_formatters.py | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) 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/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]]]]' From bcd6da95b36542dbb4ee9ff13d9e994baa0f57f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Oliveira?= Date: Wed, 17 Apr 2019 20:04:03 -0300 Subject: [PATCH 2/5] Fix mark.it titles when used with mark.parametrize --- pytest_testdox/models.py | 5 ++++- tests/test_plugin.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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_plugin.py b/tests/test_plugin.py index 0ff7a60..4932164 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -174,3 +174,25 @@ 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.{}(''' + My Title + My precious title + ''') + def test_a_passing_test(par): + assert True + """.format( + constants.TITLE_MARK + )) + + result = testdir.runpytest('--testdox') + + assert 'My Title\n My precious title[param' in result.stdout.str() From 77ccd36dac485ada2282fd27dc0c2339b731392e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Oliveira?= Date: Wed, 17 Apr 2019 20:21:03 -0300 Subject: [PATCH 3/5] Use a simple title to focus on parametrized part --- tests/test_plugin.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 4932164..c6d6b19 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -183,10 +183,7 @@ def test_should_override_test_titles_with_title_mark_parametrize( import pytest @pytest.mark.parametrize('par', ['param1', 'param2']) - @pytest.mark.{}(''' - My Title - My precious title - ''') + @pytest.mark.{}('should pass with parameters') def test_a_passing_test(par): assert True """.format( @@ -195,4 +192,4 @@ def test_a_passing_test(par): result = testdir.runpytest('--testdox') - assert 'My Title\n My precious title[param' in result.stdout.str() + assert 'should pass with parameters[param' in result.stdout.str() From 45df3157a5574191e517cefec48fb5fb0ef12c63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Oliveira?= Date: Wed, 17 Apr 2019 20:22:38 -0300 Subject: [PATCH 4/5] Add a test to ensure decorator order doesn't affect parametrize --- tests/test_plugin.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index c6d6b19..73584b4 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -193,3 +193,22 @@ def test_a_passing_test(par): result = testdir.runpytest('--testdox') assert 'should pass with parameters[param' 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[param' in result.stdout.str() From c8f06de3f5dc8142eb9e9cbb98dbab36340524a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=B4mulo=20Oliveira?= Date: Thu, 18 Apr 2019 13:12:56 -0300 Subject: [PATCH 5/5] Replace generic assertion with an assertion per param --- tests/test_plugin.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_plugin.py b/tests/test_plugin.py index 73584b4..d344aca 100644 --- a/tests/test_plugin.py +++ b/tests/test_plugin.py @@ -192,7 +192,8 @@ def test_a_passing_test(par): result = testdir.runpytest('--testdox') - assert 'should pass with parameters[param' in result.stdout.str() + 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, @@ -211,4 +212,5 @@ def test_a_passing_test(par): result = testdir.runpytest('--testdox') - assert 'should pass with parameters[param' in result.stdout.str() + assert 'should pass with parameters[param1]' in result.stdout.str() + assert 'should pass with parameters[param2]' in result.stdout.str()