[Django]-Managing Django test isolation for installable apps

3👍

A nice approach to isolation I’ve seen used by Jezdez is to have a submodule called my_app.tests which contains all the test code (example). This means that those tests are NOT run by default when someone installs your app, so they don’t get random phantom test failures, but if they want to check that they haven’t inadvertently broken something then it’s as simple as adding myapp.tests to INSTALLED_APPS to get it to run.

Within the tests, you can do your best to ensure that the correct environment exists using override_settings (if this isn’t in 1.4 then there’s not that much code to it). Personally my feeling is that with integration type tests perhaps it doesn’t matter if they fail. If you like, you can include a clean settings file (compressor.test_settings), which for a major project may be more appropriate.

An alternative is that you separate your tests out a bit – there are two separate bodies of tests for contrib.admin, those at django.contrib.admin.tests, and those at tests.regression_tests.contrib.admin (or some path like that). The ones to check public apis and core functionality (should) reside in the first, and anything likely to get broken by someone else’s (reasonable) configuration resides in the second.

IMHO, the whole running external apps tests is totally broken. It certainly shouldn’t happen by default (and there are discussions to that effect) and it shouldn’t even be a thing – if someone’s external app test suite is broken by my monkey patching (or whatever) I don’t actually care – and I definitely don’t want it to break the build of my site. That said, the above approaches allow those who disagree to run them fairly easily. Jezdez probably has as many major pluggable apps as anyone else, and even if there are some subtle issues with his approach at least there is consistency of behaviour.

2👍

Since you’re releasing a reusable third-party application, I don’t see any reason the developer using the application should be changing the code. If the code isn’t changing, the developers shouldn’t need to run your tests.

The best solution, IMO, is to have the tests sit outside of the installable package. When you install Django and run manage.py tests, you don’t run the Django test suite, because you trust the version of Django you’ve installed is stable. This should be the same for developers using your third-party application.

If there are specific settings you want to ensure work your library, just write test cases that use those settings values.

Here’s an example reusable django application that has the tests sit outside of the installed package:

https://github.com/Yipit/django-roughage/tree/master

It’s a popular way to develop python modules as seen:

https://github.com/kennethreitz/requests

https://github.com/getsentry/sentry

👤Zach

Leave a comment