StopFinder has a suite of unit and integration tests that probably has just about as much code as the application itself. The first thing I do after the deployment is run the tests. This is in the nature of a smoke test, to make sure that I haven’t messed up syncing with third-party libraries and such. If the tests pass on my local machine, I don’t really expect them to fail when they’re deployed to another host.
While it’s great to run the integration tests on the same database configuration that the app itself will be running on, sometimes your host environment will make that impossible. I deploy Django sites in a lot of different places. Often times, the hosts that I’m working with aren’t sitting in my apartment under my direct control, but are rather owned by folks who make their living by maintaining app environments for people like me.
The Django unittest framework operates by creating a test database at the beginning of every run, and then wiping the tables after each test. At the end of the entire run, the database is dropped. This can cause some problems if you don’t have control of your host. For example, some shared hosting plans will create a single database for you, and disallow you from issuing CREATE DATABASE or DROP DATABASE queries yourself. (My WebFaction account operates like this, for example.)
If you’re in the hosting scenario I just described, you can’t run your tests without hand-editing your database properties, or using a different testing-specific settings file, or something else cludgy like this. This annoyed me when I first encountered it, but I was pretty swamped at the time. I budgeted about 10 minutes to resolving the problem, and came up with this very simple solution.
The idea is to wrap django-admin.py runtests so that I can select the database configuration of my choice. If I don’t have permission to create and drop databases, I’ll use SQLite as the test database. Otherwise, I’ll use the resident database server itself.
Here’s how to do that in two easy steps.
1. Write a script to drive django-admin.py runtests that sets an environment variable when you want to use SQLite (which I stupidly called a “test database” flag). The script then issues a shell call to django-admin:
import sys
_TESTING_COMMAND = 'django-admin.py test'
def run(args):
"""
Just runs the tests, with the caveat that we've reset the environment
variable to something we want. If you want to use test database settings,
specify -tdb.
"""
if len(args) == 1 and args[0] == "-tdb":
os.environ["TESTING_DJANGO"] = "THIS_VALUE_DOESN'T_MATTER"
error_code = os.system(_TESTING_COMMAND)
if error_code != 0:
print "Testing command failed: errcode %d" % error_code
if __name__ == "__main__":
run(sys.argv[1:])
2. In your local settings files, or wherever else you define your database settings, you can override the production values by doing something like:
if "TESTING_DJANGO" in os.environ:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = ':memory:'
DATABASE_USER = ''
DATABASE_PASSWORD = ''
DATABASE_HOST = ''
DATABASE_PORT = ''
TEST_DATABASE_NAME = ":memory:"
Alternately, you could have the SQLite settings be the default, and then in the local settings file you could decide to bind the database-related properties to the production database settings only if the TESTING_DJANGO flag is not set. Whatever works best for you; this is just the general idea.
Yes, this is sort of hacky, but I had it up and running in a handful of moments and it does frequently come in handy. I was hoping that by running the tests on an in-memory database it would also speed things up a bit, but I guess the table creation and data-loading time overshadows the actual time spent exercising the database in test code; running the StopFinder tests under SQLite actually takes just as long or longer than when I run it against postgres.
Hope this helps someone out there! For all I know, more recent Django builds have this sort of flexibility directly baked-in.
July 26th, 2008 at 8:02 pm
I’m trying to do some tests in different environments these days,
so it’s a quite useful tip to me,
thanks a lot.
July 29th, 2008 at 9:04 am
put your test db settings into a file called test_settings.py
in test_settings.py add this line to the top
from settings.py import *
#database settings/other test settings.
then do this
python manage.py test –settings=test_settings
September 16th, 2008 at 11:22 pm
Excellent concept! I really like norm’s approach here also. I think this is relevant, whether you have special environment considerations or not. Thanks!
December 21st, 2009 at 7:03 am
I found this article very useful! Norm’s suggestion was the icing on the cake. Tests that were running with mysql in over 9 seconds now run in just over 0.1 seconds with sqlite3. Thank you!
February 13th, 2010 at 11:44 am
This is what I do to always run tests in memory:
if ‘test’ in sys.argv:
# test database settings go here.
I have this in my local_settings.py that’s imported at the bottom of settings:
try:
from local_settings import *
except ImportError:
pass
In my build that actually tests on postgres I use the ‘–settings=test_settings_postgres’ and have a seperate settings file that overwrites the sqllite paramters.
Hope that helps,
Sam
January 30th, 2011 at 12:02 am
;,’ I am really thankful to this topic because it really gives up to date information .*,
July 4th, 2011 at 5:27 am
vacation clip art,
July 20th, 2011 at 6:59 am
Considerably, the content is really the freshest on this notable topic. We agree with your own conclusions and will thirstily anticipate the next improvements.Ill immediately grab your feed to remain abreast of any updates.
August 5th, 2011 at 12:30 am
Just examined the topic! Awesome job. goarticles.com
August 5th, 2011 at 12:30 am
Ты в комнату войдёшь — меня не будет,
Я буду в том, что комната пуста.
September 13th, 2011 at 6:57 am
since attain competence, on earth do you reactions upgrading all your internet because of furthermore content? This is very great for me and my peers.
September 19th, 2011 at 11:13 am
Awesome blog! Do you have any suggestions for aspiring writers? I’m planning to start my own site soon but I’m a little lost on everything. Would you propose starting with a free platform like Wordpress or go for a paid option? There are so many options out there that I’m completely confused .. Any recommendations? Thank you!
September 24th, 2011 at 10:46 pm
I wish to show some thanks to the writer for rescuing me from such a instance. Just after browsing throughout the search engines and finding proposals which are not helpful, I thought my entire life was well over. Existing without the presence of strategies to the issues you have fixed as a result of your good review is a serious case, as well as the ones that might have adversely affected my entire career if I hadn’t come across your web site. Your primary natural talent and kindness in controlling every part was excellent. I don’t know what I would have done if I had not discovered such a subject like this. It’s possible to at this moment look forward to my future. Thanks a lot so much for this reliable and effective guide. I won’t be reluctant to suggest your blog to anybody who would need guidance on this topic.
September 25th, 2011 at 9:46 am
grando a orrublio si lilhare siquis con trado. dinia hestór se inhangena son sipio mi ising bismo y doteiria cintorces fimog.
October 10th, 2011 at 12:30 am
I intended to put you that very little word to thank you again for those incredible thoughts you’ve documented in this article. This is so unbelievably generous with you to convey publicly precisely what a number of people could possibly have offered as an e book to help with making some cash for their own end, precisely considering the fact that you could have done it if you desired. Those advice in addition served like a fantastic way to fully grasp most people have the same keenness just as my very own to see more with regards to this condition. I’m sure there are thousands of more enjoyable sessions in the future for many who see your blog post.
October 11th, 2011 at 3:31 pm
Woah! I’m really digging the template/theme of this website. It’s simple, yet effective. A lot of times it’s very difficult to get that “perfect balance” between user friendliness and appearance. I must say you have done a fantastic job with this. Also, the blog loads super fast for me on Safari. Excellent Blog!
October 16th, 2011 at 11:29 am
I in addition to my guys were looking through the excellent suggestions found on your website and then all of the sudden got a horrible suspicion I never thanked the site owner for those techniques. Those boys appeared to be so thrilled to learn them and have now surely been taking pleasure in them. Appreciate your being simply thoughtful and then for opting for varieties of incredibly good guides most people are really eager to learn about. My honest apologies for not saying thanks to sooner.
October 23rd, 2011 at 12:21 am
HeyWas just browing the net on this boring friday night and came across your good post. Thanks for a decent read. I bookmarked you.
October 23rd, 2011 at 6:46 am
We are getting married this coming June on the water in Newport Rhode Island. We are excited for a colorful, vibrant wedding. Our colors are yellow, orange and teal. We want to incorporate color in a bold and modern way without losing any elegance. The room has an amazing panoramic view of the ocean and we want to incorporate this into the theme without being too beachy. We want to make a bold, modern statement with color without flashy lights or too many bells and whistles. It would be great to see how you can help us do this in a way that is dramatic without taking away from the beautiful location and view.
October 29th, 2011 at 9:51 am
Good day very cool web site!! Man .. Excellent .. Wonderful .. I’ll bookmark your site and take the feeds additionally…I am satisfied to seek out a lot of helpful info here in the put up, we’d like work out extra strategies in this regard, thank you for sharing.
October 30th, 2011 at 10:07 am
hallo people!! Georgous site!
November 17th, 2011 at 10:18 am
Hello, i believe that i saw you visited my blog thus i came to “return the favor”.I’m attempting to to find issues to improve my website!I guess its ok to use a few of your concepts!!
November 18th, 2011 at 1:08 pm
My spouse and i got cheerful when John could do his research through your ideas he got through your blog. It’s not at all simplistic just to find yourself handing out tricks which people today have been trying to sell. So we take into account we need the writer to give thanks to for this. Those explanations you made, the straightforward web site menu, the friendships you assist to instill - it’s many exceptional, and it’s making our son and the family reckon that that idea is fun, and that is exceptionally indispensable. Thanks for all the pieces!
November 21st, 2011 at 3:54 pm
I appreciate the time you put in this work or in this article. Although you have interesting ideas, I really cannot agree with them. I’m sure there are better ways to walk through this sticky situation. Not trying to insult or troll or anything .
November 29th, 2011 at 8:08 pm
Thanks for the sensible critique. Me & my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more from this post. I’m very glad to see such magnificent information being shared freely out there.
December 10th, 2011 at 7:39 am
Youre so cool! I dont suppose Ive learn something like this before. So good to seek out someone with some unique thoughts on this subject. realy thank you for beginning this up. this website is one thing that is wanted on the net, someone with slightly originality. useful job for bringing something new to the internet!
December 11th, 2011 at 6:03 am
Oh my goodness! an amazing article dude. Thank you Nonetheless I am experiencing subject with ur rss . Don’t know why Unable to subscribe to it. Is there anybody getting an identical rss problem? Anybody who is aware of kindly respond. Thnkx
December 17th, 2011 at 5:35 pm
I am glad to be a visitant of this complete web site, thank you for this rare information!
December 17th, 2011 at 9:38 pm
Well, I have to say this is a nice post, You got LIKED by me. If you want, you can always visit my website designed for museums around the world
January 5th, 2012 at 2:02 am
With this information I got in you here in this discussion I think I might finally Know what I am going to do.
January 5th, 2012 at 2:36 am
We’re a group of volunteers and opening a new scheme in our community. Your site provided us with valuable info to work on. You have done a formidable job and our entire community will be grateful to you.