使用django-pipeline进行单元测试

Man*_*nne 11 django django-testing

我有问题单元测试使用django-pipeline的应用程序的视图?每当我在任何URL上执行client.get()时,它都会产生以下异常:

ValueError:使用0x10d544950处的<pipeline.storage.PipelineCachedStorage对象找不到文件'css/bootstrap.css'.

它是bootstrap.css的事实当然不重要,但由于这个异常,我无法执行视图渲染.

欢迎任何指导/提示!

myn*_*hno 9

我遇到了类似的问题.但是,设定

STATICFILES_STORAGE='pipeline.storage.NonPackagingPipelineStorage'
Run Code Online (Sandbox Code Playgroud)

在运行测试时,只能部分解决我的问题.如果你想运行LiverServerTestCase测试而不必在运行测试之前调用'collecstatic',我还必须完全禁用管道:

PIPELINE_ENABLED=False
Run Code Online (Sandbox Code Playgroud)

从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):
        ...
Run Code Online (Sandbox Code Playgroud)

然而,正如@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)
Run Code Online (Sandbox Code Playgroud)

在settings.py中

TEST_RUNNER = 'path.to.CustomTestRunner'
Run Code Online (Sandbox Code Playgroud)


Tra*_*sen 1

我遇到了同样的问题。STATIC_FILES_STORAGE我在测试时设法通过使用不同的方法来解决它:

STATICFILES_STORAGE = 'pipeline.storage.NonPackagingPipelineStorage'
Run Code Online (Sandbox Code Playgroud)

我有单独的用于生产和测试的设置文件,所以我只是将其放入我的测试版本中,但如果你不这样做,你可能可以将其包装在if DEBUG.

- 编辑

这需要更多的努力,因为这只能在单元测试期间出现。为了解决这个问题,我使用了http://djangosnippets.org/snippets/1011/上的代码片段并创建了一个 UITestCase 类:

class UITestCase(SettingsTestCase):
    '''
    UITestCase handles setting the Pipeline settings correctly.
    '''
    def __init__(self, *args, **kwargs):
        super(UITestCase, self).__init__(*args, **kwargs)

    def setUp(self):
        self.settings_manager.set(
            STATICFILES_STORAGE='pipeline.storage.NonPackagingPipelineStorage')
Run Code Online (Sandbox Code Playgroud)

现在,我所有需要渲染包含compressed_css标签的UI的测试都使用UITestCase而不是django.test.TestCase。