Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

309
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!