samedi 9 avril 2016

Django: optimize test that violates DRY

The models under test are

class A(models.Model):
    """ model A"""
    name = models.CharField(max_length=50, unique=True)
    slug = AutoSlugField(max_length=265, unique=True, populate_from='name')
    link = models.URLField(blank=True)

    class Meta:
        verbose_name = 'Model A'

    def __unicode__(self):
        return self.name

class B(models.Model):
    """ model B"""
    name = models.CharField(max_length=50, unique=True)
    slug = AutoSlugField(max_length=265, unique=True, populate_from='name')
    link = models.URLField(blank=True)

    class Meta:
        verbose_name = 'Model B'

    def __unicode__(self):
        return self.name

The simple tests for the given models,

class TestA(TestCase):
    """ Test the A model """

    def setUp(self):
        self.name = 'A'
        self.slug = 'a'
        self.object = A.objects.create(name=self.name)

    def test_autoslug_generaton(self):
        """ test automatically generated slug """
        assert self.object.slug == self.slug

    def test_return_correct_name(self):
        """ test the __unicode__() method """
        assert self.object.__unicode__() == self.name

class TestB(TestCase):
    """ Test the A model """

    def setUp(self):
        self.name = 'B'
        self.slug = 'b'
        self.object = B.objects.create(name=self.name)

    def test_autoslug_generaton(self):
        """ test automatically generated slug """
        assert self.object.slug == self.slug

    def test_return_correct_name(self):
        """ test the __unicode__() method """
        assert self.object.__unicode__() == self.name

Here the tests violates DRY as tests are just duplicates with changed models. how do I refactor the tests as it does not violates DRY?

DRY- Dont Repeat Yourself, a software development philosophy which aims at reducing redundancy and code repetition.

Thanks.

Aucun commentaire:

Enregistrer un commentaire