def test_parser(): """ Test the junit parser directly :return: """ junit = parser.Junit(filename=get_filepath("junit-simple_suite.xml")) assert len(junit.suites) == 1 assert len(junit.suites[0].properties) == 3 junit = parser.Junit(filename=get_filepath("junit-simple_suites.xml")) assert len(junit.suites) == 1 assert len(junit.suites[0].properties) == 3 junit = parser.Junit(filename=get_filepath("junit-complex_suites.xml")) assert len(junit.suites) == 66
def add_report(self, filename): """ Load a report into the matrix :param filename: :return: """ parsed = parser.Junit(filename=filename) filename = os.path.basename(filename) self.reports[filename] = parsed for suite in parsed.suites: for testclass in suite.classes: if testclass not in self.classes: self.classes[testclass] = {} if testclass not in self.casenames: self.casenames[testclass] = list() self.classes[testclass][filename] = suite.classes[testclass] for testcase in self.classes[testclass][filename].cases: basename = testcase.basename().strip() if basename not in self.casenames: self.casenames[testclass].append(basename) if testclass not in self.cases: self.cases[testclass] = {} if basename not in self.cases[testclass]: self.cases[testclass][basename] = {} self.cases[testclass][basename][filename] = testcase outcome = testcase.outcome() self.result_stats[outcome] = 1 + self.result_stats.get( outcome, 0)
def run(args): """ Run this tool :param args: :return: """ opts = PARSER.parse_args(args) if args else PARSER.parse_args() inputs = opts.REPORTS util = None if opts.merge_output: util = merge.Merger() for inputfile in inputs: util.add_report(inputfile) xmltext = util.toxmlstring() with open(opts.merge_output, "w") as outfile: outfile.write(xmltext) elif opts.text_matrix: util = matrix.TextReportMatrix() for filename in inputs: util.add_report(filename) print(util.summary()) elif opts.html_matrix: util = matrix.HtmlReportMatrix(os.path.dirname(opts.html_matrix)) for filename in inputs: util.add_report(filename) with open(opts.html_matrix, "w") as outfile: outfile.write(util.summary()) if util: if opts.fail: failed = util.failures() if len(failed) >= opts.fail: sys.exit(len(failed)) if opts.skip: skipped = util.skips() if len(skipped) >= opts.fail: sys.exit(len(skipped)) if not util: # legacy interface that we need to preserve # no options, one or two args, first is input file, optional second is output if len(opts.REPORTS) > 2: PARSER.print_usage() sys.exit(1) if len(opts.REPORTS) == 2: outfilename = opts.REPORTS[1] else: outfilename = opts.REPORTS[0] + ".html" report = parser.Junit(args[0]) html = report.html() with open(outfilename, "wb") as outfile: outfile.write(html.encode('utf-8'))
def test_parser_stringreader(): """ Test the junit parser when reading strings :return: """ with open(get_filepath("junit-complex_suites.xml"), "r") as data: junit = parser.Junit(xmlstring=data.read()) assert len(junit.suites) == 66 assert junit.suites[0].name == "Untitled suite in /Users/niko/Sites/casperjs/tests/suites/casper/agent.js" assert junit.suites[0].package == "tests/suites/casper/agent" assert junit.suites[0].classes["tests/suites/casper/agent"].cases[1].name == "Default user agent matches /plop/"
def test_parser(): """ Test the junit parser directly :return: """ junit = parser.Junit(filename=get_filepath("junit-simple_suite.xml")) assert len(junit.suites) == 1 assert len(junit.suites[0].properties) == 3 junit = parser.Junit(filename=get_filepath("junit-simple_suites.xml")) assert len(junit.suites) == 1 assert len(junit.suites[0].properties) == 3 junit = parser.Junit(filename=get_filepath("junit-complex_suites.xml")) assert len(junit.suites) == 66 junit = parser.Junit(filename=get_filepath("junit-cute2.xml")) assert len(junit.suites) == 6 junit = parser.Junit(filename=get_filepath("junit-unicode.xml")) assert len(junit.suites) == 1 assert len(junit.suites[0].classes) == 1 # different report structure, both files contain unicode symbols junit = parser.Junit(filename=get_filepath("junit-unicode2.xml")) assert len(junit.suites) == 1 assert len(junit.suites[0].classes) == 1
def run(args): """ Run this tool :param args: :return: """ (opts, args) = PARSER.parse_args(args) if args else PARSER.parse_args() if not len(args): PARSER.print_usage() sys.exit(1) outfilename = args[0] + ".html" if len(args) > 1: outfilename = args[1] report = parser.Junit(args[0]) html = report.html() with open(outfilename, "wb") as outfile: outfile.write(html)
def load_report(self, filename): """ Load a test report or folder :param filename: :return: """ if os.path.isfile(filename): report = parser.Junit(filename) for suite in report.suites: self.suites.append(suite) elif os.path.isdir(filename): # try importing all files in this folder for root, dirs, files in os.walk(filename): for filename in files: filepath = os.path.join(root, filename) if has_xml_header(filepath): try: self.load_report(filepath) except (parser.ParserError, ET.ParseError): pass
def run(args): """ Run this tool :param args: :return: """ (opts, args) = PARSER.parse_args(args) if args else PARSER.parse_args() if not len(args): PARSER.print_usage() sys.exit(1) if opts.merge_output: merger = merge.Merger() for inputfile in args: merger.load_report(inputfile) xmltext = merger.toxmlstring() with open(opts.merge_output, "w") as outfile: outfile.write(xmltext) elif opts.text_matrix: tmatrix = matrix.TextReportMatrix() for filename in args: tmatrix.add_report(filename) print(tmatrix.summary()) elif opts.html_matrix: hmatrix = matrix.HtmlReportMatrix(os.path.dirname(opts.html_matrix)) for filename in args: hmatrix.add_report(filename) with open(opts.html_matrix, "w") as outfile: outfile.write(hmatrix.summary()) else: outfilename = args[0] + ".html" if len(args) > 1: outfilename = args[1] report = parser.Junit(args[0]) html = report.html() with open(outfilename, "wb") as outfile: outfile.write(html.encode('utf-8'))
def test_binary_names(): # a test with nonsense binary in the case names junit = parser.Junit(filename=get_filepath("pytest-binary-names.xml")) assert junit
def main(self): # pragma: no cover """launcher logic""" self.logger.info('hello world') project_path = self.config.get('TEST_STEPS', 'project_path') local.cwd.chdir(project_path) self.logger.info('setting CWD: %s', local.cwd) git_repo = git.Repo() self.logger.info('Updating from MAIN') try: git_log = git_repo.git.pull() self.logger.debug(git_log) except Exception: self.logger.critical('Unable to pull project from git', exc_info=True) exit(1) self.logger.info('Starting Virtual Environment') try: self.venv_python, self.venv_pip = build_virtualenv( self.config.get('TEST_STEPS', 'venv_name'), which_python=self.config.get('TEST_STEPS', 'which_python'), logger=self.logger, ) except Exception: self.logger.critical('Unable to create virtualenv for test', exc_info=True) exit(1) self.logger.info('Preparing Environment') try: for command in parse_command_list( self.config.get('TEST_STEPS', 'prep_commands')): self.logger.info('--`%s`', command) local_command, arguments = self.parse_command(command) step_log = local_command(arguments) self.logger.debug(step_log) except Exception: self.logger.critical('Unable to execute test prep commands', exc_info=True) exit(1) self.logger.info('Running Tests') failed_logs = [] try: for command in parse_command_list( self.config.get('TEST_STEPS', 'test_commands')): self.logger.info('--`%s`', command) local_command, arguments = self.parse_command(command) retcode, stdout, stderr = local_command.run(arguments, retcode=[0, 1]) results = LocalResults(retcode, stdout, stderr) if retcode != 0: self.logger.warning('Test step failed: `%s`: %s', command, results) failed_logs.append(results) self.logger.debug(step_log) except Exception: self.logger.critical('Unable to execute test step commands', exc_info=True) exit(1) if failed_logs: self.logger.info('Processing failures') commit_name = git_repo.head.commit.message try: self.logger.info('--parsing JUNIT') # TODO: merge JUNIT in case of TOX reports junit = parser.Junit( self.config.get('TEST_STEPS', 'junit_path')) results = junit.html() except Exception: self.logger.warning('Unable to open junit results %s', self.config.get('TEST_STEPS', 'junit_path'), exc_info=True) results = '\n'.join(failed_logs) else: pass