Optimize My testcases it Taking Lot off Time While Running

I want to optimize my test cases as they are taking a lot of time to run. After deep debugging, I found that a significant amount of time is consumed in creating the database and applying migrations to set up the test case. Each test case also takes 1 to 2 seconds to run, indicating that a considerable amount of time is spent on applying migrations. When I disable the migrations, the test cases run very fast. However, I cannot disable migrations or use keepdb in the pipeline. Could you please suggest the best solution for this? I am already using pytest fixtures.

Hello there!
It looks like you’re talking about pytest-django.
Well, the best way of optimizing the test suite speed, is to check if there are tests that don’t require the DB and not using the db fixture.
Another thing is running the test suite on multiple processes, this can speed up a bit the whole execution, but it won’t speed up the execution of a single test.

Yes, I have already implemented those optimization steps. The most time-consuming part is creating the database for the alias and applying the migrations. After that, when the test passes, it shows that the test running time is 2 seconds, indicating that a significant amount of time is spent on migrations.

I can’t disable migrations or use keepdb in the pipeline.

How much time it takes to run all the tests?

10 to 15 minutes on Applying Migrations
But On Terminal It SHow test Passed: 2 secs

Right, some more information will be helpful:

  • How many tests do you have on the test suite?
  • Do you have any “data” migrations, the ones that create data on the database?
  • How many migrations files do you have, approximately?

I have 100 test cases and 400 migration files. Yes, I create mock data in the database, but I keep it in the setup function to ensure data is created in the database only once.

And you have any of them?

Yes, I create mock data in the database, but I keep it in the setup function to ensure data is created in the database only once. and reteriving the data

So you don’t have any “data” migrations, the ones with migrations.RunPython operations, sometimes this can have expensive calculations that aren’t suitable to run on every single test.
One thing that may causing this is the high number of migrations files.
If you are still on development only, one thing you can try is squashing migrations

No I AM Not Only Development I Also On Piplines That is Why I Dont Use Disable Migrations

You might want to take a look into this post to see if you can get any insights. Apart from this i can’t help much more.