'Asserts that a form field behaves correctly with various inputs.
Args:
fieldclass: the class of the field to be tested.
valid: a dictionary mapping valid inputs to their expected
cleaned values.
invalid: a dictionary mapping invalid inputs to one or more
raised error messages.
field_args: the args passed to instantiate the field
field_kwargs: the kwargs passed to instantiate the field
empty_value: the expected clean output for inputs in empty_values'

def assertFieldOutput(self, fieldclass, valid, invalid, field_args=None, field_kwargs=None, empty_value=u''):

    if (field_args is None):
        field_args = []
    if (field_kwargs is None):
        field_kwargs = {}
    required = fieldclass(*field_args, **field_kwargs)
    optional = fieldclass(*field_args, **dict(field_kwargs, required=False))
    for (input, output) in valid.items():
        self.assertEqual(required.clean(input), output)
        self.assertEqual(optional.clean(input), output)
    for (input, errors) in invalid.items():
        with self.assertRaises(ValidationError) as context_manager:
            required.clean(input)
        self.assertEqual(context_manager.exception.messages, errors)
        with self.assertRaises(ValidationError) as context_manager:
            optional.clean(input)
        self.assertEqual(context_manager.exception.messages, errors)
    error_required = [force_text(required.error_messages[u'required'])]
    for e in required.empty_values:
        with self.assertRaises(ValidationError) as context_manager:
            required.clean(e)
        self.assertEqual(context_manager.exception.messages, error_required)
        self.assertEqual(optional.clean(e), empty_value)
    if issubclass(fieldclass, CharField):
        field_kwargs.update({u'min_length': 2, u'max_length': 20})
        self.assertTrue(isinstance(fieldclass(*field_args, **field_kwargs), fieldclass))
