2016-12-14 9 views
0

레거시 데이터베이스 (읽기 전용 연결)에서 자체 데이터베이스로 데이터를 가져 오는 Django 프로젝트가 있고 통합 테스트를 실행할 때 레거시 연결에서 test_account을 읽으려고합니다.Django unittest with 레거시 데이터베이스 연결

(1049, "Unknown database 'test_account'") 

테스트 데이터베이스에서 읽기 위해 장고에 레거시 연결 만 남기도록 지시하는 방법이 있습니까?

답변

0

실제로 별도의 통합 테스트 프레임 워크를 만드는 방법을 살펴 보려면 djenga (pypi에서 사용 가능)에 통합 테스트를 만들 수있는 무언가를 썼습니다.

from django.test.runner import DiscoverRunner 
from django.apps import apps 
import sys 

class UnManagedModelTestRunner(DiscoverRunner): 
    """ 
    Test runner that uses a legacy database connection for the duration of the test run. 
    Many thanks to the Caktus Group: https://www.caktusgroup.com/blog/2013/10/02/skipping-test-db-creation/ 
    """ 
    def __init__(self, *args, **kwargs): 
     super(UnManagedModelTestRunner, self).__init__(*args, **kwargs) 
     self.unmanaged_models = None 
     self.test_connection = None 
     self.live_connection = None 
     self.old_names = None 

    def setup_databases(self, **kwargs): 
     # override keepdb so that we don't accidentally overwrite our existing legacy database 
     self.keepdb = True 
     # set the Test DB name to the current DB name, which makes this more of an 
     # integration test, but HEY, at least it's a start 
     DATABASES['legacy']['TEST'] = { 'NAME': DATABASES['legacy']['NAME'] } 
     result = super(UnManagedModelTestRunner, self).setup_databases(**kwargs) 

     return result 

# Set Django's test runner to the custom class defined above 
TEST_RUNNER = 'config.settings.test_settings.UnManagedModelTestRunner' 
TEST_NON_SERIALIZED_APPS = [ 'legacy_app' ] 
0
from django.test import TestCase, override_settings 

@override_settings(LOGIN_URL='/other/login/') 
class LoginTestCase(TestCase): 

    def test_login(self): 
     response = self.client.get('/sekrit/') 
     self.assertRedirects(response, '/other/login/?next=/sekrit/') 

https://docs.djangoproject.com/en/1.10/topics/testing/tools/

당신은 이론적으로 DIF 여기 재정의 설정을 사용하여 전환 할 수 있어야한다 : 여기

은 장고 단위 테스트 프레임 워크를 사용할 때 사용하는 테스트 주자

관련 문제