Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #57 from darrenburns/hotfix-ensure-using-binds-cor…
Browse files Browse the repository at this point in the history
…rectly

Hotfix ensure using binds correctly
  • Loading branch information
darrenburns authored Nov 9, 2019
2 parents 8d89d8c + 4221c7e commit 47f2347
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import setup

version = "0.17.0a0"
version = "0.17.1a0"
description = "A modern Python 3 test framework for finding and fixing flaws faster."
with open("README.md", "r") as fh:
if platform.system() != "Windows":
Expand Down
32 changes: 26 additions & 6 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from tests.test_suite import testable_test
from ward import expect, fixture, test, Scope
from ward.fixtures import Fixture, FixtureCache
from ward.fixtures import Fixture, FixtureCache, using
from ward.testing import Test


Expand Down Expand Up @@ -126,22 +126,22 @@ def _(
@test("FixtureCache.teardown_fixtures_for_scope removes Test fixtures from cache")
def _(
cache: FixtureCache = cache,
test: Test = my_test,
t: Test = my_test,
):
cache.teardown_fixtures_for_scope(Scope.Test, test.id)
cache.teardown_fixtures_for_scope(Scope.Test, t.id)

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

expect(fixtures_at_scope).equals({})


@test("FixtureCache.teardown_fixtures_for_scope runs teardown for Test fixtures")
def _(
cache: FixtureCache = cache,
test: Test = my_test,
t: Test = my_test,
events: List = recorded_events,
):
cache.teardown_fixtures_for_scope(Scope.Test, test.id)
cache.teardown_fixtures_for_scope(Scope.Test, t.id)

expect(events).equals(["teardown t"])

Expand Down Expand Up @@ -186,3 +186,23 @@ def _(
cache.teardown_global_fixtures()

expect(events).equals(["teardown g"])


@test("using decorator sets bound args correctly")
def _():
@fixture
def fixture_a():
pass

@testable_test
@using(
a=fixture_a,
b="val",
)
def t(a, b):
pass

bound_args = t.ward_meta.bound_args
expected = {"a": fixture_a, "b": "val"}

expect(bound_args.arguments).equals(expected)
5 changes: 4 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from tests.test_suite import example_test
from ward import expect, test
from ward import expect, test, using
from ward.testing import TestOutcome, TestResult
from ward.util import ExitCode, get_exit_code


@test(
"get_exit_code returns ExitCode.SUCCESS when PASS, SKIP and XFAIL in test results"
)
@using(
example=example_test,
)
def _(example=example_test):
test_results = [
TestResult(test=example, outcome=TestOutcome.PASS),
Expand Down
1 change: 0 additions & 1 deletion ward/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ def wrapper(*args, **kwargs):

def using(*using_args, **using_kwargs):
def decorator_using(func):

signature = inspect.signature(func)
bound_args = signature.bind_partial(*using_args, **using_kwargs)
if hasattr(func, "ward_meta"):
Expand Down
4 changes: 2 additions & 2 deletions ward/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass
from enum import Enum
from inspect import Signature
from inspect import BoundArguments
from typing import Optional

from ward.errors import FixtureError
Expand Down Expand Up @@ -42,5 +42,5 @@ class WardMeta:
description: Optional[str] = None
is_fixture: bool = False
scope: Scope = Scope.Test
bound_args: Optional[Signature] = None
bound_args: Optional[BoundArguments] = None
path: Optional[str] = None
29 changes: 20 additions & 9 deletions ward/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,13 +211,26 @@ def get_result(self, outcome, exception=None):
)
return result

def _get_default_args(self) -> Dict[str, Any]:
def _get_default_args(self, func: Optional[Union[Callable, Fixture]] = None) -> Dict[str, Any]:
"""
Returns a mapping of test argument names to values. This method does no
fixture resolution. If a value is a fixture function, then the raw fixture
function is used, *not* the `Fixture` object.
Returns a mapping of test argument names to values.
This method does no fixture resolution.
If a value is a fixture function, then the raw fixture
function is returned as a value in the dict, *not* the `Fixture` object.
"""
signature = inspect.signature(self.fn)
fn = func or self.fn
meta = getattr(fn, "ward_meta", None)
signature = inspect.signature(fn)

# Override the signature if @using is present
if meta:
bound_args = getattr(fn.ward_meta, "bound_args", None)
if bound_args:
bound_args.apply_defaults()
return bound_args.arguments

default_binding = signature.bind_partial()
default_binding.apply_defaults()
return default_binding.arguments
Expand Down Expand Up @@ -271,11 +284,9 @@ def _resolve_single_arg(
cache.cache_fixture(fixture, scope_key)
return fixture

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

Expand Down

0 comments on commit 47f2347

Please sign in to comment.