""subject" Decorator for Django Tests"

The Python elite will probably turn their noses up at me, but I just conjured an even better looking way to write my tests.

Using my utilities.py file I can write the following decorator:

def subject(item):
  def wrapper(f, _item=item):
    def test_func(*args, **kwargs):
      item = _item
      if models.Model in item.__class__.__bases__:
        item = get_model(item)
      f.__globals__['it'] = item
      res = f(*args, **kwargs)
      return res
    return test_func
  return wrapper

which allows me to write tests that look like this:

@subject(BlogFactory.build(title=''))
def test_with_blank_title(self):
  it.should_not_be_valid
Go Top
comments powered by Disqus