コード例 #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"
コード例 #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 == {}
コード例 #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"]
コード例 #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 == {}
コード例 #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"
コード例 #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
コード例 #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
コード例 #8
0
ファイル: testing.py プロジェクト: darrenburns/ward
    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
コード例 #9
0
ファイル: test_fixtures.py プロジェクト: darrenburns/ward
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"
コード例 #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
コード例 #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())
コード例 #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"
コード例 #13
0
ファイル: test_testing.py プロジェクト: AABur/ward
def cache():
    return FixtureCache()
コード例 #14
0
ファイル: test_testing.py プロジェクト: AABur/ward
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
コード例 #15
0
def cache(t=my_test):
    c = FixtureCache()
    t.resolver.resolve_args(c)
    return c
コード例 #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
コード例 #17
0
def _(cache: FixtureCache = cache, events: List = recorded_events):
    cache.teardown_global_fixtures()

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

    assert events == ["teardown m"]
コード例 #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"]
コード例 #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 == {}