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

595
Views
What are the differences between setUpClass, setUpTestData and setUp in TestCase class?

More specifically, what are the use cases for each?

What I've understood so far:

setUpClass

  • This method runs once, before all the tests in a test class

setUpTestData

  • This method runs once if the DB has transaction support. Otherwise it runs before each test.

setUp

  • This method runs before each test in a test class.

From the understanding I mentioned above, it seems setUpTestData lies in the area between setUpClass and setUp. Why do we need a class level method for setuUpTestData while the same effect could be achieved by either setUpClass or setUp or a combination of the both?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

EDIT: Update/Correction after Alasdair's comment

setUpClass

  • setUpClass is used to perform class-wide initialization/configuration (e.g. creating connections, loading webdrivers). When using setUpClass for instance to open database connection/session you can use tearDownClass to close them.
  • setUpClass is called once for the TestCase before running any of the tests. Similarly tearDownClass is called after all the tests have run.

Note from documentation:

SimpleTestCase and its subclasses (e.g. TestCase, ...) rely on setUpClass() and tearDownClass() to perform some class-wide initialization (e.g. overriding settings). If you need to override those methods, don’t forget to call the super implementation:

setUpTestData

  • setUpTestData is used to create initial test data per TestCase. This method is called by TestCase.setUpClass() (src)
  • setUpTestData is called once for TestCase, as explained in documentation. In case databases does not support transactions, setUpTestData will be called before each test run (thanks @Alasdair for correcting me)

setUp

  • setUp will be called before each test run, and should be used to prepare test dataset for each test run.

Using setUpTestData allows for test performance improvement, be aware that change to this data in tests will persist between different test runs. If needs to be reloaded it can be done so from setUp method. If database used for running tests does not support transactions, performance improvement is negated (as setUpTestData will be called before each test run)

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!