Slow feedback loop in tests using Postgres

I have a growing codebase, and the tests are getting slower and slower. My main concern is when iterating on a small test case, the overhead of running tests is the largest part of the runtime (as opposed to running all tests in continuous integration). I am running tests with pytest-django, my database is a postgres running in a docker-compose.

My baseline was 20sec with --keedp-db. I made some improvements that were easily accessible : --no-migration gave me a 3sec improvement.

But while digging into the code, it seems that whatever options I provide, I cannot skip the call_command("migrate" ...) part. I wonder why this is the case, because commenting this part gave me a 5.5 sec improvement.

I must say my django startup time has risen up a lot, at around 5 sec.

Is there something I’m missing about running small tests?

Bonjour Martin,

It’s a bit hard to provide you adequate help with the level of details you’ve provided.

For the slow call_command("migrate", ...) I’ve previously worked around it by subclassing the default test runner to allow an additional flag to be provided to the test command to skip everything for the connection repointing to test databases.

As for why it’s slow its hard to help you further without some kind of profiling / flamegraph of what is actually happening for your particular project. The migrate command does a few things even when --keep-db is run that gradually slow down as your project gets larger

  1. It loads all of your migration files
  2. Compare the graph to the content of you test dbs django_migration table
  3. Apply un-applied migrations if there are any

Hello Simon,

I understand I was not very precise in my description, but I didn’t want anyone to do the work for me!
Thank you for the guidance you provided, it helps me understand that when growing, anyone can make choices and customise what Django provides by default to better accommodate their needs.
I’ll look for a similar way to bypass all checks in tests, but for the pytest runner.

Have a great weekend,