def run(self): if os.path.isfile(self.path): print "Ouput must be a directory !" #path, filename = os.path.split(self.path) #self._processFile(path, filename, self.outputPath) return if os.path.isdir(self.path): self._processDirectory(self.path, self.outputPath) # copy css file to output path CaduceusHelper.copyResource(self.caduceusPath, self.outputPath, "caduceus.css") # Print statistics print "Assertions: %d" % self.results.getAssertionCount() print "Success: %d" % self.results.getAssertionTypeCount(CaduceusTemplateResults.SUCCESS) print "Failures: %d" % self.results.getAssertionTypeCount(CaduceusTemplateResults.FAILURE) print "Errors: %d" % self.results.getAssertionTypeCount(CaduceusTemplateResults.ERROR) # Generate execution report if self._reports[self.REPORT_HTML]: report = ReportHtml(self.results, self.outputPath, self.caduceusPath) report.generate() if self._reports[self.REPORT_JUNIT]: report = ReportJUnit(self.results, self.outputPath, self.caduceusPath) report.generate()
def _processFile(self, filePath, fileName, outFilePath): inputFullPath = os.path.join(filePath, fileName) inputBasenamePath, ext = os.path.splitext(inputFullPath) outputFullPath = os.path.join(outFilePath, fileName) if ext == ".html": if fileName[0] != "_": print " Processing file %s..." % inputFullPath template = CaduceusTemplateParser.parseTemplateFile(inputFullPath, self.rootPath, self.caduceusPath) if template: CaduceusHelper.ensurePathExists(outFilePath) outputFile = open(outputFullPath, 'w') try: # Load controller for file dictGlob = {} dictLoc = {} try: controllerName, _ext = os.path.splitext(fileName) controllerName = controllerName + "Test" sys.path.append(os.path.dirname(inputFullPath)) exec ("from %s import %s" % (controllerName, controllerName)) in dictGlob, dictLoc # We must copy local dictionnary into global, to allow some symbols resolution dictGlob.update(dictLoc) # Instanciate a controller in parsing context exec ("__caduceus_controler__ = %s()" % controllerName) in dictGlob, dictLoc # Bind all controller public methods as global methods for key in dir(dictLoc[controllerName]): if key[0] != "_": proc = eval("__caduceus_controler__.%s" % key, dictGlob, dictLoc) dictLoc[key] = proc except IOError: print "Warning: No controller file for '%s'" % inputFullPath except ImportError: print "Warning: No controller file for '%s'" % inputFullPath # Render template using context tmplResults = CaduceusTemplateResults(outputFullPath) result = template.render(dictGlob, dictLoc, tmplResults) self.results.addTemplateResult(tmplResults) outputFile.write(result) finally: outputFile.close() else: print " Skipping partial %s" % inputFullPath elif ext not in [".py", ".pyc"]: # File may be stylesheet, javasript or other resource # copy as it print " Copy file %s..." % inputFullPath CaduceusHelper.ensurePathExists(outFilePath) shutil.copyfile(inputFullPath, outputFullPath)
def _getAssertionsOverall(self, results): table = "" rows = "" for tmpltResult in results.getTemplateResults(): title = tmpltResult.getTitle() or "Untitled" url = CaduceusHelper.getHtmlRelativePath(tmpltResult.getTemplatePath(), self._reportPath) count = tmpltResult.getAssertionCount() failureCount = tmpltResult.getAssertionTypeCount(CaduceusTemplateResults.FAILURE) errorCount = tmpltResult.getAssertionTypeCount(CaduceusTemplateResults.ERROR) row = ReportHtml._createTag("td", '<a href="%s">%s</a>' % (url, tmpltResult.getTitle())) row += ReportHtml._createTag("td", count, {'class': self._getCellCountClass(count, False)}) row += ReportHtml._createTag("td", failureCount, {'class': self._getCellCountClass(failureCount, True)}) row += ReportHtml._createTag("td", errorCount, {'class': self._getCellCountClass(errorCount, True)}) rows += ReportHtml._createTag("tr", row) count = results.getAssertionCount() failureCount = results.getAssertionTypeCount(CaduceusTemplateResults.FAILURE) errorCount = results.getAssertionTypeCount(CaduceusTemplateResults.ERROR) row = ReportHtml._createTag("td", "Total") row += ReportHtml._createTag("td", count, {'class': self._getCellCountClass(count, False)}) row += ReportHtml._createTag("td", failureCount, {'class': self._getCellCountClass(failureCount, True)}) row += ReportHtml._createTag("td", errorCount, {'class': self._getCellCountClass(errorCount, True)}) rows += ReportHtml._createTag("tr", row, {'class': "total"}) table = ReportHtml._createTable(["Page", "Assertions", "Failures", "Errors"], rows) return ReportHtml._createTag( "div", [ReportHtml._createTag("h2", "Pages tests results"), table])
def _pageHeader(self): cssPath = CaduceusHelper.getHtmlPathToResource(self._reportPath, self._rootPath, "caduceus.css") logoPath = CaduceusHelper.getHtmlPathToResource(self._reportPath, self._rootPath, "caduceus-mini.png") img = ['success.png', 'failure.png'][self._result.hasFailed()] resultImgPath = CaduceusHelper.getHtmlPathToResource(self._reportPath, self._rootPath, img) favicoPath = CaduceusHelper.getHtmlPathToResource(self._reportPath, self._rootPath, "favicon.ico") html = "<html>\n<head>\n" html += ' <link rel="stylesheet" href="%s" type="text/css" />\n' % cssPath html += ' <link rel="shortcut icon" href="%s">' % favicoPath html += '</head>\n<body class="caduceus">\n' html += ReportHtml._createTag("div", [ ReportHtml._createTag("img", None, {"src" : resultImgPath, "class": "result"}), ReportHtml._createTag("h1", "Execution Report"), ReportHtml._createTag("img", None, {"src" : logoPath, "class": "caduceus_logo"}), ReportHtml._createTag("br", None, {"style" : "clear: both"}) ], {"class": "caduceus_report_head"} ) html += '<div class="caduceus_report">\n' return html
def generate(self): content = self._pageHeader() content += self._getAssertionsOverall(self._result) content += self._getAllErrors(self._result.getTemplateResults()) content += self._getAllFailures(self._result.getTemplateResults()) content += self._pageFooter() outputFile = open(self._reportFilePath, 'w') try: outputFile.write(content) CaduceusHelper.copyResource(self._caduceusPath, self._rootPath, "caduceus-mini.png") CaduceusHelper.copyResource(self._caduceusPath, self._rootPath, "success.png") CaduceusHelper.copyResource(self._caduceusPath, self._rootPath, "failure.png") CaduceusHelper.copyResource(self._caduceusPath, self._rootPath, "favicon.ico") finally: outputFile.close()
def _getAllFailures(self, tmpltResults): table = "" rows = "" for tmpltResult in tmpltResults: title = tmpltResult.getTitle() or "Untitled" url = CaduceusHelper.getHtmlRelativePath(tmpltResult.getTemplatePath(), self._reportPath) for errors in tmpltResult.getFailures(): rows += " <tr>\n " rows += '<td><a href="%s#%s">%s - %s</a></td>' % (url, errors[0], tmpltResult.getTitle(), errors[0]) rows += '<td class="message"><pre>%s</pre></td>' % errors[1] rows += '\n' rows += " </tr>\n" if rows: table = ReportHtml._createTable(["Link", "message"], rows) return ReportHtml._createTag( "div", [ReportHtml._createTag("h2", "Tests failures"), table]) return ""
def render(self, dictGlob, dictLoc, results): if self._caduceusPath: # Template is document root (ie: not a partial) cssPath = CaduceusHelper.getHtmlPathToResource(self._path, self._rootPath, "caduceus.css") stylesheetToken = CaduceusTemplateHtmlTag("link", [ ("rel", "stylesheet"), ("href", cssPath), ("type", "text/css")], True) if self._refHeadTag: # Insert Caduceus style sheet as first child self._refHeadTag.addTokenFirst(stylesheetToken) elif self._refHtmlTag: head = self._refHeadTag.addTokenFirst(CaduceusTemplateHtmlTag("head", [])) head.addTokenFirst(stylesheetToken) return "<!-- Generated By Caduceus (%s) -->\n\n%s" % \ ( strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()), CaduceusTemplateEntity.render(self, dictGlob, dictLoc, results)) return CaduceusTemplateEntity.render(self, dictGlob, dictLoc, results)