Beispiel #1
0
    def check_dependencies(self):
        """
        Checks for:
        - missing dependencies
        - circular dependencies
        - scope incoherence
        - forbidden fixture name
        raises FixtureError if a check fails
        """
        # first, check for forbidden fixture name
        for fixture_name in self._fixtures.keys():
            if fixture_name in FORBIDDEN_FIXTURE_NAMES:
                raise FixtureError("Fixture name '%s' is forbidden" %
                                   fixture_name)

        # second, check for missing & circular dependencies
        for fixture_name in self._fixtures.keys():
            self.get_fixture_dependencies(fixture_name)

        # third, check fixture scope compliance with their direct fixture dependencies
        for fixture in self._fixtures.values():
            dependency_fixtures = [
                self._fixtures[param] for param in fixture.params
                if param != "fixture_name"
            ]
            for dependency_fixture in dependency_fixtures:
                if dependency_fixture.get_scope_level(
                ) < fixture.get_scope_level():
                    raise FixtureError(
                        "Fixture '%s' with scope '%s' is incompatible with scope '%s' of fixture '%s'"
                        % (fixture.name, fixture.scope,
                           dependency_fixture.scope, dependency_fixture.name))
Beispiel #2
0
    def check_fixtures_in_suite(self, suite):
        for fixture in suite.get_fixtures():
            if fixture not in self._fixtures:
                raise FixtureError("Suite '%s' uses an unknown fixture '%s'" %
                                   (suite.path, fixture))
            if self._fixtures[fixture].get_scope_level(
            ) < SCOPE_LEVELS["suite"]:
                raise FixtureError(
                    "Suite '%s' uses fixture '%s' which has an incompatible scope"
                    % (suite.path, fixture))

        for test in suite.get_tests():
            self.check_fixtures_in_test(test)

        for sub_suite in suite.get_suites():
            self.check_fixtures_in_suite(sub_suite)
Beispiel #3
0
    def get_fixture_dependencies(self, name, ref_fixtures=()):
        fixture_params = [
            p for p in self._fixtures[name].params if p != "fixture_name"
        ]
        if any(ref_fixture in fixture_params for ref_fixture in ref_fixtures):
            raise FixtureError(
                "Fixture params %s have circular dependency on a fixture among %s"
                % (fixture_params, ref_fixtures))

        dependencies = OrderedSet()
        for param in fixture_params:
            if param not in self._fixtures:
                raise FixtureError(
                    "Fixture '%s' used by fixture '%s' does not exist" %
                    (param, name))
            dependencies.update(
                self.get_fixture_dependencies(param, (name, ) + ref_fixtures))
        dependencies.update(fixture_params)

        return dependencies
Beispiel #4
0
    def teardown(self):
        if not self._generator:
            return

        try:
            next(self._generator)
        except StopIteration:
            pass
        else:
            raise FixtureError(
                "The fixture yields more than once, only one yield is supported"
            )
Beispiel #5
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 #6
0
 def check_fixtures_in_test(self, test):
     for fixture in test.get_fixtures():
         if fixture not in self._fixtures:
             raise FixtureError("Unknown fixture '%s' used in test '%s'" %
                                (fixture, test.path))
Beispiel #7
0
 def add_fixture(self, fixture):
     if fixture.name in self._fixtures and self._fixtures[
             fixture.name].is_builtin():
         raise FixtureError("'%s' is a builtin fixture name" % fixture.name)
     self._fixtures[fixture.name] = fixture