Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Ana Schwendler
Ana Schwendler

Posted on

     

Setting up tests for Django Plugins

Did you have a good idea for a nice Django plugin? Don’t know how to set up tests for it without creating a whole Django app for your project? Here is the solution for you. This article is written after apull request forDjango Public Admin, usingdjango-stdimage as a base.

Django Public Admin is a plugin that makes a public and read-only version of the Django Admin. It uses Poetry to manage packages and dependencies,pytest to run tests in addition topytest-django.

What is Django

Django is a web framework written in Python, and it’s designed for developers to writereusable apps (apps in this context means application, which in this case is the project designed using Django):

“Reusability is the way of life in Python. […] Django itself is also a normal Python package. This means that you can take existing Python packages or Django apps and compose them into your own web project. You only need to write the parts that make your project unique. The idea of these apps.”

In other words, we can wrap reusable parts of any Django app and use them in other Django apps we write. This is the idea and for that we also need tests. At the end of the day, we have a reusable bit of code without the accompanying Django application.

So, how to test a part of a Django app without the full Django app? 🤔

Understanding what is missing

With the environment already configured, the first issue that appeared was that when running the tests outside of a Django app was that it was complaining that our Django wasImproperlyConfigured and that we need to set up theDJANGO_SETTINGS_MODULE:

You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Enter fullscreen modeExit fullscreen mode

Setting uptests/settings.py

After talking toAna Paula Gomes, she pointed out that she already saw this being implemented in a nice way on another project, and what was being made was to create atests/settings.py file that will act as configuration for Django to run the tests. So for that, I created a file that looks like that:

importosBASE_DIR=os.path.dirname(os.path.abspath(__file__))INSTALLED_APPS=('django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.sites','public_admin','tests')SECRET_KEY='foobar'
Enter fullscreen modeExit fullscreen mode

It does contain thedefault Django configuration variables as below:

  • BASE_DIR: This is pointed to the Django project directory.

  • INSTALLED_APPS: Which contains all the apps necessary to run your set up (don’t forget to add your project herepublic_admin in our target project, andtests ), and might vary depending on what you are testing.

  • SECRET_KEY: TheSECRET_KEY setting must not be empty.

Setting upsetup.cfg

After configuring up thesettings.py we need to tell our application to use this file to run the tests, setting up theDJANGO_SETTINGS_MODULE variable in a special file calledsetup.cfg, in our case, we tell that when[tool:pytest] is called we should use this settings environment, this might look like that:

[tool:pytest]DJANGO_SETTINGS_MODULE=tests.settings
Enter fullscreen modeExit fullscreen mode

And that is it, now you can set up your tests for Django Plugins without creating an app for testing usingpytest.

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
corentinbettiol profile image
Corentin Bettiol
French dev (python3/django/djangocms).
  • Location
    South of france
  • Work
    Dev at Kapt
  • Joined

Just did this less than a month ago forDjango Check SEO!

Its so nice to simulate a Django app so the tests are fast :D

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Computer Scientist, after all. Forever @railsgirls and @djangogirls mentor.
  • Location
    Berlin, DE
  • Work
    Software Engineer
  • Joined

More fromAna Schwendler

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp