18

I am running Django unit tests against a multithreaded app. Often a thread hasn't terminated by the time the unit test finishes, so the test database cannot be deleted. When I next run the tests, I get the message:

Type 'yes' if you would like to try deleting the test database 'test_appname', or 'no' to cancel`

The create_test_db autoclobber option is the functionality I want, but how can I use that? I can't find any examples or clues. I'm working in the PyCharm IDE, which is pretty configurable. I just want to delete the test database silently every time.

I'm putting tests in Transaction TestCase classes, running setup_test_environment() then Client().post(reverse(etc..))..

Chris
  • 5,664
  • 6
  • 44
  • 55

4 Answers4

22

In Pycharm django tests, you can enable the option input and enter --noinput

see screenshot below

enter image description here

Dr Manhattan
  • 13,537
  • 6
  • 45
  • 41
  • I'm struggling to remember the details, but I tried this option (see my answer [below](http://stackoverflow.com/a/34495481/1308967)) and still got the prompt. – Chris Apr 20 '16 at 20:32
  • 2
    This worked for me as well, however you must set the `--noinput` on any unit test you're trying to run individually. Curious how to set this on all tests in the project. – Rob Mar 23 '19 at 00:38
5

If you're using PyCharm and want to run a single test using the green arrow but you keep getting this error, you can modify the default django tests configuration template, so that you don't have to keep setting the --noinput option on each.

enter image description here

Maziyar Mk
  • 1,179
  • 14
  • 17
  • Someone needs to mark this as the answer. You are my lifesaver man. You gave me so much time back! – Bropane Nov 14 '22 at 22:47
3

If you are using flush in some other way, (eg, on the pre-existing development database like here), the --noinput option supresses the user prompt, eg:

from django.core.management import call_command
call_command('flush', '--noinput')
Community
  • 1
  • 1
Chris
  • 5,664
  • 6
  • 44
  • 55
0

My answer was to create a script like this:

export PGPASSWORD=the_password
if [[ `psql  -h 127.0.0.1 -d postgres -U username -p 5432 -tAc "SELECT 1 FROM pg_database WHERE datname='test_djangoprojectname'"` == "1" ]]
then
    psql -h 127.0.0.1 -d postgres -U username -p 5432 -a -w -c "SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'test_djangoprojectname' AND pid <> pg_backend_pid();"
    psql -h 127.0.0.1 -d postgres -U username -p 5432 -a -w -c "DROP DATABASE test_djangoprojectname;"
fi
  • The -d setting is database name - it can be any database your user has access to, except the one you are deleting.
  • The default username is postgres.
  • The -p setting is the port your database is on - 5432 is the default.

Save as (for example) del_test_db.sh (Windows users see below), then

chmod +x del_test_db.sh

Then in PyCharm:

  1. Run, Edit Configurations...
  2. Unfold Defaults, click Django tests
  3. In the Before launch window click +, External tools, click +
  4. Under Program, select your file del_test_db.sh, give the command a name (eg, 'del test db') and click OK.
  5. Select your tool in the list and click OK
  6. You may need to unfold Django tests in the left and delete existing test configurations

Then the script force deletes the test database before every run.

This works on Mac OS X and Ubuntu etc. For Windows the process is the same, except instead of export use SET, save the commands as a .bat file instead of .sh, and you don't need to chmod +x, and use the following syntax for the IF statement in the batch file:

if 'command' == '1' (
    ...
)

Apologies I'm unable to check this as I don't have a Windows machine.

Thanks to this answer for the code checking whether the database exists.

Community
  • 1
  • 1
Chris
  • 5,664
  • 6
  • 44
  • 55