在 Django TestCases 中呈现模板时缺少静态文件清单条目

Missing staticfiles manifest entry while rendering template in Django TestCases

我 运行 遇到 运行 宁 TestCase 的问题,我正在呈现页面模板以测试 HTML 产生。

这是我正在 运行ning:

进行的那种测试的示例

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'

问题出在我添加到 base.html 文件中的代码,以支持使用 django-material 进行最近的网站重新设计。下面,<head> 中的三行是直接从 django-material 文档中复制的。

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>

作为参考,以下是这些文件的链接:

但是,这些行破坏了我的测试。我收到这两个错误:

我对任何其他静态文件都没有这个问题,无论是我创建的那些(我在上面粘贴的修剪过的 base.html 中遗漏的条目)甚至看似django-material. 提供的其他内容 实际上,经过仔细检查,当我删除这三行时,它会在随后对 'static' 的调用中中断(我再次从这个例子是为了简单起见)。

现在,按照 , 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 的建议,我可以在我的测试 class 中创建一个 setUp() 例程,但是考虑到我 运行 在很多地方都这样测试在我的代码中测试 classes,我想避免 运行ning collectstatic 多次。

有什么想法吗?

谢谢!

显然这是一项功能而非错误。在 运行 进行测试之前,我必须将部署方式更改为 运行 collectstatic——这需要更改 .yml 配置文件自定义以适应我的持续集成环境——完全不相关以上任何一项。哦。