Reputation: 437
I am creating unittests for Django and using coverage and the wsgi.py file sticks out like a sore thumb with 0% coverage.
When creating unittests, should I create one for the wsgi.py file?
What would be the best way to do this using the standard Django test facility?
wsgi.py code:
import os
from django.core.wsgi import get_wsgi_application
from django.contrib.staticfiles.handlers import StaticFilesHandler
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ebdjango.settings")
application = StaticFilesHandler(get_wsgi_application())
Upvotes: 3
Views: 1136
Reputation: 585
Since the wsgi.py's responsibility is to provide an application
that handles requests, the test should verify that it does so.
While it's true that the file is generated by Django, you can still edit the file and cause the app to break, so testing it definitely could have some value (even though it's not unit test).
This is my approach for testing wsgi.py using pytest:
import pytest
from django.test import RequestFactory
from django.urls import Resolver404
from myapp.wsgi import application
def test_handles_request(rf: RequestFactory):
with pytest.raises(Resolver404):
application.resolve_request(rf.get("/"))
Since you wrap the wsgi application in StaticFilesHandler
, it would make sense to also test a request for static file (or whatever middleware you wrap the app in).
Upvotes: 3
Reputation: 921
Generally speaking you don't create tests for that file as it's created by Django itself so there's little point and it's more of a functional test than a unit test. I tend to add it to the .coveragerc file to ignore it from the reports if you are chasing that 100% file and code coverage - https://coverage.readthedocs.io/en/v4.5.x/config.html.
Upvotes: 3