Adding a DATABASES["default"]["TEST"]["NAME"] test database creation during pytest testing? #4725
-
Hello All, I have an existing non cookiecutter Django project where in settings.py: definition of default database I was able to create a separate test database that was created and used every time pytest was being run and when completed the test database was destroyed after. It was clearly stated in the console output every time pytest was run. # non cookiecutter project
#settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': "dbname",
'TEST': {
'NAME': "testdbname",
},
'USER': "user",
'PASSWORD': "password",
'HOST': "host",
'PORT': "port",
}
} I have started a new cookiecutter project where in config/settings/base.py the default database is defined as: # cookiecutter project
#config/settings/base.py
DATABASES = {"default": env.db("DATABASE_URL")} Now when I run pytest tests the tests are running against the DATABASES["default"] database. There is no test database being created/used/destroyed output in the console. I am trying to achieve the same functionality I had in non cookiecutter django project where every time pytest is run pytest is creating a test database running tests on it and then destroying the testdb after. I have tried adding the following line to: # cookiecutter project
#config/settings/base.py
DATABASES["default"]["TEST"]["NAME"]="testdbname" But I keep getting a KEY_ERROR "TEST". QUESTION :Does anyone know how to create the equivalent functionality in cookiecutter project? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Like this? DATABASES["default"]["TEST"] = {"NAME": "testdbname"} Also, you should probably set this in |
Beta Was this translation helpful? Give feedback.
-
@browniebroke Thanks for your response. I appreciate your help. If you don't mind helping further-can you help me with further understanding? Yes that way of defining the Test Database New Name solved the KEY_ERROR "TEST" error that was being thrown also thank you for pointing out the extra advice that I should move it to config/settings/test.py I put it there and the newly named "testdbname" was created locally. Although during this process I realized that in previous runs pytest had already created the default test database locally named "test_dbname" which is default test database name for postgres database according to documentation. I didn't bother using new "testdbname" database once I realized one was already created I just went back to using the original "test_dbname" database that was previously created in initial pytest run. Upon further investigation I realized that some of my unit tests were missing the pytest marker: @pytest.mark.django_db So I believe that when pytest was running running all tests:
Please correct me if I'm wrong. ONLY Once the mark was added to unit tests: they started to use the "test_dbname" database. @pytest.mark.django_db Running in PyCharm test runner: the following output would be displayed The last line clearly stating that existing "test_dbname" was being used to run the tests on. Operations to perform:
Synchronize unmigrated apps:
Apply all migrations:
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Running migrations:
No migrations to apply.
Using existing test database for alias 'default' ('test_dbname')... Although when running: 'pytest' command in terminal running app both locally and in docker-compose % pytest I get no clear indication that tests are being run on the test database "test_dbname". The following line is missing from pytest output: 'Using existing test database for alias 'default' ('test_dbname')...' Previously in non cookiecutter project when I would run 'pytest' command in terminal it would always clearly state a similar output of creating/using test database like the above line BUT NOW in cookiecutter project that line IS MISSING from the terminal output but if the tests are run in PyCharm Run/Debug test runner the line IS INCLUDED in console output. Question:How can I be sure that when running 'pytest' command it is running tests against "test_dbname" database and not default database? Am I missing a pytest.ini file configuration setting somewhere? |
Beta Was this translation helpful? Give feedback.
-
I'm no pytest expert but AFAIK, the I'm not sure why your tests are using the default DB, I never seen that before... That's more likely an issue with pytest-django than cookiecutter-django. You may want to bring this topic up to their repo, or to a discussion forum with more folks, like pytest/Django forum/discords or Stackoverflow. |
Beta Was this translation helpful? Give feedback.
-
For some reason when Querying database using DjangoORM: (i.e.) Model.objects.all() Database access not allowed, use the "django_db" mark, or the "db" or "transactional_db" fixtures to enable it. Pytest is not able to find or connect to my test database (it's not properly configured) and the tests can not be collected before even running. Although if I manually connect to test database using psycopg in code that is being tested using: conn = psycopg.connect(os.environ.get("TEST_DATABASE_URL")) All tests pass test database is found connected and properly used. Say I have a .env file in project root directory with the following two Database Urls: .env # both of these connection strings work when connecting with psycopg and running tests.
# local dbs (default/test) configured without password
DATABASE_URL=postgres://user:@host:port/dbname
TEST_DATABASE_URL=postgres://user:@host:port/test_dbname Now in cookiecutter project: config/settings/base.py DATABASES = {"default": env.db("DATABASE_URL")} And in cookiecutter project: config/settings/test.py DATABASES["default"]["TEST"] = {"NAME": "test_dbname"} I want to configure the test database to be found using similar functionality as in base.py using DATABASE_URL I want it to look something like this in config/settings/test.py: # did not work
DATABASES["default"]["TEST"] = env.db("TEST_DATABASE_URL") So that the pytest can properly find the test database when running locally. I have also tried this in config/settings/test.py: # did not work
# matches connection string parameters from earlier
DATABASES["default"]["TEST"] = {
'NAME': 'test_dbname',
'USER': 'postgres',
'HOST': '127.0.0.1',
'PORT': '5432',
} Questions:
1. Does anyone know how to properly configure a test database locally like this using a TEST_DATABASE_URL?
2. Or what could be causing pytest-django to not be able to find test database when using DjangoORM?
3. Do I need to explicitly connect to test database when using DjangoORM?
(I thought it was supposed to connect to default database configured in base.py unless a test database was configured properly at which point when running pytest the default connection would be to the test database?)
Summary:
DjangoORM not finding test database when running tests in pytest-django without explicitly connecting to test database with psycopg conn string.
Psycopg connection string TEST_DATABASE_URL can be explictly connected to first using:
conn = psycopg.connect(os.environ.get("TEST_DATABASE_URL"))
and then successfully queried using RawSQL: (only after explicitly connecting with psycopg conn string)
I don't want to use RawSQL I want to use the DjangoORM am I missing something here? All of my tests are properly marked with the pytest fixture: @pytest.mark.django_db(transaction=True) The test database is running locally next to default database in Postgres app Any advice helps thanks. |
Beta Was this translation helpful? Give feedback.
To anyone that might have a similar issue:
The problem was occurring when I was unit testing functions that were hitting the database.
The pytest collection er…