def _(query=each("echo", "foobar", "wizbang")): fixtures = [Fixture(f) for f in [named_fixture, marker_fixture]] assert list(filter_fixtures(fixtures, query=query)) == []
def _(path=each(THIS_FILE, THIS_FILE.parent, THIS_FILE.parent.parent)): fixtures = [Fixture(f) for f in [named_fixture]] assert list(filter_fixtures(fixtures, paths=[path])) == fixtures
def _(query=each("test", "test_collect", "collect", "t_coll")): fixtures = [Fixture(f) for f in [named_fixture]] assert list(filter_fixtures(fixtures, query=query)) == fixtures
def _(query=each("marker", "mark", "ret", "return", '"')): fixtures = [Fixture(f) for f in [named_fixture, marker_fixture]] assert list(filter_fixtures(fixtures, query=query)) == [Fixture(marker_fixture)]
def _(): fixtures = [Fixture(f) for f in [named_fixture, marker_fixture]] assert list(filter_fixtures(fixtures)) == fixtures
def _(query=each("named_fixture", "named", "fixture", "med_fix")): fixtures = [Fixture(f) for f in [named_fixture]] assert list(filter_fixtures(fixtures, query=query)) == fixtures
def _(): @fixture def fix(): pass assert len(Fixture(fix).parents()) == 0
def _(path=each(THIS_FILE.parent / "the-fixture-is-not-in-this-file.py")): fixtures = [Fixture(f) for f in [named_fixture]] assert list(filter_fixtures(fixtures, paths=[path])) == []
def exception_raising_fixture(): @fixture def i_raise_an_exception(): raise ZeroDivisionError() return Fixture(fn=i_raise_an_exception)
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 if not has_deps: try: if fixture.is_generator_fixture: fixture.gen = arg() fixture.resolved_val = next(fixture.gen) elif fixture.is_async_generator_fixture: fixture.gen = arg() awaitable = fixture.gen.__anext__() 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()) 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 fixture.is_generator_fixture: fixture.gen = arg(**args_to_inject) fixture.resolved_val = next(fixture.gen) elif fixture.is_async_generator_fixture: fixture.gen = arg(**args_to_inject) awaitable = fixture.gen.__anext__() 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 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 fixtures(self) -> Dict[str, Fixture]: return { name: Fixture(arg) for name, arg in self._get_args_for_iteration().items() if is_fixture(arg) }
def fixtures(a=fixture_a, b=fixture_b): return {"fixture_a": Fixture(fn=a), "fixture_b": Fixture(fn=b)}
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