diff --git a/testrail_api_reporter/engines/at_coverage_reporter.py b/testrail_api_reporter/engines/at_coverage_reporter.py index 7b54e48..699fafb 100644 --- a/testrail_api_reporter/engines/at_coverage_reporter.py +++ b/testrail_api_reporter/engines/at_coverage_reporter.py @@ -4,7 +4,7 @@ from ..utils.case_stat import CaseStat from ..utils.csv_parser import CSVParser -from ..utils.reporter_utils import format_error +from ..utils.reporter_utils import format_error, init_get_cases_process class ATCoverageReporter: @@ -135,12 +135,7 @@ def __get_all_cases( """ project_id = project_id if project_id else self.__project suite_id = suite_id if suite_id else self.__suite_id - debug = debug if debug is not None else self.__debug - cases_list = [] - first_run = True - criteria = None - response = None - retry = 0 + debug, cases_list, first_run, criteria, response, retry = init_get_cases_process(debug, self.__debug) while criteria is not None or first_run: if first_run: try: diff --git a/testrail_api_reporter/engines/results_reporter.py b/testrail_api_reporter/engines/results_reporter.py index f8f0057..6dfa72e 100644 --- a/testrail_api_reporter/engines/results_reporter.py +++ b/testrail_api_reporter/engines/results_reporter.py @@ -6,7 +6,7 @@ from testrail_api import TestRailAPI from xmltodict import parse -from ..utils.reporter_utils import format_error +from ..utils.reporter_utils import format_error, init_get_cases_process class TestRailResultsReporter: @@ -200,6 +200,17 @@ def __enrich_with_tc_num(self, xml_dict_list, tc_dict_list, debug=None): print(f"{len(enriched_list)} test results were prepared for send.") return enriched_list + @staticmethod + def ___handle_read_timeout(retry, retries, debug, error): + if retry < retries: + retry += 1 + if debug: + print(f"Timeout error, retrying {retry}/{retries}...") + return retry, True + raise ValueError( + f"Get cases failed. Please validate your settings!nError{format_error(error)}" + ) from error + # pylint: disable=R0912 def __get_all_auto_cases(self, retries=3, debug=None): """ @@ -209,25 +220,15 @@ def __get_all_auto_cases(self, retries=3, debug=None): :param debug: debug output is enabled, may be True or False, optional :return: list of dict with cases """ - debug = debug if debug is not None else self.__debug - cases_list = [] - first_run = True - criteria = None - response = None - retry = 0 + debug, cases_list, first_run, criteria, response, retry = init_get_cases_process(debug, self.__debug) while criteria is not None or first_run: if first_run: try: response = self.__api.cases.get_cases(project_id=self.__project_id, suite_id=self.__suite_id) except ReadTimeout as error: - if retry < retries: - retry += 1 - if debug: - print(f"Timeout error, retrying {retry}/{retries}...") + retry, should_continue = self.___handle_read_timeout(retry, retries, debug, error) + if should_continue: continue - raise ValueError( - f"Get cases failed. Please validate your settings!\nError{format_error(error)}" - ) from error except Exception as error: print(f"Get cases failed. Please validate your settings!\nError{format_error(error)}") self.__self_check() @@ -241,14 +242,9 @@ def __get_all_auto_cases(self, retries=3, debug=None): project_id=self.__project_id, suite_id=self.__suite_id, offset=offset ) except ReadTimeout as error: - if retry < retries: - retry += 1 - if debug: - print(f"Timeout error, retrying {retry}/{retries}...") + retry, should_continue = self.___handle_read_timeout(retry, retries, debug, error) + if should_continue: continue - raise ValueError( - f"Get cases failed. Please validate your settings!\nError{format_error(error)}" - ) from error retry = 0 cases = response["cases"] for item in cases: diff --git a/testrail_api_reporter/publishers/email_sender.py b/testrail_api_reporter/publishers/email_sender.py index 134379a..002201c 100644 --- a/testrail_api_reporter/publishers/email_sender.py +++ b/testrail_api_reporter/publishers/email_sender.py @@ -11,7 +11,7 @@ from apiclient import discovery from oauth2client import client, tools, file -from ..utils.reporter_utils import format_error +from ..utils.reporter_utils import format_error, check_captions_and_files class EmailSender: @@ -86,17 +86,10 @@ def send_message( # pylint: disable=too-many-branches elif not isinstance(recipients, list) and not custom_message: raise ValueError("Wrong list of recipients is provided, aborted!") debug = debug if debug is not None else self.__debug - if not isinstance(captions, list) or custom_message: + captions = check_captions_and_files(captions=captions, files=files, debug=debug) + if not captions or custom_message: if debug: - print("Caption list is empty, no legend will be displayed") - captions = None - elif len(captions) != len(files): - if debug: - print( - f"Caption and file lists are not the same length {len(captions)} != {len(files)} thus " - f"no legend will be displayed" - ) - captions = None + print("Caption list override by custom message, no legend will be displayed") timestamp = timestamp if timestamp else datetime.now().strftime("%Y-%m-%d") title = title if title else f"Test development & automation coverage report for {timestamp}" diff --git a/testrail_api_reporter/publishers/slack_sender.py b/testrail_api_reporter/publishers/slack_sender.py index e5d1a17..d968b1b 100644 --- a/testrail_api_reporter/publishers/slack_sender.py +++ b/testrail_api_reporter/publishers/slack_sender.py @@ -3,7 +3,7 @@ import requests -from ..utils.reporter_utils import format_error +from ..utils.reporter_utils import format_error, check_captions_and_files class SlackSender: @@ -99,18 +99,7 @@ def send_message( if not isinstance(files, list): raise ValueError("No file list for report provided, aborted!") debug = debug if debug is not None else self.__debug - if not isinstance(captions, list): - if debug: - print("Caption list is empty, no legend will be displayed") - captions = None - elif len(captions) != len(files): - if debug: - print( - f"Caption and file lists are not the same length {len(captions)} != {len(files)} thus " - "no legend will be displayed" - ) - captions = None - + captions = check_captions_and_files(captions=captions, files=files, debug=debug) # Send to slack try: response = requests.post( diff --git a/testrail_api_reporter/utils/csv_parser.py b/testrail_api_reporter/utils/csv_parser.py index 9daed9a..7c0b4ca 100644 --- a/testrail_api_reporter/utils/csv_parser.py +++ b/testrail_api_reporter/utils/csv_parser.py @@ -47,7 +47,7 @@ def save_history_data(self, filename=None, report=None, debug=None): raise ValueError("Can't open report file, save history data aborted!") from FileNotFoundError if last_date != date: if debug: - print("Saving data in {0} for {1}".format(filename, date)) + print(f"Saving data in {filename} for {date}") with open(filename, "a+", newline="", encoding="utf-8") as csvfile: writer = csv.writer(csvfile, delimiter=",", quotechar="|", quoting=csv.QUOTE_MINIMAL) diff --git a/testrail_api_reporter/utils/reporter_utils.py b/testrail_api_reporter/utils/reporter_utils.py index 94d2f7d..b82f1fa 100644 --- a/testrail_api_reporter/utils/reporter_utils.py +++ b/testrail_api_reporter/utils/reporter_utils.py @@ -66,3 +66,44 @@ def zip_file(filename, suffix=None, debug=True): if debug: print(f"ZIPped {filename} to {zipped_file}") return zipped_file + + +def check_captions_and_files(captions, files, debug): + """ + Service function to check captions and files lists + + :param captions: list of captions for files, list of strings, if not provided, no captions will be added + :param files: list of urls of images + :param debug: debug output is enabled, may be True or False, optional + :return: captions list or None + """ + return_value = captions + if not isinstance(captions, list): + if debug: + print("Caption list is empty, no legend will be displayed") + return_value = None + elif len(captions) != len(files): + if debug: + print( + f"Caption and file lists are not the same length {len(captions)} != {len(files)} thus " + f"no legend will be displayed" + ) + return_value = None + return return_value + + +def init_get_cases_process(debug, default_debug): + """ + Service function to initialize process + + :param debug: debug output is enabled, may be True or False, optional + :param default_debug: default debug output is enabled, may be True or False, optional + :return: debug, cases_list, first_run, criteria, response, retry + """ + debug = debug if debug is not None else default_debug + cases_list = [] + first_run = True + criteria = None + response = None + retry = 0 + return debug, cases_list, first_run, criteria, response, retry \ No newline at end of file