def run_tests(self): """Run PyUnit and wait for it to terminate.""" suite_result = unittest.TestResult() self.cfg.suite.run(suite_result) # Since we can't reliably inspect the individual testcases of a PyUnit # suite, we put all results into a single "testcase" report. This # will only list failures and errors and not give detail on individual # assertions like with MultiTest. testcase_report = report_testing.TestCaseReport( name=self._TESTCASE_NAME) for call, error in suite_result.errors: assertion_obj = assertions.RawAssertion(description=str(call), content=str(error).strip(), passed=False) testcase_report.append( schemas.base.registry.serialize(assertion_obj)) for call, error in suite_result.failures: assertion_obj = assertions.RawAssertion(description=str(call), content=str(error).strip(), passed=False) testcase_report.append( schemas.base.registry.serialize(assertion_obj)) # In case of no failures or errors we need to explicitly mark the # testsuite as passed. if not testcase_report.entries: log_entry = base_entries.Log("All PyUnit testcases passed", description="PyUnit success") testcase_report.append(schemas.base.registry.serialize(log_entry)) self.result.report.append(testcase_report)
def run_tests(self): """Run PyUnit and wait for it to terminate.""" suite_result = unittest.TestResult() self.cfg.suite.run(suite_result) for call, error in suite_result.errors: testcase_report = report_testing.TestCaseReport(name=str(call)) assertion_obj = assertions.RawAssertion(description=str(call), content=str(error).strip(), passed=False) testcase_report.append( schemas.base.registry.serialize(assertion_obj)) self.result.report.entries.append(testcase_report) for call, error in suite_result.failures: testcase_report = report_testing.TestCaseReport(name=str(call)) assertion_obj = assertions.RawAssertion(description=str(call), content=str(error).strip(), passed=False) testcase_report.append( schemas.base.registry.serialize(assertion_obj)) self.result.report.entries.append(testcase_report) # Since we only store testcases for errors or assertion failures, # we need to explicitly override the report status to be PASSED if # no errors/failures occured. if not self.result.report.entries: self.result.report.status_override = report_testing.Status.PASSED
def _run_testsuite(self, pyunit_testcase): """Run a single PyUnit Testcase as a suite and return a testsuite report.""" suite = unittest.defaultTestLoader.loadTestsFromTestCase( pyunit_testcase ) suite_result = unittest.TextTestRunner().run(suite) # Since we can't reliably inspect the individual testcases of a PyUnit # suite, we put all results into a single "testcase" report. This # will only list failures and errors and not give detail on individual # assertions like with MultiTest. testcase_report = TestCaseReport( name=self._TESTCASE_NAME, uid=self._TESTCASE_NAME ) for call, error in suite_result.errors: assertion_obj = assertions.RawAssertion( description=str(call), content=str(error).strip(), passed=False ) testcase_report.append( schemas.base.registry.serialize(assertion_obj) ) for call, error in suite_result.failures: assertion_obj = assertions.RawAssertion( description=str(call), content=str(error).strip(), passed=False ) testcase_report.append( schemas.base.registry.serialize(assertion_obj) ) # In case of no failures or errors we need to explicitly mark the # testsuite as passed. if not testcase_report.entries: log_entry = entries_base.Log( "All PyUnit testcases passed", description="PyUnit success" ) testcase_report.append(schemas.base.registry.serialize(log_entry)) testcase_report.runtime_status = RuntimeStatus.FINISHED # We have to wrap the testcase report in a testsuite report. return TestGroupReport( name=pyunit_testcase.__name__, uid=pyunit_testcase.__name__, category=ReportCategories.TESTSUITE, entries=[testcase_report], )
def process_test_data(self, test_data): """ Convert JUnit output into a a list of report entries. :param test_data: JUnit test output. :type test_data: ``list`` :return: list of sub reports. :rtype: ``list`` of (``TestGroupReport`` or ``TestCaseReport``) """ result = [] for suite in test_data: suite_name = suite.get("name", "JUnit testsuite") suite_report = TestGroupReport( name=suite_name, uid=suite_name, category=ReportCategories.TESTSUITE, ) for case in suite.xpath("testcase"): case_name = case.get("name", "JUnit testcase") testcase_report = TestCaseReport(name=case_name, uid=case_name) for error in case.xpath("error"): testcase_report.append( registry.serialize( assertions.Fail("Error executing test") ) ) testcase_report.append( registry.serialize( CodeLog( error.text, language="java", description="stacktrace", ) ) ) for failure in case.xpath("failure"): message = failure.get("message", "testcase failure") testcase_report.append( registry.serialize(assertions.Fail(message)) ) if failure.text: testcase_report.append( registry.serialize( CodeLog( failure.text, language="java", description="stacktrace", ) ) ) if not testcase_report.entries: assertion_obj = assertions.RawAssertion( description="Passed", content="Testcase {} passed".format(case_name), passed=True, ) testcase_report.append(registry.serialize(assertion_obj)) testcase_report.runtime_status = RuntimeStatus.FINISHED suite_report.append(testcase_report)
def pytest_exception_interact(self, node, call, report): """ Hook called when an exception raised and it can be handled. This hook is only called if the exception is not an PyTest internal exception. :param node: PyTest Function or Module object :param call: PyTest CallInfo object :param report: PyTest TestReport or CollectReport object """ if call.when in ('memocollect', 'collect'): # Failed to collect tests: log to console and mark the report as # ERROR. self._report.logger.error(format_trace( inspect.getinnerframes(call.excinfo.tb), call.excinfo.value)) self._report.status_override = Status.ERROR elif self._current_case_report is not None: # Log assertion errors or exceptions in testcase report traceback = call.excinfo.traceback[-1] message = getattr(call.excinfo.value, 'message', None) or \ getattr(call.excinfo.value, 'msg', None) or \ getattr(call.excinfo.value, 'args', None) or '' if isinstance(message, (tuple, list)): message = message[0] header = (('Assertion - Fail' if call.excinfo.typename == 'AssertionError' else 'Exception raised') if call.when == 'call' else '{} - Fail'.format(call.when)) details = 'File: {}{}Line: {}{}{}: {}'.format( traceback.path.strpath, os.linesep, traceback.lineno + 1, os.linesep, call.excinfo.typename, message ) if call.excinfo.typename == 'AssertionError' else ( report.longreprtext if hasattr(report, 'longreprtext') else str(report.longrepr)) assertion_obj = assertions.RawAssertion( description=header, content=details, passed=False ) serialized_obj = schema_registry.serialize(assertion_obj) self._current_case_report.append(serialized_obj) self._current_case_report.status_override = Status.FAILED else: self._report.logger.error( 'Exception occured outside of a testcase: during %s', call.when) self._report.logger.error(format_trace( inspect.getinnerframes(call.excinfo.tb), call.excinfo.value))
def pytest_exception_interact(self, node, call, report): """ Hook called when an exception raised and it can be handled. This hook is only called if the exception is not an PyTest internal exception. :param node: PyTest Function or Module object :param call: PyTest CallInfo object :param report: PyTest TestReport or CollectReport object """ if call.when in ("memocollect", "collect"): # Failed to collect tests: log to console and mark the report as # ERROR. self._report.logger.error("".join( traceback.format_exception(call.excinfo.type, call.excinfo.value, call.excinfo.tb))) self._report.status_override = Status.ERROR elif self._current_case_report is not None: # Log assertion errors or exceptions in testcase report trace = call.excinfo.traceback[-1] message = (getattr(call.excinfo.value, "message", None) or getattr(call.excinfo.value, "msg", None) or getattr(call.excinfo.value, "args", None) or "") if isinstance(message, (tuple, list)): message = message[0] header = (("Assertion - Fail" if call.excinfo.typename == "AssertionError" else "Exception raised") if call.when == "call" else "{} - Fail".format(call.when)) details = ("File: {}\nLine: {}\n{}: {}".format( trace.path.strpath, trace.lineno + 1, call.excinfo.typename, message, ) if call.excinfo.typename == "AssertionError" else (report.longreprtext if hasattr(report, "longreprtext") else str(report.longrepr))) assertion_obj = assertions.RawAssertion(description=header, content=details, passed=False) serialized_obj = schema_registry.serialize(assertion_obj) self._current_case_report.append(serialized_obj) self._current_case_report.status_override = Status.FAILED else: self._report.logger.error( "Exception occured outside of a testcase: during %s", call.when) self._report.logger.error("".join( traceback.format_exception(call.excinfo.type, call.excinfo.value, call.excinfo.tb)))
def run_tests(self): """Run PyUnit and wait for it to terminate.""" suite_result = unittest.TestResult() self.cfg.suite.run(suite_result) for call, error in suite_result.errors: testcase_report = report_testing.TestCaseReport(name=str(call)) assertion_obj = assertions.RawAssertion(description=str(call), content=str(error).strip(), passed=False) testcase_report.append( schemas.base.registry.serialize(assertion_obj)) self.result.report.entries.append(testcase_report) for call, error in suite_result.failures: testcase_report = report_testing.TestCaseReport(name=str(call)) assertion_obj = assertions.RawAssertion(description=str(call), content=str(error).strip(), passed=False) testcase_report.append( schemas.base.registry.serialize(assertion_obj)) self.result.report.entries.append(testcase_report)