Skip to content

Commit

Permalink
Merge pull request #40 from melexis/tags-for-inclusion
Browse files Browse the repository at this point in the history
Filter test cases on tags
  • Loading branch information
Letme authored Aug 31, 2023
2 parents 92bd537 + cbd3faa commit 31c30fd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
19 changes: 12 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ Usage
$ robot2rst --help
usage: robot2rst [-h] -i ROBOT_FILE -o RST_FILE [--only EXPRESSION] [-p PREFIX]
[-r [RELATIONSHIPS [RELATIONSHIPS ...]]] [-t [TAGS [TAGS ...]]]
[--type TYPE] [--trim-suffix]
[-r [RELATIONSHIPS ...]] [-t [TAGS ...]] [--include [INCLUDE ...]]
[-c [COVERAGE ...]] [--type TYPE] [--trim-suffix]
Convert robot test cases to reStructuredText with traceable items.
Expand All @@ -51,18 +51,23 @@ Usage
Input robot file
-o RST_FILE, --rst RST_FILE
Output RST file, e.g. my_component_qtp.rst
--only EXPRESSION Expression of tags for Sphinx' `only` directive that surrounds all RST
content. By default, no `only` directive is generated.
--only EXPRESSION Expression of tags for Sphinx' `only` directive that surrounds all
RST content. By default, no `only` directive is generated.
-p PREFIX, --prefix PREFIX
Overrides the default 'QTEST-' prefix.
-r [RELATIONSHIPS ...], --relationships [RELATIONSHIPS ...]
Name(s) of the relationship(s) used to link to items in Tags section.
The default value is 'validates'.
-t [TAGS ...], --tags [TAGS ...]
Regex(es) for matching tags to add a relationship link for. All tags
get matched by default.
Zero or more Python regexes for matching tags to treat them as
traceable targets via a relationship. All tags get matched by
default.
--include [INCLUDE ...]
Zero or more Python regexes for matching tags to filter test cases.
If every regex matches at least one of a test case's tags, the test
case is included.
-c [COVERAGE ...], --coverage [COVERAGE ...]
Minumum coverage percentages for the item-matrix(es); 1 value per tag
Minimum coverage percentages for the item-matrix(es); 1 value per tag
in -t, --tags.
--type TYPE Give value that starts with 'q' or 'i' (case-insensitive) to
explicitly define the type of test: qualification/integration test.
Expand Down
23 changes: 17 additions & 6 deletions mlx/robot2rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,13 @@ def main():
help="Name(s) of the relationship(s) used to link to items in Tags section. The default value "
"is 'validates'.")
parser.add_argument("-t", "--tags", nargs='*',
help="Regex(es) for matching tags to add a relationship link for. All tags get matched by "
"default.")
help="Zero or more Python regexes for matching tags to treat them as traceable targets via a "
"relationship. All tags get matched by default.")
parser.add_argument("--include", nargs='*', default=[],
help="Zero or more Python regexes for matching tags to filter test cases. "
"If every regex matches at least one of a test case's tags, the test case is included.")
parser.add_argument("-c", "--coverage", nargs='*',
help="Minumum coverage percentages for the item-matrix(es); 1 value per tag in -t, --tags.")
help="Minimum coverage percentages for the item-matrix(es); 1 value per tag in -t, --tags.")
parser.add_argument("--type", default='q',
help="Give value that starts with 'q' or 'i' (case-insensitive) to explicitly define "
"the type of test: qualification/integration test. The default is 'qualification'.")
Expand Down Expand Up @@ -137,10 +140,18 @@ def main():
f"percentages ({len(coverages)}).")
relationship_config = [(relationships[i], tag_regexes[i], coverages[i]) for i in range(len(relationships))]

parser = ParserApplication(Path(args.robot_file))
parser = ParserApplication(Path(args.robot_file), args.include)
parser.run()
return generate_robot_2_rst(parser, Path(args.rst_file), prefix, relationship_config,
gen_matrix, test_type=test_type, only=args.expression, coverages=coverages)
if parser.tests:
exit_code = generate_robot_2_rst(parser, Path(args.rst_file), prefix, relationship_config,
gen_matrix, test_type=test_type, only=args.expression, coverages=coverages)
else:
msg = f"robot2rst did not generate {args.rst_file} because {args.robot_file} does not contain any test cases"
if args.include:
msg += f" with tags matching all regexes {args.include}"
LOGGER.info(msg)
exit_code = 0
return exit_code


def entrypoint():
Expand Down
17 changes: 15 additions & 2 deletions mlx/robot_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@ class ParserApplication(ModelVisitor):
"""
TestAttributes = namedtuple('TestAttributes', 'name doc tags')

def __init__(self, robot_file, *args, **kwargs):
def __init__(self, robot_file, tags_for_inclusion, *args, **kwargs):
""" Constructor
Args:
robot_file (Path): Path to the .robot file.
tags_for_inclusion (list): Regexes for matching tags. A test case is included if every regex matches at
least one of its tags.
"""
super().__init__(*args, **kwargs)
self.robot_file = str(robot_file.resolve(strict=True))
self.model = get_model(self.robot_file)
self.tests = []
self.variables = {}
self.tags_for_inclusion = tags_for_inclusion

def run(self):
self.visit(self.model)
Expand Down Expand Up @@ -63,5 +66,15 @@ def visit_TestCase(self, node):
previous_token = token
elif element_type == Token.TAGS:
tags = [el.value for el in element.tokens if el.type == Token.ARGUMENT]
if self.evaluate_inclusion(tags):
self.tests.append(self.TestAttributes(node.name, doc, tags))

self.tests.append(self.TestAttributes(node.name, doc, tags))
def evaluate_inclusion(self, tags):
for pattern in self.tags_for_inclusion:
regexp = re.compile(pattern)
for tag in tags:
if regexp.match(tag):
break
else:
return False
return True

0 comments on commit 31c30fd

Please sign in to comment.