From 173420d633c3323c0ab7cac50c76964e1161e738 Mon Sep 17 00:00:00 2001 From: Sergey Demenok Date: Fri, 19 Mar 2021 14:31:55 +0300 Subject: [PATCH] add flag that allows not to attach logs for passed tests --- allure-pytest/src/listener.py | 3 ++ allure-pytest/src/plugin.py | 5 +++ .../acceptance/capture/capture_attach_test.py | 44 +++++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/allure-pytest/src/listener.py b/allure-pytest/src/listener.py index b6476c21..30da148a 100644 --- a/allure-pytest/src/listener.py +++ b/allure-pytest/src/listener.py @@ -203,6 +203,9 @@ def pytest_runtest_makereport(self, item, call): test_result.statusDetails = status_details if self.config.option.attach_capture: + if test_result.status == Status.PASSED and not self.config.option.attach_capture_passed: + return + if report.caplog: self.attach_data(report.caplog, "log", AttachmentType.TEXT, None) if report.capstdout: diff --git a/allure-pytest/src/plugin.py b/allure-pytest/src/plugin.py index 48c3411a..f1c0ffaa 100644 --- a/allure-pytest/src/plugin.py +++ b/allure-pytest/src/plugin.py @@ -34,6 +34,11 @@ def pytest_addoption(parser): dest="attach_capture", help="Do not attach pytest captured logging/stdout/stderr to report") + parser.getgroup("reporting").addoption('--allure-no-capture-passed', + action="store_false", + dest="attach_capture_passed", + help="Do not attach pytest captured logging/stdout/stderr to passed report") + def label_type(type_name, legal_values=set()): def a_label_type(string): atoms = set(string.split(',')) diff --git a/allure-pytest/test/acceptance/capture/capture_attach_test.py b/allure-pytest/test/acceptance/capture/capture_attach_test.py index e4375703..6b2a6f07 100644 --- a/allure-pytest/test/acceptance/capture/capture_attach_test.py +++ b/allure-pytest/test/acceptance/capture/capture_attach_test.py @@ -124,3 +124,47 @@ def test_capture_disabled(allured_testdir): assert_that(allured_testdir.allure_report, has_property("attachments", empty()) ) + + +def test_capture_passed_disabled_for_successful(allured_testdir): + """ + >>> import logging + >>> logger = logging.getLogger(__name__) + + >>> def test_capture_passed_disabled_example(): + ... logger.info("Start logging") + ... print ("Start printing") + + """ + + allured_testdir.parse_docstring_source() + allured_testdir.run_with_allure("--log-cli-level=INFO", "--allure-no-capture-passed") + + assert_that(allured_testdir.allure_report, + has_property("attachments", empty()) + ) + + +def test_capture_passed_disabled_for_unsuccessful(allured_testdir): + """ + >>> import logging + >>> logger = logging.getLogger(__name__) + + >>> def test_capture_passed_disabled_example(): + ... logger.info("Start logging") + ... print ("Start printing") + ... assert False + + """ + + allured_testdir.parse_docstring_source() + allured_testdir.run_with_allure("--log-cli-level=INFO", "--allure-no-capture-passed") + + assert_that(allured_testdir.allure_report, + has_property("attachments", + all_of( + is_(has_value(contains_string("Start logging"))), + is_(has_value(contains_string("Start printing"))) + ) + ) + )