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'
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.
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.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With