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
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
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.test.scope_key_from(fixture.scope)): return cache.get(fixture.key, fixture.scope, self.test.scope_key_from(fixture.scope)) 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 fixture.is_generator_fixture: fixture.gen = arg(**args_to_inject) fixture.resolved_val = next( fixture.gen) # type: ignore[arg-type] elif fixture.is_async_generator_fixture: fixture.gen = arg(**args_to_inject) awaitable = fixture.gen.__anext__() # type: ignore[union-attr] fixture.resolved_val = asyncio.get_event_loop( ).run_until_complete(awaitable) elif fixture.is_coroutine_fixture: fixture.resolved_val = asyncio.get_event_loop( ).run_until_complete(arg(**args_to_inject)) else: fixture.resolved_val = arg(**args_to_inject) except (Exception, SystemExit) as e: raise FixtureError( f"Unable to resolve fixture '{fixture.name}'") from e scope_key = self.test.scope_key_from(fixture.scope) cache.cache_fixture(fixture, scope_key) return fixture