2014-12-03 79 views
7

我想將我的Django(版本1.6x)應用程序切換爲使用pytest-django進行測試。 因此,我通過PIP安裝最新pytest,Django和得到這些版本:使用pytest-django自定義測試套件跑步者

pytest==2.6.4 
pytest-django==2.7.0 

通過定期Django的測試中,我使用的是自定義的測試套件亞軍延長DjangoTestSuiteRunner,我在我的settings.py配置:

settings.py:

TEST_RUNNER = "dcmanager.tests.runner.ManagedModelTestRunner" 

runner.py:

import unittest 

from django.conf import settings 
from django.db.models import get_app, get_apps 
from django.test.simple import DjangoTestSuiteRunner, build_test, build_suite, runner 


class ManagedModelTestRunner(DjangoTestSuiteRunner): 
    """ 
    Test runner that automatically makes all unmanaged models in 
    project managed for the duration of the test run and patches VStorage model, 
    so that one doesn't need to execute the SQL manually to create them. 
    """ 
    IGNORE_TESTS = ['django', 'rest_framework', 'rest_framework_swagger'] 

    def build_suite(self, test_labels, extra_tests=None, **kwargs): 
     suite = unittest.TestSuite() 

     if test_labels: 
      for label in test_labels: 
       if '.' in label: 
        suite.addTest(build_test(label)) 
       else: 
        app = get_app(label) 
        suite.addTest(build_suite(app)) 
     else: 
      ignore_list = [] 
      for app in get_apps(): 
       app_name_parts = app.__name__.split('.') 
       for index, _ in enumerate(app_name_parts): 
        app_part_name = '.'.join(app_name_parts[0:index]) 
        if app_part_name and app_part_name in self.IGNORE_TESTS: 
         ignore_list.append(app.__name__) 
         break 
       if app.__name__ not in ignore_list: 
        suite.addTest(build_suite(app)) 

     if extra_tests: 
      for test in extra_tests: 
       suite.addTest(test) 

     return runner.reorder_suite(suite, (unittest.TestCase,)) 

    def setup_test_environment(self, *args, **kwargs): 
     if settings.STAGE == 'TEST': 
      from django.db.models.loading import get_models 
      self.unmanaged_models = [m for m in get_models() 
            if not m._meta.managed] 
      for m in self.unmanaged_models: 
       m._meta.managed = True 

     super(ManagedModelTestRunner, self).setup_test_environment(*args, 
                    **kwargs) 

    def teardown_test_environment(self, *args, **kwargs): 
     super(ManagedModelTestRunner, self).teardown_test_environment(*args, 
                     **kwargs) 
     # reset unmanaged models 
     for m in self.unmanaged_models: 
      m._meta.managed = False 

我如何告訴pytest-django使用我的自定義測試套件跑步者?

回答

2

py.test不使用單元測試運行在所有

恐怕你將不得不重做自定義集合在py.test

相關問題