我遇到了类似的问题。但是,设置
STATICFILES_STORAGE='pipeline.storage.NonPackagingPipelineStorage'
在运行测试时仅部分解决了我的问题。如果您想运行 LiverServerTestCase 测试而不必在运行测试之前调用“collecstatic”,我还必须完全禁用管道:
PIPELINE_ENABLED=False
从 django 1.4 开始,修改测试设置相当容易 - 有一个方便的装饰器适用于方法或 TestCase 类:
https://docs.djangoproject.com/en/1.6/topics/testing/tools/#overriding-settings
例如
from django.test.utils import override_settings
@override_settings(STATICFILES_STORAGE='pipeline.storage.NonPackagingPipelineStorage', PIPELINE_ENABLED=False)
class BaseTestCase(LiveServerTestCase):
"""
A base test case for Selenium
"""
def setUp(self):
...
然而,正如@jrothenbuhler 在他的回答中所描述的那样,这会产生不一致的结果。无论如何,如果您正在运行集成测试,这并不理想——您应该尽可能地模拟生产以发现任何潜在问题。似乎 django 1.7 以新的测试用例“StaticLiveServerTestCase”的形式为此提供了解决方案。从文档:
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#django.contrib.staticfiles.testing.StaticLiveServerCase
这个 unittest TestCase 子类扩展
django.test.LiveServerTestCase.
就像它的父级一样,您可以使用它来编写涉及
运行被测代码并使用测试工具使用它
通过 HTTP(例如 Selenium、PhantomJS 等),因为它是
需要同时发布静态资产。
我没有对此进行测试,但听起来很有希望。现在我正在使用自定义测试运行器在他的解决方案中执行@jrothenbuhler 的操作,这不需要您运行 collectstatic。如果您真的非常希望它运行 collectstatic,您可以执行以下操作:
from django.conf import settings
from django.test.simple import DjangoTestSuiteRunner
from django.core.management import call_command
class CustomTestRunner(DjangoTestSuiteRunner):
"""
Custom test runner to get around pipeline and static file issues
"""
def setup_test_environment(self):
super(CustomTestRunner, self).setup_test_environment()
settings.STATICFILES_STORAGE = 'pipeline.storage.NonPackagingPipelineStorage'
call_command('collectstatic', interactive=False)
在settings.py中
TEST_RUNNER = 'path.to.CustomTestRunner'