Running unit tests when you can't create a new database

123 views
Skip to first unread message

Martin Omander

unread,
Dec 8, 2009, 7:53:20 PM12/8/09
to Django developers
Hi all,

How about letting Django users run unit tests without creating a new
test database?

To use Django's built-in unit test harness, you have to have database
creation privileges. Not all web hosts give users those privileges.
For example, Webfaction, an excellent commercial Django host by all
accounts (http://djangohosting.org), doesn't. This means there are
Django users out there who can't run unit tests.

See here for an example http://forum.webfaction.com/viewtopic.php?pid=13266

The third message in that thread is from me, outlining how I solved
the problem by hacking the Django testing code.

Would it be worth it to add an option in the testing code that would
allow users to run unit tests without database creation privileges?
Any thoughts on what it would look like?

Cheers,

/Martin

rebus_

unread,
Dec 8, 2009, 9:21:44 PM12/8/09
to django-d...@googlegroups.com

Martin Omander

unread,
Dec 8, 2009, 11:19:35 PM12/8/09
to Django developers

Good point about SQLite. I considered SQLite, but went with hacking
the test code in the end. My production environment uses MySql, so my
test environment should as well.

For example, I just uncovered a bug in my code that has to do with
database interactions. This bug may or may not show up when I test
against another database than MySql.

/Martin


On Dec 8, 6:21 pm, rebus_ <r.dav...@gmail.com> wrote:
> 2009/12/9 Martin Omander <martin.oman...@gmail.com>:
>
>
>
> > Hi all,
>
> > How about letting Django users run unit tests without creating a new
> > test database?
>
> > To use Django's built-in unit test harness, you have to have database
> > creation privileges. Not all web hosts give users those privileges.
> > For example, Webfaction, an excellent commercial Django host by all
> > accounts (http://djangohosting.org), doesn't. This means there are
> > Django users out there who can't run unit tests.
>
> > See here for an examplehttp://forum.webfaction.com/viewtopic.php?pid=13266

Russell Keith-Magee

unread,
Dec 9, 2009, 5:33:47 AM12/9/09
to django-d...@googlegroups.com
It already is available as an option - albeit perhaps not an obvious one.

Django allows you to define a custom test runner. Copy
django.tests.simple.run_tests() into your own code, removing the calls
to create and destroy the test database. Then set TEST_RUNNER in your
settings file to point at the new runner.

Yours
Russ Magee %-)

reg...@messir.net

unread,
Dec 9, 2009, 6:22:54 AM12/9/09
to django-d...@googlegroups.com, freakb...@gmail.com
> Django allows you to define a custom test runner. Copy
> django.tests.simple.run_tests() into your own code, removing the calls
> to create and destroy the test database. Then set TEST_RUNNER in your
> settings file to point at the new runner.
Hello!
I see many similar bugreports and wishes which was closed with this
proposal.

Unfortunately this will not work with recent django versions.
Errors like AttributeError: 'Settings' object has no attribute
'DATABASE_SUPPORTS_TRANSACTIONS'

Please help solving this issue.

Russell Keith-Magee

unread,
Dec 9, 2009, 7:49:44 AM12/9/09
to django-d...@googlegroups.com

Russell Keith-Magee

unread,
Dec 9, 2009, 7:50:29 AM12/9/09
to django-d...@googlegroups.com
On Wed, Dec 9, 2009 at 7:22 PM, <reg...@messir.net> wrote:
Ok - I oversimplified slightly. You can't just delete the call to
create the test database. You need to replicate some of the logic that
the backend that establishes the extent of transaction support on your
database of choice.

My point still stands - Django ships with an ability to control and
customize the testing process. You just need to pick the
customizations that are required by your particular deployment
scenario.

Yours,
Russ Magee %-)

Martin Omander

unread,
Dec 9, 2009, 3:16:40 PM12/9/09
to Django developers
Russ,

The method you describe sounds like the preferred way. It's cleaner to
add a new test runner module than to modify existing Django code. I
really like it.

But as you say, it's not obvious how to do this. I'd like to do a
write-up of how this is done, for the common use case where you don't
have database creation privileges.

Before I dig in and try to figure this out by myself, has anyone out
there written a test runner you could share with me?

/Martin

Sean Brant

unread,
Dec 9, 2009, 3:40:51 PM12/9/09
to django-d...@googlegroups.com
This might help you out
http://ericholscher.com/projects/django-test-utils/keep_database_runner.html.
Or at least give you some pointers.

- Sean
> --
>
> You received this message because you are subscribed to the Google Groups "Django developers" group.
> To post to this group, send email to django-d...@googlegroups.com.
> To unsubscribe from this group, send email to django-develop...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
>
>
>

Martin Omander

unread,
Dec 9, 2009, 5:14:07 PM12/9/09
to Django developers
Sean,

That's great stuff; exactly what I was looking for. Thanks for the
pointer!

/Martin

On Dec 9, 12:40 pm, Sean Brant <brant.s...@gmail.com> wrote:
> This might help you outhttp://ericholscher.com/projects/django-test-utils/keep_database_runn....
> Or at least give you some pointers.
>
> - Sean
>
Reply all
Reply to author
Forward
0 new messages