I am trying to run my tests with access to an existing database to reuse it.
Here is the error:
ERROR drf/tests/test_auth.py - RuntimeError: Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it.
here is the pytest.ini
[pytest]
DJANGO_SETTINGS_MODULE = drf.tests.test_settings
python_files = test_*.py
django_debug_mode = true
pythonpath = .venv/bin/python
addopts = --reuse-db --create-db
here is the test_settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR + '/tests', 'dump_test.sql'),
}
}
Here is a simplified version of the test:
@pytest.mark.django_db
class AuthTestCase:
def test_two_step_authentication(self):
user = UsersFactory.create()
# Initialize the API client
client = APIClient()
# Step 1: Authenticate the user to get the initial token
url_step1 = reverse('token-auth-s1')
credentials = {
'email': user.email,
'password': 'password123'
}
response_step1 = client.post(url_step1, credentials, format='json')
# Assert the response status code
assert response_step1.status_code == 200
The directory structure:
drf
├── drf
│ ├── __init__.py
│ ├── settings.py
│ ├── tests
│ │ ├── __init__.py
│ │ ├── dump_test.sql
│ │ ├── factory_boy
│ │ │ ├── __init__.py
│ │ │ ├── factory_models.py
│ │ │ └── languages_factory.py
│ │ ├── test_auth.py
│ │ └── test_settings.py
├── manage.py
└── pytest.ini
List of things I have tried so far:
- Using @pytest.mark.django_db and @pytest.mark.django_db(True) but got the same error.
- Moving @pytest.mark.django_db before the function, but got the same error.
- Originally, the database was PostgreSQL in Docker, but for simplicity, I dumped the content into test_dump.sql.
- Split the settings.py into test_settings.py and set the SQLite database.
- Checked with os.path.exists for the path of dump_test.sql, which returns True.
If anyone has idea I take it, thank you!