Beispiel #1
0
def run_suites(suites,
               fixture_registry,
               event_manager,
               force_disabled=False,
               stop_on_failure=False,
               nb_threads=1):
    fixture_teardowns = []

    # setup pre_session fixtures
    errors = []
    scheduled_fixtures = fixture_registry.get_fixtures_scheduled_for_session_prerun(
        suites)
    initialize_fixtures_cache(scheduled_fixtures)
    for setup, teardown in scheduled_fixtures.get_setup_teardown_pairs():
        try:
            setup()
        except UserError:
            raise
        except Exception:
            errors.append(
                "Got the following exception when executing fixture (scope 'session_prerun')%s"
                % (serialize_current_exception(show_stacktrace=True)))
            break
        fixture_teardowns.append(teardown)

    if not errors:
        report = run_session(suites,
                             fixture_registry,
                             scheduled_fixtures,
                             event_manager,
                             force_disabled=force_disabled,
                             stop_on_failure=stop_on_failure,
                             nb_threads=nb_threads)
    else:
        report = None

    # teardown pre_session fixtures
    for teardown in fixture_teardowns:
        try:
            teardown()
        except UserError:
            raise
        except Exception:
            errors.append(
                "Got the following exception on fixture teardown (scope 'session_prerun')%s"
                % (serialize_current_exception(show_stacktrace=True)))

    if errors:
        raise FixtureError("\n".join(errors))

    return report.is_successful() if report else False
Beispiel #2
0
def skip_task(task, context, completed_task_queue, reason=""):
    _debug("skip task %s" % task)
    try:
        task.skip(context, reason)
    except Exception:
        task.result = TaskResultException(serialize_current_exception())
    else:
        task.result = TaskResultSkipped()

    completed_task_queue.put(task)
Beispiel #3
0
def run_task(task, context, completed_task_queue):
    _debug("run task %s" % task)
    try:
        task.run(context)
    except TaskFailure as excp:
        task.result = TaskResultFailure(str(excp))
    except Exception:
        task.result = TaskResultException(serialize_current_exception())
    else:
        task.result = TaskResultSuccess()

    completed_task_queue.put(task)
def import_module(mod_filename):
    mod_dir = osp.dirname(mod_filename)
    mod_name = strip_py_ext(osp.basename(mod_filename))

    ###
    # Find module
    ###
    sys.path.insert(0, mod_dir)
    try:
        with warnings.catch_warnings():
            # would raise a warning since module's directory does not need to have a __init__.py
            warnings.simplefilter("ignore", ImportWarning)
            fh, path, description = imp.find_module(mod_name)
    except ImportError:
        raise ModuleImportError(
            "Cannot find module '%s': %s" % (mod_filename, serialize_current_exception(show_stacktrace=True))
        )
    finally:
        del sys.path[0]

    ###
    # Load module
    ###
    package_name = "".join(osp.splitdrive(mod_dir)[1].split(osp.sep)[1:])
    try:
        mod = imp.load_module(package_name + mod_name, fh, path, description)
    except ImportError:
        raise ModuleImportError(
            "Cannot import file '%s': %s" % (mod_filename, serialize_current_exception(show_stacktrace=True))
        )
    except Exception:
        raise ModuleImportError(
            "Error while importing file '%s': %s" % (mod_filename, serialize_current_exception(show_stacktrace=True))
        )
    finally:
        fh.close()

    return mod
Beispiel #5
0
def load_suite_from_class(class_):
    try:
        md = class_._lccmetadata
    except AttributeError:
        raise InvalidSuiteError()

    try:
        suite_obj = class_()
    except UserError as e:
        raise e  # propagate UserError
    except Exception:
        raise ProgrammingError("Got an unexpected error while instantiating suite class '%s':%s" % (
            class_.__name__, serialize_current_exception()
        ))
    ensure_node_is_visible(suite_obj, md)

    suite = Suite(suite_obj, md.name, md.description)
    suite.tags.extend(md.tags)
    suite.properties.update(md.properties)
    suite.links.extend(md.links)
    suite.rank = md.rank
    suite.disabled = md.disabled

    for hook_name in SUITE_HOOKS:
        if hasattr(suite_obj, hook_name):
            suite.add_hook(hook_name, getattr(suite_obj, hook_name))

    for test in load_tests_from_methods(get_test_methods_from_class(suite_obj)):
        suite.add_test(test)

    for test in get_generated_tests(suite_obj):
        suite.add_test(test)

    for sub_suite in load_suites_from_classes(get_sub_suites_from_class(suite_obj)):
        suite.add_suite(sub_suite)

    return suite
