views:

31

answers:

1

I want to generate some dynamic data and feed these data in to test cases. But I found that Django will initial the test class every time to do the test. So the data will get generated every time django test framework calls the function.

Is there anyway to use something like the singleton or static variable to solve the problem? What should be the solution?

Thanks!

+3  A: 

This is normal unittest behavior, though you would normally set up the test data in the setUp() method instead of __init__ (and destroy it in tearDown() perhaps).

If generating your dynamic test data takes to long to perform for each test case method, then I guess the best way to go is to create a singleton test data class. In this case you would have to take care that each test method leaves the test data class in exactly the same state it found it, which is not trivial if you want your test case methods to write something in it; this is the reason why unittest try to re-generate the test environment for every test case method.

One improvement might be to have the singleton test data class return a deep-copy of itself every time you request it's instance.

MihaiD
Thanks! I created a singleton before you replied and it worked. You just proved my approach!
sza