Exemplo n.º 1
0
def _():
    error = StopIteration("oh no")

    @fixture
    def raises_in_teardown():
        yield "a"
        print("stdout")
        sys.stderr.write("stderr")
        raise error

    @testable_test
    def t(fix=raises_in_teardown):
        pass

    cache = FixtureCache()
    t = Test(t, "")
    t.resolver.resolve_args(cache)

    teardown_results: List[TeardownResult] = cache.teardown_fixtures_for_scope(
        scope=Scope.Test, scope_key=t.id, capture_output=True)

    assert len(teardown_results) == 1
    assert teardown_results[0].captured_exception == error
    # Ensure that we still capture stdout, stderr on exception
    assert teardown_results[0].sout == "stdout\n"
    assert teardown_results[0].serr == "stderr"
Exemplo n.º 2
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 == {}
Exemplo n.º 3
0
def _(cache: FixtureCache = cache, events: List = recorded_events):
    cache.teardown_fixtures_for_scope(
        Scope.Module,
        testable_test.path,
        capture_output=False  # type: ignore[attr-defined]
    )

    assert events == ["teardown m"]
Exemplo n.º 4
0
def _(cache: FixtureCache = cache):
    cache.teardown_fixtures_for_scope(
        Scope.Module,
        testable_test.path,
        capture_output=True  # type: ignore[attr-defined]
    )

    fixtures_at_scope = cache.get_fixtures_at_scope(
        Scope.Module, testable_test.path)  # type: ignore[attr-defined]

    assert fixtures_at_scope == {}
Exemplo n.º 5
0
def _(cache: FixtureCache = cache):
    teardown_results = cache.teardown_global_fixtures(capture_output=True)

    # The cache contains a single global resolved fixture, which writes to sout/serr during teardown
    assert len(teardown_results) == 1
    assert teardown_results[0].sout == "stdout\n"
    assert teardown_results[0].serr == "stderr"
Exemplo n.º 6
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
Exemplo n.º 7
0
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
Exemplo n.º 8
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.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
Exemplo n.º 9
0
def _(cache: FixtureCache = cache):
    teardown_results = cache.teardown_fixtures_for_scope(Scope.Module,
                                                         testable_test.path,
                                                         capture_output=True)

    assert len(teardown_results) == 1
    assert teardown_results[0].sout == "stdout\n"
    assert teardown_results[0].serr == "stderr"
Exemplo n.º 10
0
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
Exemplo n.º 11
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())
Exemplo n.º 12
0
def _(cache: FixtureCache = cache, t: Test = my_test):
    teardown_results: List[TeardownResult] = cache.teardown_fixtures_for_scope(
        Scope.Test, t.id, capture_output=True)

    # There are 4 fixtures injected into the test. Only 2 are test-scoped, and teardown
    # code only runs in 1 of those.
    assert len(teardown_results) == 1
    assert teardown_results[0].captured_exception is None
    assert teardown_results[0].sout == "stdout\n"
    assert teardown_results[0].serr == "stderr"
Exemplo n.º 13
0
def cache():
    return FixtureCache()
Exemplo n.º 14
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
Exemplo n.º 15
0
def cache(t=my_test):
    c = FixtureCache()
    t.resolver.resolve_args(c)
    return c
Exemplo n.º 16
0
def _(f=exception_raising_fixture):
    cache = FixtureCache()
    cache.cache_fixture(f, "test_id")

    assert cache.get(f.key, Scope.Test, "test_id") == f
Exemplo n.º 17
0
def _(cache: FixtureCache = cache, events: List = recorded_events):
    cache.teardown_global_fixtures()

    assert events == ["teardown g"]
Exemplo n.º 18
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 == {}
Exemplo n.º 19
0
def _(cache: FixtureCache = cache, events: List = recorded_events):
    cache.teardown_fixtures_for_scope(Scope.Module, testable_test.path)

    assert events == ["teardown m"]
Exemplo n.º 20
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"]
Exemplo n.º 21
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 == {}