コード例 #1
0
def _(cache: FixtureCache = cache, ):
    cache.teardown_fixtures_for_scope(Scope.Module, testable_test.path)

    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Module,
                                                    testable_test.path)

    assert fixtures_at_scope == {}
コード例 #2
0
ファイル: test_fixtures.py プロジェクト: AndydeCleyre/ward
def _(cache: FixtureCache = cache, ):
    cache.teardown_fixtures_for_scope(Scope.Module, testable_test.path)

    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Module,
                                                    testable_test.path)

    expect(fixtures_at_scope).equals({})
コード例 #3
0
ファイル: testing.py プロジェクト: khusrokarim/ward
    def _resolve_single_fixture(
        self, fixture_fn: Callable, cache: FixtureCache
    ) -> Fixture:
        fixture = Fixture(fixture_fn)

        if fixture.key in cache:
            cached_fixture = cache[fixture.key]
            if fixture.scope == Scope.Global:
                return cached_fixture
            elif fixture.scope == Scope.Module:
                if cached_fixture.last_resolved_module_name == self.module_name:
                    return cached_fixture
            elif fixture.scope == Scope.Test:
                if cached_fixture.last_resolved_test_id == self.id:
                    return cached_fixture

        # Cache miss, so update the fixture metadata before we resolve and cache it
        fixture.last_resolved_test_id = self.id
        fixture.last_resolved_module_name = self.module_name

        has_deps = len(fixture.deps()) > 0
        is_generator = fixture.is_generator_fixture
        if not has_deps:
            try:
                if is_generator:
                    fixture.gen = fixture_fn()
                    fixture.resolved_val = next(fixture.gen)
                else:
                    fixture.resolved_val = fixture_fn()
            except Exception as e:
                raise FixtureError(f"Unable to resolve fixture '{fixture.name}'") from e
            cache.cache_fixture(fixture)
            return fixture

        signature = inspect.signature(fixture_fn)
        children_defaults = signature.bind_partial()
        children_defaults.apply_defaults()
        children_resolved = {}
        for name, child_fixture in children_defaults.arguments.items():
            child_resolved = self._resolve_single_fixture(child_fixture, cache)
            children_resolved[name] = child_resolved
        try:
            if is_generator:
                fixture.gen = fixture_fn(
                    **self._resolve_fixture_values(children_resolved)
                )
                fixture.resolved_val = next(fixture.gen)
            else:
                fixture.resolved_val = fixture_fn(
                    **self._resolve_fixture_values(children_resolved)
                )
        except Exception as e:
            raise FixtureError(f"Unable to resolve fixture '{fixture.name}'") from e
        cache.cache_fixture(fixture)
        return fixture
コード例 #4
0
ファイル: test_fixtures.py プロジェクト: AndydeCleyre/ward
def _(cache: FixtureCache = cache, global_fixture=global_fixture):
    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Global, Scope.Global)

    fixture = list(fixtures_at_scope.values())[0]

    expect(fixtures_at_scope).has_length(1)
    expect(fixture.fn).equals(global_fixture)
コード例 #5
0
ファイル: test_fixtures.py プロジェクト: skeledrew/ward
def _(cache: FixtureCache = cache, module_fixture=module_fixture):
    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Module, testable_test.path)

    fixture = list(fixtures_at_scope.values())[0]

    assert len(fixtures_at_scope) == 1
    assert fixture.fn == module_fixture
コード例 #6
0
ファイル: test_fixtures.py プロジェクト: skeledrew/ward
def _(cache: FixtureCache = cache, t: Test = my_test, default_fixture=default_fixture):
    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Test, t.id)

    fixture = list(fixtures_at_scope.values())[0]

    assert len(fixtures_at_scope) == 1
    assert fixture.fn == default_fixture
コード例 #7
0
def _(cache: FixtureCache = cache, global_fixture=global_fixture):
    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Global, Scope.Global)

    fixture = list(fixtures_at_scope.values())[0]

    assert len(fixtures_at_scope) == 1
    assert fixture.fn == global_fixture
コード例 #8
0
ファイル: test_fixtures.py プロジェクト: AndydeCleyre/ward
def _(cache: FixtureCache = cache, module_fixture=module_fixture):
    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Module,
                                                    testable_test.path)

    fixture = list(fixtures_at_scope.values())[0]

    expect(fixtures_at_scope).has_length(1)
    expect(fixture.fn).equals(module_fixture)
コード例 #9
0
ファイル: test_fixtures.py プロジェクト: AndydeCleyre/ward
def _(cache: FixtureCache = cache,
      t: Test = my_test,
      default_fixture=default_fixture):
    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Test, t.id)

    fixture = list(fixtures_at_scope.values())[0]

    expect(fixtures_at_scope).has_length(1)
    expect(fixture.fn).equals(default_fixture)
コード例 #10
0
def _(exit_code=each(0, 1)):
    @fixture
    def exits():
        sys.exit(exit_code)

    t = Test(fn=lambda exits=exits: None, module_name="foo")

    with raises(FixtureError):
        t.resolver.resolve_args(FixtureCache())
