Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to optimise the fixture-loading part of Django tests?

my Django tests run really slowly, but it's not the test's fault.

At the moment, the whole process takes 14s, but only 0.1s of that is running tests. The first few seconds are creating tables & indexes, the rest is applying the project's many fixtures.

What's the best way to deal with this? I think there is a way of specifying which fixtures to load in each test, but I need most of them to do most tests...

A solution I think would work is if the tests didn't drop the tables after each run, that way there would be no need to create & populate the database each run-through of the tests. Most tests don't even write to the DB.

What's the best way to optimise the fixture-loading part of Django tests? Thanks!

(I'm using nose, but otherwise just plain Django and sqlite)

EDIT: I should have mentioned that I'm using an in-memory sqlite database. What I'm looking for - specifically - is an optimisation of the fixture-loading section of the test.

like image 350
0atman Avatar asked Feb 17 '11 13:02

0atman


People also ask

How do I load all fixtures in Django?

By default, Django only loads fixtures into the default database. Use before_scenario to load the fixtures in all of the databases you have configured if your tests rely on the fixtures being loaded in all of them.

Why are Django tests so slow?

By default, Django creates a test database for each test run, which is destroyed at the end. This is a rather slow process, especially if you want to run just a few tests! The --keepdb option will not destroy and recreate the database locally on every run.

How do I use fixtures in Django?

Providing data with fixtures The most straightforward way of creating a fixture if you've already got some data is to use the manage.py dumpdata command. Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML (with PyYAML installed) documents.


2 Answers

"but I need most of them to do most tests"...

Sorry about this, but to speed things up you're going to have to do some thinking.

"I think there is a way of specifying which fixtures to load in each test"

This is a disturbing thing to read. Have you looked at your tests recently?

Your tests do -- specifically -- list the fixtures. You need to minimize that list.

like image 61
S.Lott Avatar answered Sep 23 '22 12:09

S.Lott


You may use sqlite in-memory database for tests - it really fast

like image 41
Alexey Kuzminich Avatar answered Sep 26 '22 12:09

Alexey Kuzminich