Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing apps.py in django

How can I write a test to cover my apps.py files for each model in a django application? I need 100% code coverage and cannot figure out how to test these files. Example of one of my apps.py files:

from django.apps import AppConfig

class ReportsConfig(AppConfig):
    name = 'reports'
like image 499
Molly Davey Avatar asked Apr 11 '17 00:04

Molly Davey


People also ask

What is test py in Django?

Django uses the unittest module's built-in test discovery, which will discover tests under the current working directory in any file named with the pattern test*.py. Provided you name the files appropriately, you can use any structure you like.

How do I run a specific app in Django?

It is possible to run a single app's tests standalone, without creating a Django test project for that purpose. One way of doing so is by creating a runtests.py in your app's root dir which setups Django settings and runs ./manage.py test your_app programmatically.

How do I test a Django project?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.


1 Answers

you could do it like this:

from django.apps import apps
from django.test import TestCase
from reports.apps import ReportsConfig


class ReportsConfigTest(TestCase):
    def test_apps(self):
        self.assertEqual(ReportsConfig.name, 'reports')
        self.assertEqual(apps.get_app_config('reports').name, 'reports')
like image 118
udo Avatar answered Oct 23 '22 19:10

udo