Skip to content

Instantly share code, notes, and snippets.

@justinabrahms
Created April 28, 2010 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinabrahms/382306 to your computer and use it in GitHub Desktop.
Save justinabrahms/382306 to your computer and use it in GitHub Desktop.
class TestActiveField(object):
models_to_test = ['Model1', 'Model2', 'foo']
def test_valid_choice(self):
for model in self.models_to_test:
yield self.check_valid_choice_for_active_field, model
def check_valid_choice_for_active_field(self, model):
# implement test logic here
assert 'foo' != model, "Model was %s" % model
#
# results in:
#
# views.base.TestActiveField.test_valid_choice('Model1',) ... ok
# views.base.TestActiveField.test_valid_choice('Model2',) ... ok
# views.base.TestActiveField.test_valid_choice('foo',) ... FAIL
#
# ======================================================================
# FAIL: views.base.TestActiveField.test_valid_choice('foo',)
# ----------------------------------------------------------------------
# Traceback (most recent call last):
# File "/home/jlilly/.virtualenvs/dashboard/lib/python2.5/site-packages/nose-0.11.2-py2.5.egg/nose/case.py", line 183, in runTest
# self.test(*self.arg)
# File "/home/jlilly/src/dashboard/tests/views/base.py", line 13, in check_valid_choice_for_active_field
# assert 'foo' != model, "Model was %s" % model
# AssertionError: Model was foo
#
# ----------------------------------------------------------------------
# Ran 3 tests in 0.002 seconds
# FAILED (failures=1)
@varikin
Copy link

varikin commented Apr 28, 2010

Why is that better than:

def test_valid_choice(self):
    for model in self.models_to_test:
        self.check_valid_choice_for_active_field(model)

@bastih
Copy link

bastih commented Apr 28, 2010

You get every single case as a failure or success.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment