Skip to content

Instantly share code, notes, and snippets.

Created January 26, 2011 06:49
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save timbertson/796334 to your computer and use it in GitHub Desktop.
Save timbertson/796334 to your computer and use it in GitHub Desktop.
This is just a quick hack, I hope to make it a proper library soon.
import unittest
class StepCollection(object):
def __setattr__(self, attr, val):
if hasattr(self, attr):
raise RuntimeError("step %s is already declared!" % (attr,))
return super(StepCollection, self).__setattr__(attr, val)
class Object(object): pass
class World(unittest.TestCase):
def __init__(self):
def __getattr__(self, a):
return getattr(self._current, a)
def _reset(self):
self._current = Object()
class StepCollectionWrapper(object):
def __init__(self, prefix):
self._prefix = prefix
def __getattr__(self, a):
attr = getattr(steps, a)
return attr(self._prefix)
steps = StepCollection()
world = World()
Given = StepCollectionWrapper('Given')
When = StepCollectionWrapper('When')
Then = StepCollectionWrapper('Then')
And = StepCollectionWrapper('And')
class TestCase(unittest.TestCase):
def setUp(self):
global world
import functools
import sys
import termstyle
def step(func):
name = func.__name__.replace('_', ' ')
def _(prefix):
def s(*a, **kw):
desc = "\t%s %s" % (prefix, ' '.join([
termstyle.bold(' '.join(map(repr,a))),
' '.join(["%s=%r" % (k, termstyle.bold(v)) for k,v in kw.items()])])
ret = func(*a, **kw)
print >> sys.stderr,
return ret
print >> sys.stderr,
import traceback
return s
setattr(steps, func.__name__, _)
return func
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment