[TIP] Testing Django with py.test

Brent Hoover brent at autoshepherd.com
Tue Nov 1 12:16:15 PDT 2011


I apologize if this is an obvious question but I have searched in vain 
for it for quite some time.

I currently have over to 400 tests on my Django project that no longer 
work because I am doing a small bit of Postgresql-specific code (after a 
lot of time trying to avoid it). So I have written a test runner and 
test case that skips the the db creation/teardown and substitutes an 
already in place, empty copy of the db.

The problem is that using my own runner I am back to the cruddy test 
discovery that Django has and no reporting, coverage etc. I was using 
django-nose but would like to switch to py.test but run into the simple 
issue that I can't seem to figure out how to get py.test to run in the 
Django-specific setup (e.g. inserting the project root into the front of 
sys.path, importing settings, etc). So when I run my tests, py.test 
finds them but all imports fail as the are all relative to the project 
root. Making them absolute means nothing works when running the dev server.

Where would be the "hook" point for pytest so that it does the little 
hacks to make this work? Is this a best written as a plugin or just 
something I can somehow put in my py.test config file?

Your help would be very much appreciated.

Brent Hoover
Senior Web Application Developer

More information about the testing-in-python mailing list