views:

140

answers:

1

Is there some way (using the standard Django.test.TestCase framework) to perform a global initialization of certain variables, so that it only happens once.

Putting things setUp() makes it so that the variables are initialized before each test, which kills performance when the setup involves expensive operations. I'd like to run a setup type feature once, and then have the variables initialized here be visible to all my tests.

I'd prefer not to rewrite the test runner framework.

I am thinking of something similar to a before(:all) in the Ruby/RSpec world.

-S

+1  A: 

You don't need to "re-write the whole test runner framework" but you will need to create a custom test_runner (you can just copy the existing one and modify it to include your global setup code). It's about 100 lines of code. Then set the TEST_RUNNER setting to point to your custom runner and away you go.

Lance McNearney
Right - I was trying to avoid a custom test runner - doesn't look like I may have a choice. The more I work with Django, the more I miss Rails. Python has much better scientific library support though, so I guess I can live with it.
shreddd
I think the test runner used to be like 15-30 lines of code in older Django versions so they probably didn't consider it a big deal to copy it and create a new one. It got changed into a whole Class and grew in size in later versions. You could always suggest a feature request to add in a way to run some additional setup code (maybe in setup_test_environment()) without specifying a whole new runner (an additional setting or global signal)?
Lance McNearney
Thanks again for the tips - I was looking for a nice declarative way to do this along the lines of setUp() but it looks like I may have to rig up a runner. A feature request make sense.
shreddd