George Keishing | e7e9171 | 2021-09-03 11:28:44 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 2 | |
| 3 | r""" |
| 4 | Use robot framework API to extract test data from test suites. |
| 5 | Refer to https://robot-framework.readthedocs.io/en/3.0.1/autodoc/robot.parsing.html |
| 6 | """ |
| 7 | |
George Keishing | 37c58c8 | 2022-12-08 07:42:54 -0600 | [diff] [blame] | 8 | import sys |
| 9 | import os |
| 10 | from robot.parsing.model import TestData |
| 11 | sys.path.append(os.path.join(os.path.dirname(__file__), "../lib")) |
| 12 | |
George Keishing | 0967989 | 2022-12-08 08:21:52 -0600 | [diff] [blame] | 13 | from gen_arg import * # NOQA |
| 14 | from gen_print import * # NOQA |
| 15 | from gen_valid import * # NOQA |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 16 | |
| 17 | # Set exit_on_error for gen_valid functions. |
| 18 | set_exit_on_error(True) |
| 19 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 20 | valid_options = ['name', 'tags', 'doc', 'all'] |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 21 | |
| 22 | parser = argparse.ArgumentParser( |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 23 | usage='%(prog)s [OPTIONS]', |
| 24 | description=";%(prog)s will print test suite information to stdout. This \ |
| 25 | information consists of any and/or all of the following: \ |
| 26 | the suite name, test case names, tag names and doc strings. \ |
| 27 | Example for generated test case names \ |
| 28 | tests/test_basic_poweron.robot \ |
| 29 | Verify Front And Rear LED At Standby \ |
| 30 | Power On Test \ |
| 31 | Check For Application Failures \ |
| 32 | Verify Uptime Average Against Threshold \ |
| 33 | Test SSH And IPMI Connections", |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 34 | formatter_class=argparse.ArgumentDefaultsHelpFormatter, |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 35 | prefix_chars='-+') |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 36 | |
| 37 | parser.add_argument( |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 38 | '--source_path', |
| 39 | '-s', |
| 40 | help='The robot test file or directory path.') |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 41 | |
| 42 | parser.add_argument( |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 43 | '--option', |
| 44 | '-o', |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 45 | default="name", |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 46 | help='Test case attribute name. This may be any one of the following:\n' |
| 47 | + sprint_var(valid_options)) |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 48 | |
| 49 | # Populate stock_list with options we want. |
| 50 | stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)] |
| 51 | |
| 52 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 53 | def exit_function(signal_number=0, |
| 54 | frame=None): |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 55 | r""" |
| 56 | Execute whenever the program ends normally or with the signals that we |
| 57 | catch (i.e. TERM, INT). |
| 58 | """ |
| 59 | |
| 60 | dprint_executing() |
| 61 | |
| 62 | dprint_var(signal_number) |
| 63 | |
| 64 | qprint_pgm_footer() |
| 65 | |
| 66 | |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 67 | def signal_handler(signal_number, |
| 68 | frame): |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 69 | r""" |
| 70 | Handle signals. Without a function to catch a SIGTERM or SIGINT, the |
| 71 | program would terminate immediately with return code 143 and without |
| 72 | calling the exit_function. |
| 73 | """ |
| 74 | |
| 75 | # Our convention is to set up exit_function with atexit.register() so |
| 76 | # there is no need to explicitly call exit_function from here. |
| 77 | |
| 78 | dprint_executing() |
| 79 | |
| 80 | # Calling exit prevents us from returning to the code that was running |
| 81 | # when the signal was received. |
| 82 | exit(0) |
| 83 | |
| 84 | |
| 85 | def validate_parms(): |
| 86 | r""" |
| 87 | Validate program parameters, etc. Return True or False (i.e. pass/fail) |
| 88 | accordingly. |
| 89 | """ |
| 90 | |
| 91 | valid_path(source_path) |
| 92 | |
| 93 | valid_value(option, valid_values=valid_options) |
| 94 | |
| 95 | gen_post_validation(exit_function, signal_handler) |
| 96 | |
| 97 | |
| 98 | def parse_test_suites(source_path, option): |
| 99 | r""" |
| 100 | Parse the robot files and extract test data output. |
| 101 | |
| 102 | Description of argument(s): |
| 103 | source_path The path to a robot file or a directory of robot files. |
| 104 | option Test case attribute instances such as "name", |
| 105 | "tags" or "doc". |
| 106 | """ |
| 107 | if os.path.isfile(source_path): |
| 108 | file_paths = [source_path] |
| 109 | else: |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 110 | file_paths = [os.path.join(path, file) |
| 111 | for (path, dirs, files) in os.walk(source_path) |
| 112 | for file in files] |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 113 | |
| 114 | for file_path in file_paths: |
George Keishing | bfa859a | 2020-04-02 00:38:24 -0500 | [diff] [blame] | 115 | print(file_path) |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 116 | if "__init__.robot" in file_path: |
| 117 | continue |
| 118 | test_suite_obj = TestData(parent=None, source=file_path) |
| 119 | parse_test_file(test_suite_obj, option) |
| 120 | |
| 121 | |
| 122 | def parse_test_file(test_suite_obj, option): |
| 123 | r""" |
| 124 | Extract test information from test suite object and print it to stdout in |
| 125 | the following format: |
| 126 | |
| 127 | <Test Case name> |
| 128 | <Test Tags name> |
| 129 | <Test Documentation> |
| 130 | |
| 131 | Description of argument(s): |
| 132 | test_suite_obj Test data suite object. |
| 133 | option Test case attribute instances such as "name", |
| 134 | "tags" or "doc". |
| 135 | """ |
| 136 | |
| 137 | for testcase in test_suite_obj.testcase_table: |
| 138 | if option == "name": |
George Keishing | bfa859a | 2020-04-02 00:38:24 -0500 | [diff] [blame] | 139 | print(testcase.name) |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 140 | elif option == "tags": |
George Keishing | bfa859a | 2020-04-02 00:38:24 -0500 | [diff] [blame] | 141 | print(testcase.tags) |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 142 | elif option == "doc": |
George Keishing | bfa859a | 2020-04-02 00:38:24 -0500 | [diff] [blame] | 143 | print(testcase.doc) |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 144 | elif option == "all": |
George Keishing | bfa859a | 2020-04-02 00:38:24 -0500 | [diff] [blame] | 145 | print(testcase.name) |
| 146 | print(testcase.tags) |
| 147 | print(testcase.doc) |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 148 | |
| 149 | |
| 150 | def main(): |
George Keishing | e635ddc | 2022-12-08 07:38:02 -0600 | [diff] [blame] | 151 | |
George Keishing | 13bdbff | 2018-05-31 02:52:22 -0500 | [diff] [blame] | 152 | gen_get_options(parser, stock_list) |
| 153 | |
| 154 | validate_parms() |
| 155 | |
| 156 | qprint_pgm_header() |
| 157 | |
| 158 | parse_test_suites(source_path, option) |
| 159 | |
| 160 | return True |
| 161 | |
| 162 | |
| 163 | # Main |
| 164 | main() |