djangonosedjango-testingdjango-testscollectstatic

Missing staticfiles manifest entry while rendering template in Django TestCases


I'm running into a problem with running TestCases in which I am rendering the template of a page in order to test pieces of the HTML that are produced.

Here is an example of the kind of test I am running:

test.py

from django.test import TestCase

class NavTestCase(TestCase):
    def test_standard_user_nav(self):
        self.client.login(username='username', password='password')
        user = auth.get_user(self.client)
        response = self.client.get('/')
        content = response.render().content
        # Run logic to check pieces of the nav in the rendered HTML

requirements.txt

django-material==1.0.0
django-nose==1.4.5
nose==1.3.7

settings.py

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

The issue is with code I added into my base.html file to support using django-material for a recent redesign of the site. Below, the three lines in <head> are copied directly from the django-material documentation.

base.html

{% load static %}
<html lang="en-us">
<head>
    {% include 'material/includes/material_css.html' %}
    <script src="{% static '[material/js/jquery.js' %}"></script>
    {% include 'material/includes/material_js.html' %}
</head>
<body>...</body>
</html>

For reference, here are links to those files:

However, these lines are what are breaking my tests. I am getting these two errors:

I do not have this problem with any other static files, either those created by me (whose entries I left out of the trimmed-down base.html I pasted above) or even seemingly the other ones provided by django-material. Actually, on closer inspection, when I remove these three lines, it then breaks on the subsequent calls to 'static' (which, again, I omitted from this example for simplicity).

Now, following the advice of this Stack Overflow question, when I run collectstatic locally, it does take care of this problem. However, when I deploy the code and run the tests remotely, I don't know how to get collectstatic to run there. I tried changing the base class of my tests to StaticLiveServerTestCase, but that did not make a difference. I also see from the Nose documentation that I could create a setUp() routine in my testing class, but given that I run tests like this in many test classes all over my code, I want to avoid running collectstatic multiple times.


Solution

  • Apparently this was a feature and not a bug. I had to change how I did deployments to run collectstatic before running the tests -- this required a change in the .yml config file custom to my continuous integration environment -- totally unrelated to any of the above. D'oh.