Integrating Selenium tests into Django unit tests.

79 views
Skip to first unread message

Shawn Milochik

unread,
Aug 3, 2011, 12:40:29 PM8/3/11
to django...@googlegroups.com
I'm hoping someone can help me improve this process.

Short:

I want to run Selenium tests during Django's unit tests.

When 'manage.py test' is running, there's no dev server available
for Selenium.

If I run my Django app separately, changes to the database are
cumulative instead of discrete, like Django's own tests.

My current solution:

I have a bash script that re-loads my local database from my test
fixtures then runs my app (via gunicorn) in the background.

Then I run 'manage.py test' and my tests (including the Selenium
tests) run.

Afterwards, my bash script sends a 'kill' command to the process ID
of my gunicorn script.

This works, but:

The Selenium tests each run from the same database, so db changes
are cumulative.

I have to create the database each time, separate from the one
Django's TestCase instances are using.
This also replaces my local development database each time, which
isn't a problem because I reload it from the fixtures regularly anyway,
but is mildly inconvenient.

Alternative method:

It's possible to use subprocess in a TestCase setUp and tearDown to
launch the Django dev server each time. This fixes the fixture refresh
issue.
However, this requires a separate settings.py file for testing to
set the "default" database settings to what the real settings.py file
uses for the test database.
I don't like this solution because it makes it impossible to use an
in-memory sqlite3 database and the interaction with the command line via
subprocess gets messy.

I found django-nose-selenium via a Google search, but it requires the
Java Selenium server to be running. It also adds a lot of extra stuff to
my project: installed apps, extra settings, extra command-line commands
required, and more complicated TestCases. I'm just using the Selenium
webdriver in a unittest.TestCase and it works great without any of that.
I don't want to add all that complexity just to get fresh fixtures for
each test.

Thanks,
Shawn


Tom Christie

unread,
Aug 3, 2011, 3:56:36 PM8/3/11
to django...@googlegroups.com
I just had to do the same thing. Tried to use the django-sane-testing package, but didn't have much success there. In the end this ticket here: https://code.djangoproject.com/ticket/2879 pretty much did the trick, with a bit of updating as it didn't quite work with >= 1.2. I'll see if I can get a snippet to ya some time shortly (or drop me a mail off-list: t...@tomchristie.com)
Cheers,
T.

Shawn Milochik

unread,
Aug 3, 2011, 5:29:57 PM8/3/11
to django...@googlegroups.com
Tom,

Thanks for the reply. I read the history of that ticket and it is
exactly what I'm looking for. Too bad it's five years in the making and
still not in trunk.

So, what did you do, use one of the patches in the ticket? If so, which
one? If you have a snippet or something then please post to the list --
it may be helpful to others.

Thanks,
Shawn


Tom Christie

unread,
Aug 4, 2011, 4:51:25 AM8/4/11
to django...@googlegroups.com
This...


With changes to reflect the fact that the database settings layout has changed since 1.0, like so...

=================
         # Must do database stuff in this new thread if database in memory.
         from django.conf import settings

-        if ( settings.DATABASE_ENGINE == 'sqlite3' and
-            (not settings.TEST_DATABASE_NAME
-             or settings.TEST_DATABASE_NAME == ':memory:') ):
+        database = settings.DATABASES['default']
+
+        if ( database.get('ENGINE', None).split('.')[-1] == 'sqlite3' and
+            (not database.get('TEST_NAME', None)
+             or database.get('TEST_NAME') == ':memory:') ):
             from django.db import connection
             db_name = connection.creation.create_test_db(0)
             # Import the fixture data into the test database.
===================

If possible I'll try to find some time to:

1. Package this up into a usable form on PyPI
2. Update the ticket with a working patch, and perhaps try to push it along some.
(If you wanted to help make sure that it makes it into trunk that'd be great!)

  Tom

Almad

unread,
Aug 12, 2011, 4:33:38 AM8/12/11
to Django users


On 3 srp, 21:56, Tom Christie <christie....@gmail.com> wrote:
> I just had to do the same thing. Tried to use the django-sane-testing package, but didn't have much success there.

Might I ask what was the problem?

> Cheers,
>   T.

Thanks,

Almad
Reply all
Reply to author
Forward
0 new messages