From 699ad72190676d846bc58619eebeae8b6db48701 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 4 Oct 2018 12:42:44 +0200 Subject: [PATCH 1/5] Using iter_markers instead of get_markers. --- allure-pytest/src/utils.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/allure-pytest/src/utils.py b/allure-pytest/src/utils.py index bf23779e..79596cbc 100644 --- a/allure-pytest/src/utils.py +++ b/allure-pytest/src/utils.py @@ -65,19 +65,20 @@ def pytest_markers(item): for keyword in item.keywords.keys(): if not any((keyword.startswith('allure_'), keyword == 'parametrize')): - marker = item.get_marker(keyword) - if marker: - yield mark_to_str(marker) + for n, marker in enumerate(item.iter_markers(keyword)): + yield mark_to_str(marker, n) -def mark_to_str(marker): +def mark_to_str(marker, distance=None): args = [represent(arg) for arg in marker.args] kwargs = ['{name}={value}'.format(name=key, value=represent(marker.kwargs[key])) for key in marker.kwargs] + markstr = '@pytest.mark.{name}'.format(name=marker.name) if args or kwargs: parameters = ', '.join(args + kwargs) - return '@pytest.mark.{name}({parameters})'.format(name=marker.name, parameters=parameters) - else: - return '@pytest.mark.{name}'.format(name=marker.name) + markstr = '{markstr}({parameters})'.format(markstr=markstr, name=marker.name, parameters=parameters) + if distance == 0: + markstr = '{} closest'.format(markstr) + return markstr def allure_package(item): From b0fb55704687191a36a6957f098073351bfd81f6 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 4 Oct 2018 13:11:05 +0200 Subject: [PATCH 2/5] Refactored representation. --- allure-pytest/src/utils.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/allure-pytest/src/utils.py b/allure-pytest/src/utils.py index 79596cbc..fe3af845 100644 --- a/allure-pytest/src/utils.py +++ b/allure-pytest/src/utils.py @@ -65,18 +65,21 @@ def pytest_markers(item): for keyword in item.keywords.keys(): if not any((keyword.startswith('allure_'), keyword == 'parametrize')): - for n, marker in enumerate(item.iter_markers(keyword)): - yield mark_to_str(marker, n) + markers = list(item.iter_markers(keyword)) + for n, marker in enumerate(markers): + # If we have only one marker it is unnecessary to show it as closest + closest = True if (len(markers) > 1 and n==0) else False + yield mark_to_str(marker, closest) -def mark_to_str(marker, distance=None): +def mark_to_str(marker, closest): args = [represent(arg) for arg in marker.args] kwargs = ['{name}={value}'.format(name=key, value=represent(marker.kwargs[key])) for key in marker.kwargs] markstr = '@pytest.mark.{name}'.format(name=marker.name) if args or kwargs: parameters = ', '.join(args + kwargs) markstr = '{markstr}({parameters})'.format(markstr=markstr, name=marker.name, parameters=parameters) - if distance == 0: + if closest: markstr = '{} closest'.format(markstr) return markstr From 3f47a636c6f88e02a5285385a6c03661ef86d49f Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 4 Oct 2018 13:29:55 +0200 Subject: [PATCH 3/5] Removed duplicated tag addition to report and test in place. --- allure-pytest/src/listener.py | 1 - allure-pytest/test/conftest.py | 2 ++ .../test/labels/mark/function_marker_test.py | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/allure-pytest/src/listener.py b/allure-pytest/src/listener.py index 55e444a7..894b57a2 100644 --- a/allure-pytest/src/listener.py +++ b/allure-pytest/src/listener.py @@ -101,7 +101,6 @@ def pytest_runtest_teardown(self, item): test_result = self.allure_logger.get_test(uuid) test_result.labels.extend([Label(name=name, value=value) for name, value in allure_labels(item)]) test_result.labels.extend([Label(name=LabelType.TAG, value=value) for value in pytest_markers(item)]) - test_result.labels.extend([Label(name=LabelType.TAG, value=value) for value in pytest_markers(item)]) test_result.labels.extend([Label(name=name, value=value) for name, value in allure_suite_labels(item)]) test_result.labels.append(Label(name=LabelType.HOST, value=self._host)) test_result.labels.append(Label(name=LabelType.THREAD, value=self._thread)) diff --git a/allure-pytest/test/conftest.py b/allure-pytest/test/conftest.py index 96c9b48b..8d0132c4 100644 --- a/allure-pytest/test/conftest.py +++ b/allure-pytest/test/conftest.py @@ -73,6 +73,8 @@ def allure_report(request, tmpdir_factory): module = request.module.__file__ tmpdir = tmpdir_factory.mktemp('data') _runner(tmpdir.strpath, module) + with open("debug-runner", "a") as debugfile: + print(tmpdir.strpath, file=debugfile) return AllureReport(tmpdir.strpath) diff --git a/allure-pytest/test/labels/mark/function_marker_test.py b/allure-pytest/test/labels/mark/function_marker_test.py index f1a1a636..9f8feccf 100644 --- a/allure-pytest/test/labels/mark/function_marker_test.py +++ b/allure-pytest/test/labels/mark/function_marker_test.py @@ -26,6 +26,21 @@ def test_pytest_marker(): pass +@pytest.mark.skipif(False, reason="reason2") +@pytest.mark.skipif(False, reason="reason1") +def test_pytest_multiple_markers(): + """ + >>> allure_report = getfixture('allure_report') + >>> assert_that(allure_report, + ... has_test_case('test_pytest_multiple_markers', + ... has_tag("@pytest.mark.skipif(False, reason='reason2')"), + ... has_tag("@pytest.mark.skipif(False, reason='reason1') closest") + ... ) + ... ) + """ + pass + + @pytest.mark.marker('cool', 'stuff') def test_pytest_marker_with_args(): """ From e48a490823cb4dc2e4569c2af595fd94b02a8cc0 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 4 Oct 2018 14:26:35 +0200 Subject: [PATCH 4/5] Fixed linting. --- allure-pytest/src/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/allure-pytest/src/utils.py b/allure-pytest/src/utils.py index fe3af845..a63987c1 100644 --- a/allure-pytest/src/utils.py +++ b/allure-pytest/src/utils.py @@ -68,7 +68,7 @@ def pytest_markers(item): markers = list(item.iter_markers(keyword)) for n, marker in enumerate(markers): # If we have only one marker it is unnecessary to show it as closest - closest = True if (len(markers) > 1 and n==0) else False + closest = True if (len(markers) > 1 and n == 0) else False yield mark_to_str(marker, closest) From 82cf99b6a5461e3a67e50daed8724792c4269c2a Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 4 Oct 2018 19:36:36 +0200 Subject: [PATCH 5/5] Considering only user marks for allure tags. --- allure-pytest/src/utils.py | 25 +++++++++-------- .../test/labels/mark/function_marker_test.py | 28 +++++++++++-------- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/allure-pytest/src/utils.py b/allure-pytest/src/utils.py index a63987c1..d2ee92f0 100644 --- a/allure-pytest/src/utils.py +++ b/allure-pytest/src/utils.py @@ -62,25 +62,26 @@ def allure_links(item): def pytest_markers(item): + """Do not consider pytest marks (has args/kwargs) as user tags + e.g. @pytest.mark.parametrize/skip/skipif/usefixtures etc...""" for keyword in item.keywords.keys(): - if not any((keyword.startswith('allure_'), - keyword == 'parametrize')): - markers = list(item.iter_markers(keyword)) - for n, marker in enumerate(markers): - # If we have only one marker it is unnecessary to show it as closest - closest = True if (len(markers) > 1 and n == 0) else False - yield mark_to_str(marker, closest) + if keyword.startswith('allure_'): + continue + marker = item.get_closest_marker(keyword) + if marker is None: + continue + user_tag_mark = (not marker.args and not marker.kwargs) + if marker.name == "marker" or user_tag_mark: + yield mark_to_str(marker) -def mark_to_str(marker, closest): +def mark_to_str(marker): args = [represent(arg) for arg in marker.args] kwargs = ['{name}={value}'.format(name=key, value=represent(marker.kwargs[key])) for key in marker.kwargs] - markstr = '@pytest.mark.{name}'.format(name=marker.name) + markstr = '{name}'.format(name=marker.name) if args or kwargs: parameters = ', '.join(args + kwargs) - markstr = '{markstr}({parameters})'.format(markstr=markstr, name=marker.name, parameters=parameters) - if closest: - markstr = '{} closest'.format(markstr) + markstr = '{}({})'.format(markstr, parameters) return markstr diff --git a/allure-pytest/test/labels/mark/function_marker_test.py b/allure-pytest/test/labels/mark/function_marker_test.py index 9f8feccf..1e525909 100644 --- a/allure-pytest/test/labels/mark/function_marker_test.py +++ b/allure-pytest/test/labels/mark/function_marker_test.py @@ -18,23 +18,29 @@ def test_pytest_marker(): >>> allure_report = getfixture('allure_report') >>> assert_that(allure_report, ... has_test_case('test_pytest_marker', - ... has_tag('@pytest.mark.cool'), - ... has_tag('@pytest.mark.stuff') + ... has_tag('cool'), + ... has_tag('stuff') ... ) ... ) """ pass +@pytest.mark.usermark1 +@pytest.mark.usermark2 +@pytest.mark.parametrize("param", ["foo"]) @pytest.mark.skipif(False, reason="reason2") @pytest.mark.skipif(False, reason="reason1") -def test_pytest_multiple_markers(): +def test_omit_pytest_markers(param): """ >>> allure_report = getfixture('allure_report') >>> assert_that(allure_report, - ... has_test_case('test_pytest_multiple_markers', - ... has_tag("@pytest.mark.skipif(False, reason='reason2')"), - ... has_tag("@pytest.mark.skipif(False, reason='reason1') closest") + ... has_test_case('test_omit_pytest_markers[foo]', + ... has_tag("usermark1"), + ... has_tag("usermark2"), + ... is_not(has_tag("skipif(False, reason='reason2')")), + ... is_not(has_tag("skipif(False, reason='reason1')")), + ... is_not(has_tag("parametrize('param', ['foo'])")), ... ) ... ) """ @@ -47,7 +53,7 @@ def test_pytest_marker_with_args(): >>> allure_report = getfixture('allure_report') >>> assert_that(allure_report, ... has_test_case('test_pytest_marker_with_args', - ... has_tag("@pytest.mark.marker('cool', 'stuff')") + ... has_tag("marker('cool', 'stuff')") ... ) ... ) """ @@ -60,7 +66,7 @@ def test_pytest_marker_with_kwargs(): >>> allure_report = getfixture('allure_report') >>> assert_that(allure_report, ... has_test_case('test_pytest_marker_with_kwargs', - ... has_tag("@pytest.mark.marker(stuff='cool')") + ... has_tag("marker(stuff='cool')") ... ) ... ) """ @@ -74,7 +80,7 @@ def test_pytest_marker_with_kwargs_native_encoding(): >>> allure_report = getfixture('allure_report') >>> assert_that(allure_report, ... has_test_case('test_pytest_marker_with_kwargs_native_encoding', - ... has_tag("@pytest.mark.marker(stuff=%s)" % represent('я')) + ... has_tag("marker(stuff=%s)" % represent('я')) ... ) ... ) """ @@ -88,7 +94,7 @@ def test_pytest_marker_with_kwargs_utf_encoding(): >>> allure_report = getfixture('allure_report') >>> assert_that(allure_report, ... has_test_case('test_pytest_marker_with_kwargs_utf_encoding', - ... has_tag("@pytest.mark.marker(stuff=%s)" % represent('я')) + ... has_tag("marker(stuff=%s)" % represent('я')) ... ) ... ) """ @@ -101,7 +107,7 @@ def test_pytest_marker_with_args_and_kwargs(): >>> allure_report = getfixture('allure_report') >>> assert_that(allure_report, ... has_test_case('test_pytest_marker_with_args_and_kwargs', - ... has_tag("@pytest.mark.marker('that', stuff='cool')") + ... has_tag("marker('that', stuff='cool')") ... ) ... ) """