Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
django: repeat testcase with different fixtures

I have a Django TestCase, all of whose tests I'd like to run for two different sets of data (in this case, the Bike object whose colour can be red or blue).

Whether that's through loading different fixtures, or the same fixtures and manipulating the colour, I have no preference.

See below example:

class TestBike(TestCase):
    fixtures = [
        "testfiles/data/blue_bike.json",
    ]

    def setUp(self, *args, **kwargs):
        self.bike = Bike.objects.get(color="blue")

        run_expensive_commands(self.bike)

    def test_bike_1(self):
        # one of many tests

    def test_bike_2(self):
        # second of many tests

One way I considered was using the parameterized package, but then I'd have to parameterize each test, and call a prepare() function from each test. Sounds like a whole lot of redundancy.

Another is to multiple-inherit the test with different fixtures. Also a bit too verbose for my liking.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Did you consider to parameterize on class level, i.e. via class attribute?

class TestBikeBase:
    COLOR = ""  # Override in derived class.

    def setUp(self, *args, **kwargs):
        self.bike = Bike.objects.get(color=self.COLOR)

        run_expensive_commands(self.bike)

    def test_bike_1(self):
        # one of many tests

    def test_bike_2(self):
        # second of many tests


class TestRedBike(TestBikeBase, TestCase):
    COLOR = "red"


class TestBlueBike(TestBikeBase, TestCase):
    COLOR = "blue" 
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda