3

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())

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the advice - that's great.
@MarkyMark1000 No problem. It you found my answer useful would you mind clicking the up vote to indicate it for anyone in searching in the future who finds your question.
3

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).

1 Comment

Thanks for the comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.