blob: 4a2f1156f18d3375e734b0d3777f484de015d3d3 [file] [log] [blame]
George Keishinge7e91712021-09-03 11:28:44 -05001#!/usr/bin/env python3
George Keishing13bdbff2018-05-31 02:52:22 -05002
3r"""
4Use robot framework API to extract test data from test suites.
5Refer to https://robot-framework.readthedocs.io/en/3.0.1/autodoc/robot.parsing.html
6"""
7
George Keishing37c58c82022-12-08 07:42:54 -06008import sys
9import os
10from robot.parsing.model import TestData
11sys.path.append(os.path.join(os.path.dirname(__file__), "../lib"))
12
George Keishing09679892022-12-08 08:21:52 -060013from gen_arg import * # NOQA
14from gen_print import * # NOQA
15from gen_valid import * # NOQA
George Keishing13bdbff2018-05-31 02:52:22 -050016
17# Set exit_on_error for gen_valid functions.
18set_exit_on_error(True)
19
George Keishinge635ddc2022-12-08 07:38:02 -060020valid_options = ['name', 'tags', 'doc', 'all']
George Keishing13bdbff2018-05-31 02:52:22 -050021
22parser = argparse.ArgumentParser(
George Keishinge635ddc2022-12-08 07:38:02 -060023 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 Keishing13bdbff2018-05-31 02:52:22 -050034 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
George Keishinge635ddc2022-12-08 07:38:02 -060035 prefix_chars='-+')
George Keishing13bdbff2018-05-31 02:52:22 -050036
37parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060038 '--source_path',
39 '-s',
40 help='The robot test file or directory path.')
George Keishing13bdbff2018-05-31 02:52:22 -050041
42parser.add_argument(
George Keishinge635ddc2022-12-08 07:38:02 -060043 '--option',
44 '-o',
George Keishing13bdbff2018-05-31 02:52:22 -050045 default="name",
George Keishinge635ddc2022-12-08 07:38:02 -060046 help='Test case attribute name. This may be any one of the following:\n'
47 + sprint_var(valid_options))
George Keishing13bdbff2018-05-31 02:52:22 -050048
49# Populate stock_list with options we want.
50stock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)]
51
52
George Keishinge635ddc2022-12-08 07:38:02 -060053def exit_function(signal_number=0,
54 frame=None):
George Keishing13bdbff2018-05-31 02:52:22 -050055 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 Keishinge635ddc2022-12-08 07:38:02 -060067def signal_handler(signal_number,
68 frame):
George Keishing13bdbff2018-05-31 02:52:22 -050069 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
85def 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
98def 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 Keishinge635ddc2022-12-08 07:38:02 -0600110 file_paths = [os.path.join(path, file)
111 for (path, dirs, files) in os.walk(source_path)
112 for file in files]
George Keishing13bdbff2018-05-31 02:52:22 -0500113
114 for file_path in file_paths:
George Keishingbfa859a2020-04-02 00:38:24 -0500115 print(file_path)
George Keishing13bdbff2018-05-31 02:52:22 -0500116 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
122def 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 Keishingbfa859a2020-04-02 00:38:24 -0500139 print(testcase.name)
George Keishing13bdbff2018-05-31 02:52:22 -0500140 elif option == "tags":
George Keishingbfa859a2020-04-02 00:38:24 -0500141 print(testcase.tags)
George Keishing13bdbff2018-05-31 02:52:22 -0500142 elif option == "doc":
George Keishingbfa859a2020-04-02 00:38:24 -0500143 print(testcase.doc)
George Keishing13bdbff2018-05-31 02:52:22 -0500144 elif option == "all":
George Keishingbfa859a2020-04-02 00:38:24 -0500145 print(testcase.name)
146 print(testcase.tags)
147 print(testcase.doc)
George Keishing13bdbff2018-05-31 02:52:22 -0500148
149
150def main():
George Keishinge635ddc2022-12-08 07:38:02 -0600151
George Keishing13bdbff2018-05-31 02:52:22 -0500152 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
164main()