|
1 | 1 | from collections import defaultdict |
2 | 2 | from collections.abc import Callable |
3 | 3 | from dataclasses import dataclass |
| 4 | +from typing import TypedDict |
4 | 5 |
|
5 | 6 | from robot.api import ExecutionResult, ResultVisitor, logger |
6 | 7 | from robot.libraries.BuiltIn import BuiltIn |
@@ -78,6 +79,44 @@ def _normalize_kw_name(self, name: str) -> str: |
78 | 79 | return name.replace(" ", "").replace("_", "").lower() |
79 | 80 |
|
80 | 81 |
|
| 82 | +class ExecutionCountChecker(ResultVisitor): |
| 83 | + def __init__(self): |
| 84 | + self.test_names_counts = defaultdict(int) |
| 85 | + self.test_count = 0 |
| 86 | + |
| 87 | + def visit_test(self, test: TestCase): |
| 88 | + self.test_count += 1 |
| 89 | + test_name = test.name.split(" ")[0] |
| 90 | + self.test_names_counts[test_name] += 1 |
| 91 | + return super().visit_test(test) |
| 92 | + |
| 93 | + |
| 94 | +class ExecutionLogChecker(ResultVisitor): |
| 95 | + def __init__(self, match_start: str, kw_name: str = "Call And Validate"): |
| 96 | + self.match_start = match_start |
| 97 | + self.kw_name = kw_name.lower() |
| 98 | + self.test_logs = {} |
| 99 | + |
| 100 | + def visit_message(self, message: Message): |
| 101 | + if not message.message.startswith(self.match_start): |
| 102 | + return super().visit_message(message) |
| 103 | + if not ( |
| 104 | + message.parent |
| 105 | + and isinstance(message.parent, Keyword) |
| 106 | + and message.parent.name.lower() == self.kw_name |
| 107 | + ): |
| 108 | + return super().visit_message(message) |
| 109 | + parent = message.parent |
| 110 | + while not isinstance(parent, TestCase): |
| 111 | + if not parent.parent: |
| 112 | + return super().visit_message(message) |
| 113 | + parent = parent.parent |
| 114 | + test = parent |
| 115 | + logger.debug(f"test case: {test}, message: {message.message}") |
| 116 | + self.test_logs[test.name] = message.message |
| 117 | + return super().visit_message(message) |
| 118 | + |
| 119 | + |
81 | 120 | class TestSuiteChecker: |
82 | 121 | def check_suite(self, output_xml: str, test_description: list[TestCaseData]): |
83 | 122 | """Check the test suite for test cases names.""" |
@@ -108,6 +147,55 @@ def check_suite(self, output_xml: str, test_description: list[TestCaseData]): |
108 | 147 | self._check_test_logs(test.name, test.logs, checker.received_test_logs, should_match) |
109 | 148 | logger.info(f"Checked test suite in {output_xml}") |
110 | 149 |
|
| 150 | + def check_test_names_and_counts( |
| 151 | + self, output_xml: str, min_count: int, max_count: int, expected_names_and_counts: dict[str, int] |
| 152 | + ): |
| 153 | + """Check the test case start with correct names and has correct number of test cases.""" |
| 154 | + result = ExecutionResult(output_xml) |
| 155 | + checker = ExecutionCountChecker() |
| 156 | + result.visit(checker) |
| 157 | + result.save(output_xml) |
| 158 | + logger.info(f"Checked test suite in {output_xml}") |
| 159 | + assert checker.test_count >= min_count, ( |
| 160 | + f"Expected at least {min_count} tests, but found {checker.test_count}." |
| 161 | + ) |
| 162 | + assert checker.test_count <= max_count, ( |
| 163 | + f"Expected at most {max_count} tests, but found {checker.test_count}." |
| 164 | + ) |
| 165 | + for test_name, count in expected_names_and_counts.items(): |
| 166 | + assert test_name in checker.test_names_counts, ( |
| 167 | + f"Expected test name {test_name} not found in {checker.test_names_counts.keys()} " |
| 168 | + ) |
| 169 | + assert checker.test_names_counts[test_name] >= count, ( |
| 170 | + f"Expected {count} for test name {test_name} but it was {checker.test_names_counts[test_name]}" |
| 171 | + ) |
| 172 | + |
| 173 | + def check_specific_test_logs( |
| 174 | + self, |
| 175 | + output_xml: str, |
| 176 | + log_message_start: str, |
| 177 | + kw_name: str = "Call And Validate", |
| 178 | + number_of_tests: int = 15, |
| 179 | + string_in_log: None | str = None, |
| 180 | + ): |
| 181 | + assert number_of_tests > 0, "number_of_tests must be greater than 0" |
| 182 | + assert string_in_log, "string_in_log must be provided" |
| 183 | + result = ExecutionResult(output_xml) |
| 184 | + checker = ExecutionLogChecker(log_message_start, kw_name) |
| 185 | + result.visit(checker) |
| 186 | + result.save(output_xml) |
| 187 | + logger.info(f"Checked test suite in {output_xml}") |
| 188 | + assert len(checker.test_logs) == number_of_tests, ( |
| 189 | + f"Expected {number_of_tests} tests with logs starting with '{log_message_start}', " |
| 190 | + f"but found {len(checker.test_logs)}." |
| 191 | + ) |
| 192 | + for test, log in checker.test_logs.items(): |
| 193 | + logger.info(f"Checking log for test '{test}': {log}") |
| 194 | + assert string_in_log in log, ( |
| 195 | + f"Expected log to contain '{string_in_log}', but it was not found in log: {log} in test '{test}'." |
| 196 | + ) |
| 197 | + logger.debug(f"Found logs for tests: {checker.test_logs}") |
| 198 | + |
111 | 199 | def _check_test_logs( |
112 | 200 | self, |
113 | 201 | test_name: str, |
|
0 commit comments