2011-11-19 17 views
4

據我所知,可以運行所有安裝的應用程序或單個應用程序的測試。測試所有的應用程序似乎是過度的,因爲它通常包含Django的本地模塊(django.contrib.auth等)。是否有可能創建一個測試套件,其中只包含我指定的應用程序(在我的案例中,它將共享我項目文件夾中所有應用程序的所有測試)?測試Django中的特定應用程序

回答

5

你可以寫一個自定義的測試運行,只有測試您的應用程序。在過去,我已經使用了類似

tests/runner.py

from django.test.simple import DjangoTestSuiteRunner 
from django.conf import settings 

class AppsTestSuiteRunner(DjangoTestSuiteRunner): 
    """ Override the default django 'test' command, include only 
     apps that are part of this project 
     (unless the apps are specified explicitly) 
    """ 


    def run_tests(self, test_labels, extra_tests=None, **kwargs): 
     if not test_labels: 
      PROJECT_PREFIX = 'my_project.' 
      test_labels = [app.replace(PROJECT_PREFIX, '') 
       for app in settings.INSTALLED_APPS 
       if app.startswith(PROJECT_PREFIX)] 
     return super(AppsTestSuiteRunner, self).run_tests(
       test_labels, extra_tests, **kwargs) 

你再settings.py

TEST_RUNNER = 'my_project.tests.runner.AppsTestSuiteRunner' 
+0

工程就像一個魅力,非常感謝! –

1

使用nosedjango nose

配置後它設置爲默認的測試運行,就跑

./manage.py測試

,它會只是你的應用程序的測試。

相關問題