Esempio n. 1
0
def _is_exception_capture_enabled(item):
    """returns if exception capture is disabled for the given test item.
    """
    disabled = get_marker(item, "qt_no_exception_capture") or item.config.getini(
        "qt_no_exception_capture"
    )
    return not disabled
Esempio n. 2
0
 def pytest_runtest_setup(self, item):
     if get_marker(item, "no_qt_log"):
         return
     m = get_marker(item, "qt_log_ignore")
     if m:
         if not set(m.kwargs).issubset({"extend"}):
             raise ValueError("Invalid keyword arguments in {!r} for "
                              "qt_log_ignore mark.".format(m.kwargs))
         if m.kwargs.get("extend", True):
             config_regexes = self.config.getini("qt_log_ignore")
             ignore_regexes = config_regexes + list(m.args)
         else:
             ignore_regexes = m.args
     else:
         ignore_regexes = self.config.getini("qt_log_ignore")
     item.qt_log_capture = _QtMessageCapture(ignore_regexes)
     item.qt_log_capture._start()
Esempio n. 3
0
 def pytest_runtest_setup(self, item):
     if get_marker(item, "no_qt_log"):
         return
     m = get_marker(item, "qt_log_ignore")
     if m:
         if not set(m.kwargs).issubset({"extend"}):
             raise ValueError(
                 "Invalid keyword arguments in {!r} for "
                 "qt_log_ignore mark.".format(m.kwargs)
             )
         if m.kwargs.get("extend", True):
             config_regexes = self.config.getini("qt_log_ignore")
             ignore_regexes = config_regexes + list(m.args)
         else:
             ignore_regexes = m.args
     else:
         ignore_regexes = self.config.getini("qt_log_ignore")
     item.qt_log_capture = _QtMessageCapture(ignore_regexes)
     item.qt_log_capture._start()
Esempio n. 4
0
    def pytest_runtest_makereport(self, item, call):
        """Add captured Qt messages to test item report if the call failed."""
        outcome = yield
        if not hasattr(item, 'qt_log_capture'):
            return

        if call.when == 'call':
            report = outcome.get_result()

            m = get_marker(item, 'qt_log_level_fail')
            if m:
                log_fail_level = m.args[0]
            else:
                log_fail_level = self.config.getini('qt_log_level_fail')
            assert log_fail_level in QtLoggingPlugin.LOG_FAIL_OPTIONS

            # make test fail if any records were captured which match
            # log_fail_level
            if log_fail_level != 'NO' and report.outcome != 'failed':
                for rec in item.qt_log_capture.records:
                    if rec.matches_level(log_fail_level) and not rec.ignored:
                        report.outcome = 'failed'
                        if report.longrepr is None:
                            report.longrepr = \
                                _QtLogLevelErrorRepr(item, log_fail_level)
                        break

            # if test has failed, add recorded messages to its terminal
            # representation
            if not report.passed:
                long_repr = getattr(report, 'longrepr', None)
                if hasattr(long_repr, 'addsection'):  # pragma: no cover
                    log_format = self.config.getoption('qt_log_format')
                    if log_format is None:
                        if qt_api.pytest_qt_api == 'pyqt5':
                            log_format = '{rec.context.file}:{rec.context.function}:' \
                                      '{rec.context.line}:\n    {rec.type_name}: {rec.message}'
                        else:
                            log_format = '{rec.type_name}: {rec.message}'
                    lines = []
                    for rec in item.qt_log_capture.records:
                        suffix = ' (IGNORED)' if rec.ignored else ''
                        line = log_format.format(rec=rec) + suffix
                        lines.append(line)
                    if lines:
                        long_repr.addsection('Captured Qt messages',
                                             '\n'.join(lines))

            item.qt_log_capture._stop()
            del item.qt_log_capture
