1e7e9171eSGeorge Keishing#!/usr/bin/env python3 213bdbffcSGeorge Keishing 313bdbffcSGeorge Keishingr""" 413bdbffcSGeorge KeishingUse robot framework API to extract test data from test suites. 513bdbffcSGeorge KeishingRefer to https://robot-framework.readthedocs.io/en/3.0.1/autodoc/robot.parsing.html 613bdbffcSGeorge Keishing""" 713bdbffcSGeorge Keishing 837c58c8cSGeorge Keishingimport os 9*20f38712SPatrick Williamsimport sys 10*20f38712SPatrick Williams 1137c58c8cSGeorge Keishingfrom robot.parsing.model import TestData 12*20f38712SPatrick Williams 1337c58c8cSGeorge Keishingsys.path.append(os.path.join(os.path.dirname(__file__), "../lib")) 1437c58c8cSGeorge Keishing 1509679890SGeorge Keishingfrom gen_arg import * # NOQA 1609679890SGeorge Keishingfrom gen_print import * # NOQA 1709679890SGeorge Keishingfrom gen_valid import * # NOQA 1813bdbffcSGeorge Keishing 1913bdbffcSGeorge Keishing# Set exit_on_error for gen_valid functions. 2013bdbffcSGeorge Keishingset_exit_on_error(True) 2113bdbffcSGeorge Keishing 22*20f38712SPatrick Williamsvalid_options = ["name", "tags", "doc", "all"] 2313bdbffcSGeorge Keishing 2413bdbffcSGeorge Keishingparser = argparse.ArgumentParser( 25*20f38712SPatrick Williams usage="%(prog)s [OPTIONS]", 26*20f38712SPatrick Williams description=( 27*20f38712SPatrick Williams ";%(prog)s will print test suite information to stdout. This " 28*20f38712SPatrick Williams " information consists of any and/or all of the following: " 29*20f38712SPatrick Williams " the suite name, test case names, tag names and doc" 30*20f38712SPatrick Williams " strings. Example for generated test case names " 31*20f38712SPatrick Williams " tests/test_basic_poweron.robot " 32*20f38712SPatrick Williams " Verify Front And Rear LED At Standby Power On Test" 33*20f38712SPatrick Williams " Check For Application Failures " 34*20f38712SPatrick Williams " Verify Uptime Average Against Threshold Test SSH" 35*20f38712SPatrick Williams " And IPMI Connections" 36*20f38712SPatrick Williams ), 3713bdbffcSGeorge Keishing formatter_class=argparse.ArgumentDefaultsHelpFormatter, 38*20f38712SPatrick Williams prefix_chars="-+", 39*20f38712SPatrick Williams) 4013bdbffcSGeorge Keishing 4113bdbffcSGeorge Keishingparser.add_argument( 42*20f38712SPatrick Williams "--source_path", "-s", help="The robot test file or directory path." 43*20f38712SPatrick Williams) 4413bdbffcSGeorge Keishing 4513bdbffcSGeorge Keishingparser.add_argument( 46*20f38712SPatrick Williams "--option", 47*20f38712SPatrick Williams "-o", 4813bdbffcSGeorge Keishing default="name", 49*20f38712SPatrick Williams help="Test case attribute name. This may be any one of the following:\n" 50*20f38712SPatrick Williams + sprint_var(valid_options), 51*20f38712SPatrick Williams) 5213bdbffcSGeorge Keishing 5313bdbffcSGeorge Keishing# Populate stock_list with options we want. 5413bdbffcSGeorge Keishingstock_list = [("test_mode", 0), ("quiet", 0), ("debug", 0)] 5513bdbffcSGeorge Keishing 5613bdbffcSGeorge Keishing 57*20f38712SPatrick Williamsdef exit_function(signal_number=0, frame=None): 5813bdbffcSGeorge Keishing r""" 5913bdbffcSGeorge Keishing Execute whenever the program ends normally or with the signals that we 6013bdbffcSGeorge Keishing catch (i.e. TERM, INT). 6113bdbffcSGeorge Keishing """ 6213bdbffcSGeorge Keishing 6313bdbffcSGeorge Keishing dprint_executing() 6413bdbffcSGeorge Keishing 6513bdbffcSGeorge Keishing dprint_var(signal_number) 6613bdbffcSGeorge Keishing 6713bdbffcSGeorge Keishing qprint_pgm_footer() 6813bdbffcSGeorge Keishing 6913bdbffcSGeorge Keishing 70*20f38712SPatrick Williamsdef signal_handler(signal_number, frame): 7113bdbffcSGeorge Keishing r""" 7213bdbffcSGeorge Keishing Handle signals. Without a function to catch a SIGTERM or SIGINT, the 7313bdbffcSGeorge Keishing program would terminate immediately with return code 143 and without 7413bdbffcSGeorge Keishing calling the exit_function. 7513bdbffcSGeorge Keishing """ 7613bdbffcSGeorge Keishing 7713bdbffcSGeorge Keishing # Our convention is to set up exit_function with atexit.register() so 7813bdbffcSGeorge Keishing # there is no need to explicitly call exit_function from here. 7913bdbffcSGeorge Keishing 8013bdbffcSGeorge Keishing dprint_executing() 8113bdbffcSGeorge Keishing 8213bdbffcSGeorge Keishing # Calling exit prevents us from returning to the code that was running 8313bdbffcSGeorge Keishing # when the signal was received. 8413bdbffcSGeorge Keishing exit(0) 8513bdbffcSGeorge Keishing 8613bdbffcSGeorge Keishing 8713bdbffcSGeorge Keishingdef validate_parms(): 8813bdbffcSGeorge Keishing r""" 8913bdbffcSGeorge Keishing Validate program parameters, etc. Return True or False (i.e. pass/fail) 9013bdbffcSGeorge Keishing accordingly. 9113bdbffcSGeorge Keishing """ 9213bdbffcSGeorge Keishing 9313bdbffcSGeorge Keishing valid_path(source_path) 9413bdbffcSGeorge Keishing 9513bdbffcSGeorge Keishing valid_value(option, valid_values=valid_options) 9613bdbffcSGeorge Keishing 9713bdbffcSGeorge Keishing gen_post_validation(exit_function, signal_handler) 9813bdbffcSGeorge Keishing 9913bdbffcSGeorge Keishing 10013bdbffcSGeorge Keishingdef parse_test_suites(source_path, option): 10113bdbffcSGeorge Keishing r""" 10213bdbffcSGeorge Keishing Parse the robot files and extract test data output. 10313bdbffcSGeorge Keishing 10413bdbffcSGeorge Keishing Description of argument(s): 10513bdbffcSGeorge Keishing source_path The path to a robot file or a directory of robot files. 10613bdbffcSGeorge Keishing option Test case attribute instances such as "name", 10713bdbffcSGeorge Keishing "tags" or "doc". 10813bdbffcSGeorge Keishing """ 10913bdbffcSGeorge Keishing if os.path.isfile(source_path): 11013bdbffcSGeorge Keishing file_paths = [source_path] 11113bdbffcSGeorge Keishing else: 112*20f38712SPatrick Williams file_paths = [ 113*20f38712SPatrick Williams os.path.join(path, file) 11413bdbffcSGeorge Keishing for (path, dirs, files) in os.walk(source_path) 115*20f38712SPatrick Williams for file in files 116*20f38712SPatrick Williams ] 11713bdbffcSGeorge Keishing 11813bdbffcSGeorge Keishing for file_path in file_paths: 11913bdbffcSGeorge Keishing print(file_path) 12013bdbffcSGeorge Keishing if "__init__.robot" in file_path: 12113bdbffcSGeorge Keishing continue 12213bdbffcSGeorge Keishing test_suite_obj = TestData(parent=None, source=file_path) 12313bdbffcSGeorge Keishing parse_test_file(test_suite_obj, option) 12413bdbffcSGeorge Keishing 12513bdbffcSGeorge Keishing 12613bdbffcSGeorge Keishingdef parse_test_file(test_suite_obj, option): 12713bdbffcSGeorge Keishing r""" 12813bdbffcSGeorge Keishing Extract test information from test suite object and print it to stdout in 12913bdbffcSGeorge Keishing the following format: 13013bdbffcSGeorge Keishing 13113bdbffcSGeorge Keishing <Test Case name> 13213bdbffcSGeorge Keishing <Test Tags name> 13313bdbffcSGeorge Keishing <Test Documentation> 13413bdbffcSGeorge Keishing 13513bdbffcSGeorge Keishing Description of argument(s): 13613bdbffcSGeorge Keishing test_suite_obj Test data suite object. 13713bdbffcSGeorge Keishing option Test case attribute instances such as "name", 13813bdbffcSGeorge Keishing "tags" or "doc". 13913bdbffcSGeorge Keishing """ 14013bdbffcSGeorge Keishing 14113bdbffcSGeorge Keishing for testcase in test_suite_obj.testcase_table: 14213bdbffcSGeorge Keishing if option == "name": 14313bdbffcSGeorge Keishing print(testcase.name) 14413bdbffcSGeorge Keishing elif option == "tags": 14513bdbffcSGeorge Keishing print(testcase.tags) 14613bdbffcSGeorge Keishing elif option == "doc": 14713bdbffcSGeorge Keishing print(testcase.doc) 14813bdbffcSGeorge Keishing elif option == "all": 14913bdbffcSGeorge Keishing print(testcase.name) 15013bdbffcSGeorge Keishing print(testcase.tags) 15113bdbffcSGeorge Keishing print(testcase.doc) 15213bdbffcSGeorge Keishing 15313bdbffcSGeorge Keishing 15413bdbffcSGeorge Keishingdef main(): 15513bdbffcSGeorge Keishing gen_get_options(parser, stock_list) 15613bdbffcSGeorge Keishing 15713bdbffcSGeorge Keishing validate_parms() 15813bdbffcSGeorge Keishing 15913bdbffcSGeorge Keishing qprint_pgm_header() 16013bdbffcSGeorge Keishing 16113bdbffcSGeorge Keishing parse_test_suites(source_path, option) 16213bdbffcSGeorge Keishing 16313bdbffcSGeorge Keishing return True 16413bdbffcSGeorge Keishing 16513bdbffcSGeorge Keishing 16613bdbffcSGeorge Keishing# Main 16713bdbffcSGeorge Keishingmain() 168