Forums

Testing in djangoproject tutorial

While working through the basic djangoproject tutorial, I ran into a problem at testing.

The command

python3 manage.py test polls

responded with

Creating test database for alias 'default'...
Got an error creating the test database: Access denied for user....

Any help understanding and dealing with this would be appreciated.

The Django test runner is trying to create a database, but can't. To get it to work, create a database from the databases tab that has the name that is your main Django database name with "test_" prepended.

You can also use sqlite to run your tests instead. This is faster to run tests, but there are sometimes differences between sqlite and mysql that you need to be aware of. At PythonAnywhere, we use sqlite for most tests, but there are tests where we know that they will fail unless they're run against mysql and there are a few times where we've been surprised by a test failure in our integration loop because that always uses mysql.

That's really something we need to put into the docs or an FAQ somewhere.

Sorry, I am lost here.

It is not possible to prepend my main database with test _ from the Database tab. (In my case test_teamh$)

Attempts to use a mySQL console fail with the following mssage:

ERROR 1044 (42000): Access denied for user 'teamh'@'%' to database 'teamh$default'
Console closed.

Doh! You're right. I didn't think that one through very well. You can specify the name of your test database in your DATABASES settings entry like this:

DATABASES = {
    'default': {
        # ... your current database settings
        'TEST_NAME': 'teamh$test_default'
    }
}

Just make sure that you've created the test_default database and your tests should run.

Worked perfect! Thank you for patience and help!