Skip to content
Advertisement

Gitlab CI/CD Pipeline Runs Django Unit Tests Before Migrations

Problem

I’m trying to set up a test stage in Gitlab’s CI/CD. Locally, running the unit tests goes fine and as expected. In Gitlab’s CI/CD, though, when running the script coverage run manage.py test -v 2 && coverage report the unit tests are executing before the migrations are completed in the test database, which is unexpected, and will always fail. Migrations on the test database need to run prior to unit tests executing.

Any idea why this behavior might happen?

When running python manage.py test these are the steps that happen by default:

  1. Creation of test databases.

  2. Database migration.

  3. Run system checks.

  4. Running tests.

  5. Report on the number of tests and success / failure.

  6. Removing test databases.

In the local test run output below, you can see those exact steps, in that sequence happening. The problem is that in Gitlab’s CI/CD pipeline, step 2 and 4 are switched for some mysterious reason to me.

Things I’ve already tried

  • Removing coverage, and only executing python manage.py test Result: same error
  • Removing user test, where Gitlab says the test fails: Result: same error
  • Removing all but one unit test to attempt to isolate: Result: same error
  • Adding and activating a virtual environment inside Gitlab’s container: Result: same error
  • Trying a SQLite3: Result: migrations fail due to necessary array field necessary in the project not supported in SQLite3

Gitlab CI/CD Output

My actual output is much bigger. But here’s the relevant parts:

JavaScript

Local Test Run Output

JavaScript

.gitlab-ci.yml file

My actual file is much bigger. But here’s the relevant parts:

JavaScript

Advertisement

Answer

Add a python manage.py migrate line before coverage run manage.py test ... in the scripts section of your gitlab-ci.yml.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement