0

Suppose I have 3 test functions in my testcase Class. test1 does A before all the asserts statements and test2 does AB and test3 does ABC.

A, B, C all mean some database creations. Based on the mechanism of Django TestCase, for each test function it will create a new database. However, in my case test3 depends on the data state of test2 and test2 depends on the state of test1.

I have several questions:

  1. Is there an elegant way to deal with this kind of test problem? (I know fixtures can be useful. But I want to maintain the data dynamically created in the previous test functions)

  2. I strongly believe unittests should be independent. How do you deal with the inter-relation tests in the layer of database in Django?

Fu Jian
  • 395
  • 2
  • 12

2 Answers2

0

Here you are trying to use the previous test cases in order to write your next test case. In such a situation you can define the basic functionality in the setUp() function. Then in the test cases accordingly use the setUp. The setUp function is called at the start of each test case.

Arpit Goyal
  • 2,212
  • 11
  • 31
0

Mostly when there are dependent unit test functions, you need to check the code that you are testing first. The code needs to be broken down into a units that are unit testable.

But there could be some exceptions, in those cases grouping the test functions together into one would make the most sense. Because when you test a function, you are testing the functionality of the whole function and not parts of it.

fiskrisktisk
  • 158
  • 1
  • 12