diff --git a/robotframework2testrail.py b/robotframework2testrail.py index 8ad0f7f..caf3497 100644 --- a/robotframework2testrail.py +++ b/robotframework2testrail.py @@ -30,20 +30,27 @@ logging.getLogger().addHandler(CONSOLE_HANDLER) +DEFAULT_TAG_PREFIX = "test_case_id" + + class TestRailResultVisitor(ResultVisitor): """ Implement a `Visitor` that retrieves TestRail ID from Robot Framework Result """ - def __init__(self): + def __init__(self, tag_prefix=DEFAULT_TAG_PREFIX): """ Init """ + if not isinstance(tag_prefix, str): + raise ValueError("tag_prefix should be of type str, got %s" % type(tag_prefix)) + self.result_testcase_list = [] + self.tag_prefix = tag_prefix def end_suite(self, suite): """ Called when suite end """ - for _suite, test, test_case_id in self._get_test_case_id_from_suite(suite): + for _suite, test, test_case_id in self._get_test_case_id_from_suite(suite, self.tag_prefix): self._append_testrail_result(_suite, test, test_case_id) @staticmethod - def _get_test_case_id_from_suite(suite): + def _get_test_case_id_from_suite(suite, tag_prefix): """ Retrieve list of Test Case ID from a suite Manage both case: ID in metadata or in tags. """ @@ -56,7 +63,7 @@ def _get_test_case_id_from_suite(suite): break # We only take the first ID found # Retrieve test_case_ids from tags for test in suite.tests: - test_case_ids_from_tags = TestRailResultVisitor._get_test_case_ids_from_tags(test.tags) + test_case_ids_from_tags = TestRailResultVisitor._get_test_case_ids_from_tags(test.tags, tag_prefix) if test_case_ids_from_tags: for tcid in test_case_ids_from_tags: result.append((test.name, test, tcid)) @@ -68,12 +75,13 @@ def _get_test_case_id_from_suite(suite): return result @staticmethod - def _get_test_case_ids_from_tags(tags): + def _get_test_case_ids_from_tags(tags, tag_prefix): """ Retrieve all test case tags found in the list """ + prefix = "%s=" % tag_prefix test_case_list = [] for tag in tags: - if re.findall("(test_case_id=[C]?[0-9]+)", tag): - test_case_list.append(tag[len('test_case_id='):]) + if re.findall("(%s[C]?[0-9]+)" % prefix, tag): + test_case_list.append(tag[len(prefix):]) return test_case_list def _append_testrail_result(self, name, test, testcase_id): @@ -99,10 +107,10 @@ def _append_testrail_result(self, name, test, testcase_id): }) -def get_testcases(xml_robotfwk_output): +def get_testcases(xml_robotfwk_output, tag_prefix): """ Return the list of Testcase ID with status """ result = ExecutionResult(xml_robotfwk_output, include_keywords=False) - visitor = TestRailResultVisitor() + visitor = TestRailResultVisitor(tag_prefix) result.visit(visitor) return visitor.result_testcase_list @@ -216,6 +224,9 @@ def options(): '--tr-dont-publish-blocked', action='store_true', help='Do not publish results of "blocked" testcases in TestRail.') + parser.add_argument( + '--tag-prefix', dest='tag_prefix', default=DEFAULT_TAG_PREFIX, help='Tag prefix to use if extracting testrail ID from tags. Default = \'test_case_id\'' + ) group = parser.add_mutually_exclusive_group(required=True) group.add_argument( @@ -246,7 +257,7 @@ def options(): # Manage options ARGUMENTS = options() - TESTCASES = get_testcases(ARGUMENTS.xml_robotfwk_output[0].name) + TESTCASES = get_testcases(ARGUMENTS.xml_robotfwk_output[0].name, ARGUMENTS.tag_prefix) if ARGUMENTS.dryrun: pretty_print(TESTCASES)