Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global set up in django test framework?

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

like image 272
shreddd Avatar asked Mar 03 '10 19:03

shreddd


2 Answers

This is partially addressed in newer versions of python/django by setUpClass() which will at least allow me to run class level setup.

like image 183
shreddd Avatar answered Nov 02 '22 15:11

shreddd


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.

  • http://docs.djangoproject.com/en/dev/topics/testing/#using-different-testing-frameworks
like image 26
Lance McNearney Avatar answered Nov 02 '22 17:11

Lance McNearney