Beispiel #6
0
def load_project_from_file(project_filename):
    project_dir = osp.dirname(project_filename)

    # load project module
    try:
        project_config_module = import_module(project_filename)
    except UserError as e:
        raise e  # propagate UserError
    except Exception:
        raise ProjectError("Got an unexpected error while loading project:%s" %
                           (serialize_current_exception()))

    # get project config instance
    try:
        project_config = project_config_module.project
    except AttributeError:
        raise ProjectError("Cannot find symbol 'project' in module '%s'" %
                           project_filename)
    if not isinstance(project_config, ProjectConfiguration):
        raise ProjectError(
            "Symbol 'project' in module '%s' does not inherit lemoncheesecake.project.ProjectConfiguration"
            % project_filename)

    return Project(project_config, project_dir)
Beispiel #7
0
def run_project(project, cli_args):
    nb_threads = get_nb_threads(cli_args)
    if nb_threads > 1 and not project.is_threaded():
        raise LemonCheesecakeException("Project does not support multi-threading")

    suites = get_suites_from_project(project, cli_args)

    # Build fixture registry
    fixture_registry = build_fixture_registry(project, cli_args)
    fixture_registry.check_fixtures_in_suites(suites)

    # Set active reporting backends
    available_reporting_backends = {
        backend.name: backend for backend in
        filter_reporting_backends_by_capabilities(
            project.get_all_reporting_backends(), CAPABILITY_REPORTING_SESSION
        )
    }
    active_reporting_backends = set()
    for backend_name in cli_args.reporting + cli_args.enable_reporting:
        try:
            active_reporting_backends.add(available_reporting_backends[backend_name])
        except KeyError:
            raise LemonCheesecakeException("Unknown reporting backend '%s'" % backend_name)
    for backend_name in cli_args.disable_reporting:
        try:
            active_reporting_backends.discard(available_reporting_backends[backend_name])
        except KeyError:
            raise LemonCheesecakeException("Unknown reporting backend '%s'" % backend_name)

    # Get report save mode
    report_saving_strategy = get_report_saving_strategy(cli_args)

    # Create report dir
    if cli_args.report_dir:
        report_dir = cli_args.report_dir
        try:
            os.mkdir(report_dir)
        except Exception as e:
            return LemonCheesecakeException("Cannot create report directory: %s" % e)
    else:
        try:
            report_dir = project.create_report_dir()
        except UserError as e:
            raise e
        except Exception:
            raise LemonCheesecakeException(
                "Got an unexpected exception while creating report directory:%s" % \
                serialize_current_exception(show_stacktrace=True)
            )

    # Handle before run hook
    try:
        project.run_pre_session_hook(cli_args, report_dir)
    except UserError as e:
        raise e
    except Exception:
        raise ProgrammingError(
            "Got an unexpected exception while running the pre-session hook:%s" % \
            serialize_current_exception(show_stacktrace=True)
        )

    # Initialize event manager
    event_manager = initialize_event_manager(
        suites, active_reporting_backends, report_dir, report_saving_strategy, nb_threads
    )
    event_manager.add_listener(project)

    # Run tests
    is_successful = run_suites(
        suites, fixture_registry, event_manager,
        force_disabled=cli_args.force_disabled, stop_on_failure=cli_args.stop_on_failure,
        nb_threads=nb_threads
    )

    # Handle after run hook
    try:
        project.run_post_session_hook(cli_args, report_dir)
    except UserError as e:
        raise e
    except Exception:
        raise ProgrammingError(
            "Got an unexpected exception while running the post-session hook:%s" % \
            serialize_current_exception(show_stacktrace=True)
        )

    if cli_args.exit_error_on_failure:
        return 0 if is_successful else 1
    else:
        return 0