2016-12-14 53 views
0

我有一個從傳統數據庫(只讀連接)中提取數據到自己的數據庫中的Django項目,當我運行集成測試,它試圖從test_account遺留連接讀取。Django的單元測試與傳統的數據庫連接

(1049, "Unknown database 'test_account'") 

有沒有辦法告訴Django離開遺留連接單獨從測試數據庫讀取?

回答

0

如果您想查看如何創建單獨的集成測試框架,我實際上已經編寫了一些內容,可讓您在djenga(pypi上提供)中創建集成測試。

下面是測試運行我使用Django的單元測試框架時使用:

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