pytest-dev/pytest-django — pull request #182 by qris (2014-10-17 15:56:24+00:00)
Title: Fix clobbering of real database by tests

Force disconnection if a database is already connected when patching for DB reuse.

DjangoCMS opens database connections very early, to set up URL routing, before
pytest has time to patch the database name to use the test database. Thus the
connection is already open and changing the NAME has no effect, so the real
database gets clobbered instead of the test one.

This isn't easy to trigger - I've only just noticed it and I've been using
DjangoCMS together with pytest-django for several projects. I don't yet know
what causes it. But because it's so catastrophic, I think we ought to put some
protection against it in place, hence this patch.