コード例 #11
0
    def _resolve_single_arg(self, arg: Callable,
                            cache: FixtureCache) -> Union[Any, Fixture]:
        """
        Get the fixture return value

        If the fixture has been cached, return the value from the cache.
        Otherwise, call the fixture function and return the value.
        """

        if not hasattr(arg, "ward_meta"):
            return arg

        fixture = Fixture(arg)
        if cache.contains(fixture, fixture.scope,
                          self.scope_key_from(fixture.scope)):
            return cache.get(fixture.key, fixture.scope,
                             self.scope_key_from(fixture.scope))

        has_deps = len(fixture.deps()) > 0
        is_generator = fixture.is_generator_fixture
        if not has_deps:
            try:
                if is_generator:
                    fixture.gen = arg()
                    fixture.resolved_val = next(fixture.gen)
                else:
                    fixture.resolved_val = arg()
            except Exception as e:
                raise FixtureError(
                    f"Unable to resolve fixture '{fixture.name}'") from e
            scope_key = self.scope_key_from(fixture.scope)
            cache.cache_fixture(fixture, scope_key)
            return fixture

        children_defaults = self._get_default_args(func=arg)
        children_resolved = {}
        for name, child_fixture in children_defaults.items():
            child_resolved = self._resolve_single_arg(child_fixture, cache)
            children_resolved[name] = child_resolved

        try:
            args_to_inject = self._unpack_resolved(children_resolved)
            if is_generator:
                fixture.gen = arg(**args_to_inject)
                fixture.resolved_val = next(fixture.gen)
            else:
                fixture.resolved_val = arg(**args_to_inject)
        except Exception as e:
            raise FixtureError(
                f"Unable to resolve fixture '{fixture.name}'") from e
        scope_key = self.scope_key_from(fixture.scope)
        cache.cache_fixture(fixture, scope_key)
        return fixture
コード例 #12
0
def _(cache: FixtureCache = cache,
      t: Test = my_test,
      events: List = recorded_events):
    cache.teardown_fixtures_for_scope(Scope.Test, t.id)

    assert events == ["teardown t"]
コード例 #13
0
def _(exit_code=each(0, 1), outcome=each(TestOutcome.FAIL, TestOutcome.FAIL)):
    t = Test(fn=lambda: sys.exit(exit_code), module_name=mod)

    assert t.run(FixtureCache()).outcome is outcome
コード例 #14
0
def _(cache: FixtureCache = cache, t: Test = my_test):
    cache.teardown_fixtures_for_scope(Scope.Test, t.id)

    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Test, t.id)

    assert fixtures_at_scope == {}
コード例 #15
0
ファイル: test_testing.py プロジェクト: skeledrew/ward
def cache():
    return FixtureCache()
コード例 #16
0
ファイル: test_fixtures.py プロジェクト: AndydeCleyre/ward
def _(cache: FixtureCache = cache, t: Test = my_test):
    cache.teardown_fixtures_for_scope(Scope.Test, t.id)

    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Test, t.id)

    expect(fixtures_at_scope).equals({})
コード例 #17
0
ファイル: test_fixtures.py プロジェクト: AndydeCleyre/ward
def _(cache: FixtureCache = cache,
      t: Test = my_test,
      events: List = recorded_events):
    cache.teardown_fixtures_for_scope(Scope.Test, t.id)

    expect(events).equals(["teardown t"])
コード例 #18
0
def cache(t=my_test):
    c = FixtureCache()
    t.resolver.resolve_args(c)
    return c
コード例 #19
0
def _(cache: FixtureCache = cache, ):
    cache.teardown_global_fixtures()

    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Global, Scope.Global)

    assert fixtures_at_scope == {}
コード例 #20
0
def _(cache: FixtureCache = cache, events: List = recorded_events):
    cache.teardown_fixtures_for_scope(Scope.Module, testable_test.path)

    assert events == ["teardown m"]
コード例 #21
0
ファイル: test_fixtures.py プロジェクト: AndydeCleyre/ward
def _(cache: FixtureCache = cache, events: List = recorded_events):
    cache.teardown_fixtures_for_scope(Scope.Module, testable_test.path)

    expect(events).equals(["teardown m"])
コード例 #22
0
def _(cache: FixtureCache = cache, events: List = recorded_events):
    cache.teardown_global_fixtures()

    assert events == ["teardown g"]
コード例 #23
0
ファイル: test_fixtures.py プロジェクト: AndydeCleyre/ward
def _(cache: FixtureCache = cache, ):
    cache.teardown_global_fixtures()

    fixtures_at_scope = cache.get_fixtures_at_scope(Scope.Global, Scope.Global)

    expect(fixtures_at_scope).equals({})
コード例 #24
0
def _(f=exception_raising_fixture):
    cache = FixtureCache()
    cache.cache_fixture(f, "test_id")

    assert cache.get(f.key, Scope.Test, "test_id") == f
コード例 #25
0
ファイル: test_fixtures.py プロジェクト: AndydeCleyre/ward
def _(cache: FixtureCache = cache, events: List = recorded_events):
    cache.teardown_global_fixtures()

    expect(events).equals(["teardown g"])
コード例 #26
0
def _(f=exception_raising_fixture):
    cache = FixtureCache()
    cache.cache_fixture(f)

    expect(cache[f.key]).equals(f)
コード例 #27
0
ファイル: test_fixtures.py プロジェクト: AndydeCleyre/ward
def _(f=exception_raising_fixture):
    cache = FixtureCache()
    cache.cache_fixture(f, "test_id")

    expect(cache.get(f.key, Scope.Test, "test_id")).equals(f)