| #!/usr/bin/python |
| # Copyright 2012 Google Inc. All Rights Reserved. |
| |
| """This class defines the base class for automated Bruno test cases. |
| |
| This file also defines some basic functions that is required for |
| most test cases. Users can define their own functions in the extended |
| class, the Run() method should be use as the start point of test case. |
| """ |
| |
| __author__ = 'Lehan Meng (lmeng@google.com)' |
| |
| import datetime |
| import logging |
| import re |
| |
| |
| class TestCase(object): |
| """Base class for all test cases. |
| |
| This base class provides basic functions for the test case: |
| - configure file parsing |
| - logging |
| - test information creation |
| - etc. |
| |
| Test case starts in the Run() method, test case specific code can be |
| placed in here. |
| """ |
| |
| def __init__(self, **kwargs): |
| """Initiate a test class instance. |
| |
| Args: |
| kwargs['testID']: The assigned test ID on TCM |
| kwargs['title']: Title (section number) of the test case |
| kwargs['start_time']: Start time of the test case (for statistic purpose) |
| kwargs['key_word']: the label of the test case |
| kwargs['description']: description if required |
| """ |
| self.test_info = { |
| 'testID': '0', |
| 'title': 'None', |
| 'start_time': str(datetime.datetime.now() |
| .strftime('%Y-%m-%d %H:%M')), |
| 'key_word': 'None', |
| 'description': 'None', |
| 'configFile': 'config.cfg'} |
| self.params = {} |
| |
| for s in ('testID', 'title', 'start_time', 'key_word', |
| 'description', 'configFile'): |
| if s in kwargs: |
| self.test_info[s] = kwargs[s] |
| |
| self.log = logging.Logging() |
| self.ParseConfig() |
| self.Run() |
| |
| def ParseConfig(self): |
| """Parse the configuration file for test case. |
| |
| The structure of the configuration file: |
| Each test case configuration begins with its testID in a sigle line, |
| the following lines have the format: |
| ############# configuration for Test 11362 ######################## |
| testID: 11362 |
| parameter_1 = value_1 |
| parameter_2 = value_2 |
| parameter_3 = value_3 |
| ... |
| |
| ############# configuration for Test 11363 ######################## |
| testID: 11363 |
| parameter_1 = value_1 |
| parameter_2 = value_2 |
| parameter_3 = value_3 |
| ... |
| ############# comment line start with '#' ######################### |
| |
| Returns: |
| reuturn 1 if succeed |
| """ |
| # dictionary for params |
| self.cfg_file = open(self.test_info['configFile'], 'r') |
| |
| cfg_list = self.cfg_file.readlines() |
| test_id_matched = False |
| |
| for line in cfg_list: |
| s = line.lstrip().strip().strip('\n') |
| m = re.match('testID: ' + str(self.test_info['testID']), s) |
| if (m is not None) and not s.startswith('#'): |
| # test case matching succeed |
| test_id_matched = True |
| # get next line index: |
| index = cfg_list.index(line)+1 |
| |
| if index >= len(cfg_list): |
| # end of file reached |
| info = self.log.createErrorInfo( |
| 'Low', |
| 'Warning: No configuration found for this test case in file: ' |
| + self.test_info['configFile']) |
| self.log.sendLine(self.test_info, info) |
| |
| s = cfg_list[index] |
| s = s.lstrip().rstrip() |
| s = s.split('#', 2)[0] |
| |
| m = re.match('testID: ', s) |
| while m is None: |
| # not reaching configuration for next test case, |
| # read and create current test case configuration data as dictionary |
| # format: parameter = value |
| if s is not '' and not s.startswith('#'): |
| name = s.split('=')[0].strip().lstrip() |
| value = s.split('=')[1].strip().lstrip() |
| if not value or value == 'None': |
| self.params[name] = None |
| else: |
| self.params[name] = value |
| |
| index += 1 |
| if index == len(cfg_list): break |
| s = cfg_list[index] |
| s = s.lstrip().rstrip() |
| m = re.match('testID: ', s) |
| |
| # all configuration parsed |
| self.cfg_file.close() |
| if not test_id_matched: |
| # end of file reached |
| info = self.log.createErrorInfo( |
| 'Low', 'Warning: No configuration found for this test case in file: ' |
| + self.test_info['configFile']) |
| self.log.sendLine(self.test_info, info) |
| return True |
| |
| def AddConfigParams(self, par='par', value='value'): |
| """Add new parameter to the params list. |
| |
| Args: |
| par: parameter name |
| value: parameter value |
| """ |
| self.params.setdefault(par.strip().lstrip(), value.strip().lstrip()) |
| |
| def __del__(self): |
| pass |
| |
| #################### define your own functions here ####################### |
| def Run(self): |
| """Starts to Run the test case.""" |
| ##### Add your code here -- BEGIN ####### |
| print 'Test Started...' |
| |
| print 'Test Completed...' |
| ##### Add your code here -- END ####### |
| |
| def Destructor(self): |
| pass |