pytest 시 여러개의 디비 사용하기

pytest 시 여러개의 디비 사용하기

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

# -*- coding: utf-8 -*- import os import dotenv import pytest from django.conf import settings dotenv.load_dotenv(os.path.join(os.path.abspath(os.path.dirname(__name__)), '.env' )) @pytest.fixture(scope = 'session' ) def django_db_setup(): # settings.py의 설정을 그대로 가져온다. settings.DATABASES = { 'default' : { 'ENGINE' : 'django.db.backends.postgresql' , 'NAME' : os.getenv( "POSTGRES_DB" ), 'USER' : os.getenv( "POSTGRES_USER" ), 'PASSWORD' : os.getenv( "POSTGRES_PASSWORD" ), 'HOST' : os.getenv( "POSTGRES_HOST" ) }, 'other_db1' : { 'ENGINE' : 'django.db.backends.postgresql' , 'NAME' : os.getenv( "POSTGRES_other_db1_DB" ), 'USER' : os.getenv( "POSTGRES_other_db1_USER" ), 'PASSWORD' : os.getenv( "POSTGRES_other_db1_PASSWORD" ), 'HOST' : os.getenv( "POSTGRES_other_db1_HOST" )

}, 'other_db2' : { 'ENGINE' : 'django.db.backends.postgresql' , 'NAME' : os.getenv( "POSTGRES_other_db2_DB" ), 'USER' : os.getenv( "POSTGRES_other_db2_USER" ), 'PASSWORD' : os.getenv( "POSTGRES_other_db2_PASSWORD" ), 'HOST' : os.getenv( "POSTGRES_other_db2_HOST" )

} } @pytest.fixture(autouse = True ) def use_dummy_cache_backend(settings): settings.CACHES = { "default" : { "BACKEND" : "django.core.cache.backends.dummy.DummyCache" , } } # 공통으로 사용할 fixture 들 선언 @pytest.fixture def function_fixture(): print ( 'Fixture for each test' ) return 1 @pytest.fixture(scope = 'module' ) def module_fixture(): print ( 'Fixture for module' ) return 2 Colored by Color Scripter

from http://uiandwe.tistory.com/1342 by ccl(A) rewrite - 2021-10-08 11:26:58