DEV Community

Guillermo Alfonso Morales
Guillermo Alfonso Morales

Posted on

8 2

Execute Django tests in your github actions

When you are trying github actions with your django project, and your database is for example, PostgreSQL, could be a problem running tests in the workflow (personal experience 😋).

The problem is, you don't have an environment or DB engine running inside github actions platform, neither a server where github actions could run tests, then, when is turn of "run tests" step, you'll have this error.

Running django tests with connection error

So, ¿what do we do?.

Well, an easy way to fix this problem is check if your project is running in test mode to change your DB engine to SQLite inside settings.py, in this way, github actions could run all tests.

import sys
import os
# ...
# settings.py extra configurations
# ...
if sys.argv[1] == 'test':
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
else:
DATABASES = {'default': 'psql://user:password@server:5432/database'}
view raw settings.py hosted with ❤ by GitHub

Running django tests Ok

This is my first post in dev.to, I hope could help somebody with the same problem I had.

Github actions workflow

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (3)

Collapse
 
barnettdalean profile image
Dalean Barnett

How about service containers? Building an application that depends on PostgreSQL and then executing tests based on Sqlite could pose a problem.

Collapse
 
sabderemane profile image
Sarah Abd

Haha I've faced to the same problem and I've got another solution, I write an article on that also : dev.to/s_abderemane/how-to-use-doc...

Collapse
 
gamorales profile image
Guillermo Alfonso Morales

I was thinking in docker for a second post, you won 😊

Thanks for the feedback.

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay