mercredi 6 décembre 2017

How to run the same Django test for different datasets?

I want to run the same exact test in a django.test.TestCase for different datasets:

class MyTestCase(django.test.TestCase):
    fixtures = ['datasets.json']

    @classmethod
    def setUpTestData(cls):
        super(MyTestCase, cls).setUpTestData()
        self.datasets = []  # get from fixtures


    def test(self):
        for dataset in self.datasets:
            self._test_dataset(dataset)

    def _test_dataset(self, dataset):
        # peforms the actual test for the given dataset
        self.assertEquals(...)

How can I do that in a more elegant way? Does Django test framework provide a tool to do this?

The main issue is determining what dataset has failed, if the test fails. Since my example uses a for loop that would not be easy unless we used additional statements.

Let me mentioned that I found out that this can apparently be done with pytest, but I am using django.test here.

Aucun commentaire:

Enregistrer un commentaire