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 += " <meta http-equiv='Content-Type' content='Type=text/html; charset=utf-8'>" 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 run(self): if os.path.isfile(self.path): print("Output 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, self._jUnitPackagePrefix) report.generate() return self.results.failures == 0 and self.results.errors == 0
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), ) else: # Template is a partial # Find head tag head = self.findFirstTag("head") if head: cssPath = CaduceusHelper.getHtmlPathToResource(self._path, self._rootPath, "caduceus.css") # print("#### css path %s / %s / %s" % (self._path, self._rootPath, cssPath)) stylesheetToken = CaduceusTemplateHtmlTag( "link", [("rel", "stylesheet"), ("href", cssPath), ("type", "text/css")], True ) 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)
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], title, 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 _xmlSuite(self, results): content = "" for tmpltResult in results.getTemplateResults(): #_path, filename = os.path.split(tmpltResult.getTemplatePath()) #filename, _ext = os.path.splitext(filename) suiteName = CaduceusHelper.getHtmlRelativePath(tmpltResult.getTemplatePath(), self._rootPath) suiteName, _ext = os.path.splitext(suiteName) suiteName = suiteName.replace("/", ".") content += '<testsuite name="%s%s" errors="%d" failures="%d" tests="%d" >\n' \ % (self._packagePrefix, suiteName, tmpltResult.getAssertionTypeCount(CaduceusTemplateResults.ERROR), tmpltResult.getAssertionTypeCount(CaduceusTemplateResults.FAILURE), tmpltResult.getAssertionCount()) for assertion in tmpltResult.getAssertions(): content += self._xmlTestCase(assertion) content += "</testsuite>\n" return content
def __init__(self, path, templateFilter, outputPath, reports, jUnitPackagePrefix): sys.setrecursionlimit(10000) self.caduceusPath = CaduceusHelper.getPackagePath() self.path = os.path.abspath(path) self.rootPath = self.path if templateFilter: print("Filtering templates using '%s'" % templateFilter) self._templateFilter = re.compile(templateFilter) else: self._templateFilter = None self.outputPath = os.path.abspath(outputPath) self._reports = reports self._jUnitPackagePrefix = jUnitPackagePrefix self.results = CaduceusResults() os.environ["CADUCEUS_OUTPUT"] = self.outputPath os.environ["CADUCEUS_SRC"] = self.rootPath
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) executionTime = tmpltResult.getExecutionTime() row = ReportHtml._createTag("td", '<a href="%s">%s</a>' % (url, title)) 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)}) row += ReportHtml._createTag("td", executionTime, {"class": self._getCellTimeClass(executionTime)}) rows += ReportHtml._createTag("tr", row) count = results.getAssertionCount() failureCount = results.getAssertionTypeCount(CaduceusTemplateResults.FAILURE) errorCount = results.getAssertionTypeCount(CaduceusTemplateResults.ERROR) executionTime = results.getExecutionTime() 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)}) row += ReportHtml._createTag("td", executionTime, {"class": self._getCellTimeClass(executionTime)}) rows += ReportHtml._createTag("tr", row, {"class": "total"}) table = ReportHtml._createTable(["Page", "Assertions", "Failures", "Errors", "Execution Time (ms)"], rows) return ReportHtml._createTag("div", [ReportHtml._createTag("h2", "Pages tests results"), table])
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 self.allowedTemplateFile(filePath, fileName): 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 exec ("from %s import %s" % (controllerName, controllerName), 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 exec ("__caduceus_controler__ = %s()" % controllerName, dictGlob, dictLoc) os.environ["CADUCEUS_TEMPLATE_PATH"] = inputFullPath # 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) import traceback traceback.print_exc() # Render template using context tmplResults = CaduceusTemplateResults(outputFullPath) result = template.render(dictGlob, dictLoc, tmplResults) self.results.addTemplateResult(tmplResults) outputFile.write(result) finally: outputFile.close() 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)