views:

99

answers:

7

How do I run a unit test against the production database instead of the test database?

I have a bug that's seems to occur on my production server but not on my development computer.

I don't care if the database gets trashed.

+3  A: 

Is it feasible to make a copy the database, or part of the database that causes the problem? If you keep a backup server, you might be able to copy the data from there instead (make sure you have another backup, in case you messed the backup database).

Basically, you don't want to mess with live data and you don't want to be left with no backup in case you mess something up (and you will!).

Lie Ryan
A: 

Make a copy the database... It's really a good practices!!

Just execute the test, instead call commit, call rollback at the end of.

Marco Catunda
A: 

The first thing to try should be manually executing the test code on the shell, on the production server.

python manage.py shell

If that doesn't work, you may need to dump the production data, copy it locally and use it as a fixture for the testcase you are using.

If there is a way to ask django to use the standard database without creating a new one, I think rather than creating a fixture, you can do a sqldump which will generally be a much smaller file.

Lakshman Prasad
A: 

Short answer: you don't.

Long answer: you don't, you make a copy of the production database and run it there

Mel Gerats
A: 

If you really don't care about trashing the db, then Marco's answer of rolling back the transaction is my preferred choice as well. You could also try NdbUnit but I personally don't think the extra baggage it brings is worth the gains.

How do you test the test db now? By test db do you mean SQLite?

HTH,
Berryl

Berryl
A: 

Use manage.py dumpdata > mydata.json to get a copy of the data from your database.

Go to your local machine, copy mydata.json to a subdirectory of your app called fixtures e.g. myapp/fixtures/mydata.json and do:

manage.py syncdb # Set up an empty database
manage.py loaddata mydata.json

Your local database will be populated with data and you can test away.

blokeley
A: 

You don't. Unit tests don't talk to databases.

Andrei Tanasescu