Esempio n. 5
0
    def pytest_runtest_makereport(self, item, call):
        """Add captured Qt messages to test item report if the call failed."""
        outcome = yield
        if not hasattr(item, "qt_log_capture"):
            return

        if call.when == "call":
            report = outcome.get_result()

            m = get_marker(item, "qt_log_level_fail")
            if m:
                log_fail_level = m.args[0]
            else:
                log_fail_level = self.config.getini("qt_log_level_fail")
            assert log_fail_level in QtLoggingPlugin.LOG_FAIL_OPTIONS

            # make test fail if any records were captured which match
            # log_fail_level
            if report.outcome != "failed":
                for rec in item.qt_log_capture.records:
                    is_modeltest_error = (rec.context is not None
                                          and rec.context.category
                                          == "qt.modeltest"
                                          and rec.matches_level("WARNING"))
                    if (rec.matches_level(log_fail_level)
                            and not rec.ignored) or is_modeltest_error:
                        report.outcome = "failed"
                        if report.longrepr is None:
                            report.longrepr = _QtLogLevelErrorRepr(
                                item, log_fail_level, is_modeltest_error)
                        break

            # if test has failed, add recorded messages to its terminal
            # representation
            if not report.passed:
                long_repr = getattr(report, "longrepr", None)
                if hasattr(long_repr, "addsection"):  # pragma: no cover
                    log_format = self.config.getoption("qt_log_format")
                    context_format = None
                    if log_format is None:
                        context_format = "{rec.context.file}:{rec.context.function}:{rec.context.line}:\n"
                        log_format = "    {rec.type_name}: {rec.message}"

                    lines = []
                    for rec in item.qt_log_capture.records:
                        suffix = " (IGNORED)" if rec.ignored else ""

                        if (rec.context is not None
                                and (rec.context.file is not None
                                     or rec.context.function is not None
                                     or rec.context.line != 0)
                                and context_format is not None):
                            context_line = context_format.format(rec=rec)
                            lines.append(context_line)
                        else:
                            log_format = log_format.lstrip()

                        line = log_format.format(rec=rec) + suffix
                        lines.append(line)
                    if lines:
                        long_repr.addsection("Captured Qt messages",
                                             "\n".join(lines))

            item.qt_log_capture._stop()
            del item.qt_log_capture
Esempio n. 6
0
    def pytest_runtest_makereport(self, item, call):
        """Add captured Qt messages to test item report if the call failed."""
        outcome = yield
        if not hasattr(item, "qt_log_capture"):
            return

        if call.when == "call":
            report = outcome.get_result()

            m = get_marker(item, "qt_log_level_fail")
            if m:
                log_fail_level = m.args[0]
            else:
                log_fail_level = self.config.getini("qt_log_level_fail")
            assert log_fail_level in QtLoggingPlugin.LOG_FAIL_OPTIONS

            # make test fail if any records were captured which match
            # log_fail_level
            if report.outcome != "failed":
                for rec in item.qt_log_capture.records:
                    is_modeltest_error = (
                        rec.context is not None
                        and rec.context.category == "qt.modeltest"
                        and rec.matches_level("WARNING")
                    )
                    if (
                        rec.matches_level(log_fail_level) and not rec.ignored
                    ) or is_modeltest_error:
                        report.outcome = "failed"
                        if report.longrepr is None:
                            report.longrepr = _QtLogLevelErrorRepr(
                                item, log_fail_level, is_modeltest_error
                            )
                        break

            # if test has failed, add recorded messages to its terminal
            # representation
            if not report.passed:
                long_repr = getattr(report, "longrepr", None)
                if hasattr(long_repr, "addsection"):  # pragma: no cover
                    log_format = self.config.getoption("qt_log_format")
                    context_format = None
                    if log_format is None:
                        if qt_api.pytest_qt_api == "pyqt5":
                            context_format = "{rec.context.file}:{rec.context.function}:{rec.context.line}:\n"
                            log_format = "    {rec.type_name}: {rec.message}"
                        else:
                            context_format = None
                            log_format = "{rec.type_name}: {rec.message}"
                    lines = []
                    for rec in item.qt_log_capture.records:
                        suffix = " (IGNORED)" if rec.ignored else ""

                        if (
                            rec.context is not None
                            and (
                                rec.context.file is not None
                                or rec.context.function is not None
                                or rec.context.line != 0
                            )
                            and context_format is not None
                        ):
                            context_line = context_format.format(rec=rec)
                            lines.append(context_line)

                        line = log_format.format(rec=rec) + suffix
                        lines.append(line)
                    if lines:
                        long_repr.addsection("Captured Qt messages", "\n".join(lines))

            item.qt_log_capture._stop()
            del item.qt_log_capture