2014-10-07

如何解決升級 django 1.6/1.7 後 test runner 造成的問題?

如果你升級到 django 1.6.1 以上, 你可能會遇到以下錯誤訊息
這是因為 在 v1.6 以上時 test runner 的方式已經改了, 詳看這裡


$ python manage.py check
System check identified some issues:

WARNINGS:
?: (1_6.W001) Some project unittests may not execute as expected.
HINT: Django 1.6 introduced a new default test runner. It looks like this project was generated using Django 1.5 or earlier. You should ensure your tests are all running & behaving as expected. See https://docs.djangoproject.com/en/dev/releases/1.6/#new-test-runner for more information.

System check identified 1 issue (0 silenced).



那麼這問題要怎麼解決呢?
解法有二,一是逃避式,另一是一個個去修改你所有module使其符合新版格式

1.逃避式:
逃避式就是眼不見為淨,如這裡所示只要在設定裡將TEST_RUNNER設成舊的 run tester 即可.
到你的 settings, 多加入以下這行
TEST_RUNNER = 'django.test.runner.DiscoverRunner'

2.一個個的修改:
首先我們要知道這個訊息到底是從哪裡來的, 廢話不說, 請打開 [django]/django/core/checks/compatibility/django_1_6_0.py 你會看到以下內容:
請看重點紅色部分, 這裡你就可以發現原來它是這樣檢查的


    # If TEST_RUNNER is explicitly set, it's all a moot point - if it's been explicitly set,
    # the user has opted into a specific set of behaviors, which won't change as the
    # default changes.
    if not settings.is_overridden('TEST_RUNNER'):
        # Strong markers:
        # SITE_ID = 1 is in 1.5 template, not defined in 1.6 template
        try:
            settings.SITE_ID
            weight += 2
        except AttributeError:
            pass

        # BASE_DIR is not defined in 1.5 template, set in 1.6 template
        try:
            settings.BASE_DIR
        except AttributeError:
            weight += 2

        # TEMPLATE_LOADERS defined in 1.5 template, not defined in 1.6 template
        if settings.is_overridden('TEMPLATE_LOADERS'):
            weight += 2

        # MANAGERS defined in 1.5 template, not defined in 1.6 template
        if settings.is_overridden('MANAGERS'):
            weight += 2

        # Weaker markers - These are more likely to have been added in common usage
        # ADMINS defined in 1.5 template, not defined in 1.6 template
        if settings.is_overridden('ADMINS'):
            weight += 1

        # Clickjacking enabled by default in 1.6
        if 'django.middleware.clickjacking.XFrameOptionsMiddleware' not in set(settings.MIDDLEWARE_CLASSES):
            weight += 1

    if weight >= 6:
        return [
            Warning(
                "Some project unittests may not execute as expected.",
                hint=("Django 1.6 introduced a new default test runner. It looks like "
                      "this project was generated using Django 1.5 or earlier. You should "
                      "ensure your tests are all running & behaving as expected. See "
                      "https://docs.djangoproject.com/en/dev/releases/1.6/#new-test-runner "
                      "for more information."),
                obj=None,
                id='1_6.W001',
            )
        ]
    else:
        return []




從以上我們可以知道, 原來第一種逃避式可行是因為有這行的關係, 有設 TEST_RUNNER 則跳過版本檢查
if not settings.is_overridden('TEST_RUNNER'):


這裡你會看到, 原來它要檢查這麼多東西, 就請您一一個 check 並且修改吧。不同的項目有不同的權重,只要權重和修到小於 6 就會過了它的檢查。
1. 不能有 SITE_ID
2. 要有 BASE_DIR
3. settings 裡不能有 TEMPLATE_LOADERS, MANAGERS
4. 不能有 ADMINS
5. 要有 django.middleware.clickjacking.XFrameOptionsMiddleware 

改完後再 check 看看, OK, 大功告成
$ python manage.py check
System check identified no issues (0 silenced).

[附註]
manage check 的錯誤代號參考 http://django.readthedocs.org/en/latest/ref/checks.html


沒有留言:

張貼留言