Commits

Zero Piraeus committed 8849fc5

Add working @fixture decorator.

  • Participants
  • Parent commits 6f3c946

Comments (0)

Files changed (2)

File fix/decorators.py

 """Decorators."""
 
+from functools import wraps
 
-def fixture():
-    """Decorate a callable with a test fixture."""
-    pass
+
+def fixture(create_fixture, **kwargs):
+    """Decorate a callable with a fixture."""
+    context = {}
+    fixt = create_fixture(context, **kwargs)
+    if isinstance(fixt, tuple):
+        setup, teardown = fixt
+    else:
+        setup = fixt
+        teardown = lambda: None
+
+    def wrap(func):
+        """Wrap the callable."""
+        @wraps(func)
+        def call():
+            """Call `setup` and `teardown` before and after `func`."""
+            setup()
+            result = func(context)
+            teardown()
+            return result
+        return call
+
+    return wrap

File tests/decorators/test_fixture.py

 
 
 def test_call():
-    """`fix.fixture` function is callable"""
-    assert fixture() is None
+    """`fix.fixture` function does something sensible"""
+
+    def my_fixture(context):
+        """Create a fixture that assigns "bar" to `context["foo"]`."""
+        def setup():
+            """Assign "bar" to `context["foo"]`."""
+            context["foo"] = "bar"
+        return setup
+
+    @fixture(my_fixture)
+    def case(context):
+        """Return `context["foo"]`."""
+        return context["foo"]
+
+    assert case() == "bar"  # pylint: disable=E1120