lundi 3 août 2015

Django Testing: How to test similar code

Let's say i have 4 django model-fields i have customized in this manner:

# This adds an extra argument 'rs_serial' to the fields that subclass this
#
class RsBaseFieldMixin(models.Field):
    def __init__(self, *args, rs_serial=None, rs_arg2=None,.... ,**kwargs):
    self.rs_serial = rs_serial
    self.rs_arg1 = rs_arg1
    self.rs_arg2 = rs_arg2
    ...
    ...
    super().__init__(*args, **kwargs)

class RsCharField(models.CharField, RsBaseFieldMixin, metaclass=models.SubfieldBase):
    pass

class RsDecimalField(models.DecimalField, RsBaseFieldMixin, metaclass=models.SubfieldBase):
    pass

class RsIntegerField(models.IntegerField, RsBaseFieldMixin, metaclass=models.SubfieldBase):
    pass

class RsBooleanField(models.BooleanField, RsBaseFieldMixin, metaclass=models.SubfieldBase):
    pass

Q1: Do i test the 4 custom fields, or just the mixin? My guess is the 4 fields, since i want to test functionality (as in end-result: i want a decimalfield with an rs_serial-argument etc), not the implementation.

I can create seperate tests for every customized field type, and every argument (this is a simplified model, in reality i have a lot of extra arguments and a lot more field-types). But i could create a for loop:

class TestFields(TestCase):

    for ft in ['RsDecimalField','RsCharField','RsBooleanField', andsoon]:

        for arg in ['rs_serial', 'rs_arg2', 'rs_arg3' ... andsoon]:
            ...
            ... [ assertEqual(ft.arg, myexpectedvalue) ]
            ...

Q2 Is this a wrong way to test? If so, why, and how should i test them?

Aucun commentaire:

Enregistrer un commentaire