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