def __init__(self, testPlan, flavours, testFactory): # Check test plan if (not os.path.exists(testPlan)) or (not os.path.isfile(testPlan)) \ or (not testPlan.endswith('.rtf')): raise TesterError(WRONG_TEST_PLAN) self.testPlan = testPlan self.testFolder = os.path.abspath(os.path.dirname(testPlan)) # Check flavours if (not isinstance(flavours, list)) and \ (not isinstance(flavours, tuple)): raise TesterError(FLAVOURS_NOT_LIST) for flavour in flavours: if not isinstance(flavour, basestring): raise TesterError(FLAVOUR_NOT_STRING) self.flavours = flavours self.flavour = None # Check test factory if not issubclass(testFactory, TestFactory): raise TesterError(WRONG_TEST_FACTORY) self.testFactory = testFactory self.getOptions() self.report = TestReport('%s/Tester.report.txt' % self.testFolder, self.verbose) self.report.say('Parsing RTF file... ') t1 = time.time() self.tables = RtfTablesParser(testPlan).parse() t2 = time.time() - t1 self.report.say('Done in %d seconds' % t2) self.config = None ext = '' if self.flavour: ext = '.%s' % self.flavour configTableName = 'Configuration%s' % ext if self.tables.has_key(configTableName): self.config = self.tables[configTableName].asDict() self.tempFolder = os.path.join(self.testFolder, 'temp') if os.path.exists(self.tempFolder): FolderDeleter.delete(self.tempFolder) self.nbOfTests = 0 self.nbOfSuccesses = 0 self.nbOfIgnoredTests = 0
class Tester: def __init__(self, testPlan, flavours, testFactory): # Check test plan if (not os.path.exists(testPlan)) or (not os.path.isfile(testPlan)) \ or (not testPlan.endswith('.rtf')): raise TesterError(WRONG_TEST_PLAN) self.testPlan = testPlan self.testFolder = os.path.abspath(os.path.dirname(testPlan)) # Check flavours if (not isinstance(flavours, list)) and \ (not isinstance(flavours, tuple)): raise TesterError(FLAVOURS_NOT_LIST) for flavour in flavours: if not isinstance(flavour, basestring): raise TesterError(FLAVOUR_NOT_STRING) self.flavours = flavours self.flavour = None # Check test factory if not issubclass(testFactory, TestFactory): raise TesterError(WRONG_TEST_FACTORY) self.testFactory = testFactory self.getOptions() self.report = TestReport('%s/Tester.report.txt' % self.testFolder, self.verbose) self.report.say('Parsing RTF file... ') t1 = time.time() self.tables = RtfTablesParser(testPlan).parse() t2 = time.time() - t1 self.report.say('Done in %d seconds' % t2) self.config = None ext = '' if self.flavour: ext = '.%s' % self.flavour configTableName = 'Configuration%s' % ext if self.tables.has_key(configTableName): self.config = self.tables[configTableName].asDict() self.tempFolder = os.path.join(self.testFolder, 'temp') if os.path.exists(self.tempFolder): FolderDeleter.delete(self.tempFolder) self.nbOfTests = 0 self.nbOfSuccesses = 0 self.nbOfIgnoredTests = 0 def getOptions(self): optParser = OptionParser() optParser.add_option("-v", "--verbose", action="store_true", help="Dumps the whole test report on stdout") optParser.add_option("-k", "--keepTemp", action="store_true", help = \ "Keep the temp folder, in order to be able to " \ "copy some results and make them expected " \ "results when needed.") (options, args) = optParser.parse_args() if self.flavours: if len(args) != 1: raise TesterError(WRONG_ARGS % self.flavours) self.flavour = args[0] if not self.flavour in self.flavours: raise TesterError(WRONG_FLAVOUR % (self.flavour, self.flavours)) self.verbose = options.verbose == True self.keepTemp = options.keepTemp == True def runSuite(self, suite): self.report.say('*' * 79) self.report.say('* Suite %s.' % suite['Name']) self.report.say('* %s\n' % suite['Description']) i = -1 for testData in self.tables['%s.data' % suite['Name']]: self.nbOfTests += 1 i += 1 if testData['Name'].startswith('_'): self.nbOfIgnoredTests += 1 else: description = self.tables['%s.descriptions' % \ suite['Name']][i]['Description'] test = self.testFactory.createTest( testData, description, self.testFolder, self.config, self.flavour) testFailed = test.run() if not self.verbose: sys.stdout.write('.') sys.stdout.flush() if testFailed: self.report.say('Test failed.\n') else: self.report.say('Test successful.\n') self.nbOfSuccesses += 1 def run(self): assert self.tables.has_key('TestSuites'), \ TesterError(MAIN_TABLE_NOT_FOUND % self.testPlan) for testSuite in self.tables['TestSuites']: if (not testSuite.has_key('Name')) or \ (not testSuite.has_key('Description')): raise TesterError(MAIN_TABLE_MALFORMED) if testSuite['Name'].startswith('_'): tsName = testSuite['Name'][1:] tsIgnored = True else: tsName = testSuite['Name'] tsIgnored = False assert self.tables.has_key('%s.descriptions' % tsName) \ and self.tables.has_key('%s.data' % tsName), \ TesterError(TEST_SUITE_NOT_FOUND % (tsName, tsName)) assert len(self.tables['%s.descriptions' % tsName]) == \ len(self.tables['%s.data' % tsName]), \ TesterError(TEST_SUITE_MALFORMED % ((tsName,)*4)) if tsIgnored: nbOfIgnoredTests = len(self.tables['%s.data' % tsName]) self.nbOfIgnoredTests += nbOfIgnoredTests self.nbOfTests += nbOfIgnoredTests else: self.runSuite(testSuite) self.finalize() def finalize(self): msg = '%d/%d successful test(s)' % \ (self.nbOfSuccesses, (self.nbOfTests-self.nbOfIgnoredTests)) if self.nbOfIgnoredTests >0: msg += ', but %d ignored test(s) not counted' % \ self.nbOfIgnoredTests msg += '.' self.report.say(msg, force=True) self.report.close() if not self.keepTemp: if os.path.exists(self.tempFolder): FolderDeleter.delete(self.tempFolder)
class Tester: def __init__(self, testPlan, flavours, testFactory): # Check test plan if (not os.path.exists(testPlan)) or (not os.path.isfile(testPlan)) \ or (not testPlan.endswith('.rtf')): raise TesterError(WRONG_TEST_PLAN) self.testPlan = testPlan self.testFolder = os.path.abspath(os.path.dirname(testPlan)) # Check flavours if (not isinstance(flavours, list)) and \ (not isinstance(flavours, tuple)): raise TesterError(FLAVOURS_NOT_LIST) for flavour in flavours: if not isinstance(flavour, basestring): raise TesterError(FLAVOUR_NOT_STRING) self.flavours = flavours self.flavour = None # Check test factory if not issubclass(testFactory, TestFactory): raise TesterError(WRONG_TEST_FACTORY) self.testFactory = testFactory self.getOptions() self.report = TestReport('%s/Tester.report.txt' % self.testFolder, self.verbose) self.report.say('Parsing RTF file... ') t1 = time.time() self.tables = RtfTablesParser(testPlan).parse() t2 = time.time() - t1 self.report.say('Done in %d seconds' % t2) self.config = None ext = '' if self.flavour: ext = '.%s' % self.flavour configTableName = 'Configuration%s' % ext if self.tables.has_key(configTableName): self.config = self.tables[configTableName].asDict() self.tempFolder = os.path.join(self.testFolder, 'temp') if os.path.exists(self.tempFolder): FolderDeleter.delete(self.tempFolder) self.nbOfTests = 0 self.nbOfSuccesses = 0 self.nbOfIgnoredTests = 0 def getOptions(self): optParser = OptionParser() optParser.add_option("-v", "--verbose", action="store_true", help="Dumps the whole test report on stdout") optParser.add_option("-k", "--keepTemp", action="store_true", help = \ "Keep the temp folder, in order to be able to " \ "copy some results and make them expected " \ "results when needed.") (options, args) = optParser.parse_args() if self.flavours: if len(args) != 1: raise TesterError(WRONG_ARGS % self.flavours) self.flavour = args[0] if not self.flavour in self.flavours: raise TesterError(WRONG_FLAVOUR % (self.flavour, self.flavours)) self.verbose = options.verbose == True self.keepTemp = options.keepTemp == True def runSuite(self, suite): self.report.say('*' * 79) self.report.say('* Suite %s.' % suite['Name']) self.report.say('* %s\n' % suite['Description']) i = -1 for testData in self.tables['%s.data' % suite['Name']]: self.nbOfTests += 1 i += 1 if testData['Name'].startswith('_'): self.nbOfIgnoredTests += 1 else: description = self.tables['%s.descriptions' % \ suite['Name']][i]['Description'] test = self.testFactory.createTest(testData, description, self.testFolder, self.config, self.flavour) testFailed = test.run() if not self.verbose: sys.stdout.write('.') sys.stdout.flush() if testFailed: self.report.say('Test failed.\n') else: self.report.say('Test successful.\n') self.nbOfSuccesses += 1 def run(self): assert self.tables.has_key('TestSuites'), \ TesterError(MAIN_TABLE_NOT_FOUND % self.testPlan) for testSuite in self.tables['TestSuites']: if (not testSuite.has_key('Name')) or \ (not testSuite.has_key('Description')): raise TesterError(MAIN_TABLE_MALFORMED) if testSuite['Name'].startswith('_'): tsName = testSuite['Name'][1:] tsIgnored = True else: tsName = testSuite['Name'] tsIgnored = False assert self.tables.has_key('%s.descriptions' % tsName) \ and self.tables.has_key('%s.data' % tsName), \ TesterError(TEST_SUITE_NOT_FOUND % (tsName, tsName)) assert len(self.tables['%s.descriptions' % tsName]) == \ len(self.tables['%s.data' % tsName]), \ TesterError(TEST_SUITE_MALFORMED % ((tsName,)*4)) if tsIgnored: nbOfIgnoredTests = len(self.tables['%s.data' % tsName]) self.nbOfIgnoredTests += nbOfIgnoredTests self.nbOfTests += nbOfIgnoredTests else: self.runSuite(testSuite) self.finalize() def finalize(self): msg = '%d/%d successful test(s)' % \ (self.nbOfSuccesses, (self.nbOfTests-self.nbOfIgnoredTests)) if self.nbOfIgnoredTests > 0: msg += ', but %d ignored test(s) not counted' % \ self.nbOfIgnoredTests msg += '.' self.report.say(msg, force=True) self.report.close() if not self.keepTemp: if os.path.exists(self.tempFolder): FolderDeleter.delete(self.tempFolder)