README.rst 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. ===========
  2. django-nose
  3. ===========
  4. .. image:: https://travis-ci.org/django-nose/django-nose.png
  5. :target: https://travis-ci.org/django-nose/django-nose
  6. Features
  7. --------
  8. * All the goodness of `nose`_ in your Django tests, like...
  9. * Testing just your apps by default, not all the standard ones that happen to
  10. be in ``INSTALLED_APPS``
  11. * Running the tests in one or more specific modules (or apps, or classes, or
  12. folders, or just running a specific test)
  13. * Obviating the need to import all your tests into ``tests/__init__.py``.
  14. This not only saves busy-work but also eliminates the possibility of
  15. accidentally shadowing test classes.
  16. * Taking advantage of all the useful `nose plugins`_
  17. * Fixture bundling, an optional feature which speeds up your fixture-based
  18. tests by a factor of 4
  19. * Reuse of previously created test DBs, cutting 10 seconds off startup time
  20. * Hygienic TransactionTestCases, which can save you a DB flush per test
  21. * Support for various databases. Tested with MySQL, PostgreSQL, and SQLite.
  22. Others should work as well.
  23. .. _nose: http://somethingaboutorange.com/mrl/projects/nose/
  24. .. _nose plugins: http://nose-plugins.jottit.com/
  25. Installation
  26. ------------
  27. You can get django-nose from PyPI with... ::
  28. pip install django-nose
  29. The development version can be installed with... ::
  30. pip install -e git://github.com/django-nose/django-nose.git#egg=django-nose
  31. Since django-nose extends Django's built-in test command, you should add it to
  32. your ``INSTALLED_APPS`` in ``settings.py``::
  33. INSTALLED_APPS = (
  34. ...
  35. 'django_nose',
  36. ...
  37. )
  38. Then set ``TEST_RUNNER`` in ``settings.py``::
  39. TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'
  40. Use
  41. ---
  42. The day-to-day use of django-nose is mostly transparent; just run ``./manage.py
  43. test`` as usual.
  44. See ``./manage.py help test`` for all the options nose provides, and look to
  45. the `nose docs`_ for more help with nose.
  46. .. _nose docs: http://somethingaboutorange.com/mrl/projects/nose/
  47. Enabling Database Reuse
  48. -----------------------
  49. You can save several seconds at the beginning and end of your test suite by
  50. reusing the test database from the last run. To do this, set the environment
  51. variable ``REUSE_DB`` to 1::
  52. REUSE_DB=1 ./manage.py test
  53. The one new wrinkle is that, whenever your DB schema changes, you should leave
  54. the flag off the next time you run tests. This will cue the test runner to
  55. reinitialize the test database.
  56. Also, REUSE_DB is not compatible with TransactionTestCases that leave junk in
  57. the DB, so be sure to make your TransactionTestCases hygienic (see below) if
  58. you want to use it.
  59. Enabling Fast Fixtures
  60. ----------------------
  61. django-nose includes a fixture bundler which drastically speeds up your tests
  62. by eliminating redundant setup of Django test fixtures. To use it...
  63. 1. Subclass ``django_nose.FastFixtureTestCase`` instead of
  64. ``django.test.TestCase``. (I like to import it ``as TestCase`` in my
  65. project's ``tests/__init__.py`` and then import it from there into my actual
  66. tests. Then it's easy to sub the base class in and out.) This alone will
  67. cause fixtures to load once per class rather than once per test.
  68. 2. Activate fixture bundling by passing the ``--with-fixture-bundling`` option
  69. to ``./manage.py test``. This loads each unique set of fixtures only once,
  70. even across class, module, and app boundaries.
  71. How Fixture Bundling Works
  72. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  73. The fixture bundler reorders your test classes so that ones with identical sets
  74. of fixtures run adjacently. It then advises the first of each series to load
  75. the fixtures once for all of them (and the remaining ones not to bother). It
  76. also advises the last to tear them down. Depending on the size and repetition
  77. of your fixtures, you can expect a 25% to 50% speed increase.
  78. Incidentally, the author prefers to avoid Django fixtures, as they encourage
  79. irrelevant coupling between tests and make tests harder to comprehend and
  80. modify. For future tests, it is better to use the "model maker" pattern,
  81. creating DB objects programmatically. This way, tests avoid setup they don't
  82. need, and there is a clearer tie between a test and the exact state it
  83. requires. The fixture bundler is intended to make existing tests, which have
  84. already committed to fixtures, more tolerable.
  85. Troubleshooting
  86. ~~~~~~~~~~~~~~~
  87. If using ``--with-fixture-bundling`` causes test failures, it likely indicates
  88. an order dependency between some of your tests. Here are the most frequent
  89. sources of state leakage we have encountered:
  90. * Locale activation, which is maintained in a threadlocal variable. Be sure to
  91. reset your locale selection between tests.
  92. * memcached contents. Be sure to flush between tests. Many test superclasses do
  93. this automatically.
  94. It's also possible that you have ``post_save`` signal handlers which create
  95. additional database rows while loading the fixtures. ``FastFixtureTestCase``
  96. isn't yet smart enough to notice this and clean up after it, so you'll have to
  97. go back to plain old ``TestCase`` for now.
  98. Exempting A Class From Bundling
  99. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  100. In some unusual cases, it is desirable to exempt a test class from fixture
  101. bundling, forcing it to set up and tear down its fixtures at the class
  102. boundaries. For example, we might have a ``TestCase`` subclass which sets up
  103. some state outside the DB in ``setUpClass`` and tears it down in
  104. ``tearDownClass``, and it might not be possible to adapt those routines to heed
  105. the advice of the fixture bundler. In such a case, simply set the
  106. ``exempt_from_fixture_bundling`` attribute of the test class to ``True``.
  107. Speedy Hygienic TransactionTestCases
  108. ------------------------------------
  109. Unlike the stock Django test runner, django-nose lets you write custom
  110. TransactionTestCase subclasses which expect to start with an unmarred DB,
  111. saving an entire DB flush per test.
  112. Background
  113. ~~~~~~~~~~
  114. The default Django TransactionTestCase class `can leave the DB in an unclean
  115. state`_ when it's done. To compensate, TransactionTestCase does a
  116. time-consuming flush of the DB *before* each test to ensure it begins with a
  117. clean slate. Django's stock test runner then runs TransactionTestCases last so
  118. they don't wreck the environment for better-behaved tests. django-nose
  119. replicates this behavior.
  120. Escaping the Grime
  121. ~~~~~~~~~~~~~~~~~~
  122. Some people, however, have made subclasses of TransactionTestCase that clean up
  123. after themselves (and can do so efficiently, since they know what they've
  124. changed). Like TestCase, these may assume they start with a clean DB. However,
  125. any TransactionTestCases that run before them and leave a mess could cause them
  126. to fail spuriously.
  127. django-nose offers to fix this. If you include a special attribute on your
  128. well-behaved TransactionTestCase... ::
  129. class MyNiceTestCase(TransactionTestCase):
  130. cleans_up_after_itself = True
  131. ...django-nose will run it before any of those nasty, trash-spewing test cases.
  132. You can thus enjoy a big speed boost any time you make a TransactionTestCase
  133. clean up after itself: skipping a whole DB flush before every test. With a
  134. large schema, this can save minutes of IO.
  135. django-nose's own FastFixtureTestCase uses this feature, even though it
  136. ultimately acts more like a TestCase than a TransactionTestCase.
  137. .. _can leave the DB in an unclean state: https://docs.djangoproject.com/en/1.4/topics/testing/#django.test.TransactionTestCase
  138. Test-Only Models
  139. ----------------
  140. If you have a model that is used only by tests (for example, to test an
  141. abstract model base class), you can put it in any file that's imported in the
  142. course of loading tests. For example, if the tests that need it are in
  143. ``test_models.py``, you can put the model in there, too. django-nose will make
  144. sure its DB table gets created.
  145. Assertions
  146. ----------
  147. ``django-nose.tools`` provides pep8 versions of Django's TestCase asserts
  148. and some of its own as functions. ::
  149. assert_redirects(response, expected_url, status_code=302, target_status_code=200, host=None, msg_prefix='')
  150. assert_contains(response, text, count=None, status_code=200, msg_prefix='')
  151. assert_not_contains(response, text, count=None, status_code=200, msg_prefix='')
  152. assert_form_error(response, form, field, errors, msg_prefix='')
  153. assert_template_used(response, template_name, msg_prefix='')
  154. assert_template_not_used(response, template_name, msg_prefix='')
  155. assert_queryset_equal(qs, values, transform=repr)
  156. assert_num_queries(num, func=None, *args, **kwargs)
  157. assert_code(response, status_code, msg_prefix='')
  158. assert_ok(response, msg_prefix='')
  159. assert_mail_count(count, msg=None)
  160. Using With South
  161. ----------------
  162. `South`_ installs its own test command that turns off migrations during
  163. testing. Make sure that django-nose comes *after* ``south`` in
  164. ``INSTALLED_APPS`` so that django_nose's test command is used.
  165. .. _South: http://south.aeracode.org/
  166. Always Passing The Same Options
  167. -------------------------------
  168. To always set the same command line options you can use a `nose.cfg or
  169. setup.cfg`_ (as usual) or you can specify them in settings.py like this::
  170. NOSE_ARGS = ['--failed', '--stop']
  171. .. _nose.cfg or setup.cfg: http://somethingaboutorange.com/mrl/projects/nose/0.11.2/usage.html#configuration
  172. Custom Plugins
  173. --------------
  174. If you need to `make custom plugins`_, you can define each plugin class
  175. somewhere within your app and load them from settings.py like this::
  176. NOSE_PLUGINS = [
  177. 'yourapp.tests.plugins.SystematicDysfunctioner',
  178. # ...
  179. ]
  180. Just like middleware or anything else, each string must be a dot-separated,
  181. importable path to an actual class. Each plugin class will be instantiated and
  182. added to the Nose test runner.
  183. .. _make custom plugins: http://somethingaboutorange.com/mrl/projects/nose/0.11.2/plugins.html#writing-plugins
  184. Older Versions of Django
  185. ------------------------
  186. Upgrading from Django <= 1.3 to Django 1.4
  187. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  188. In versions of Django < 1.4 the project folder was in fact a python package as
  189. well (note the __init__.py in your project root). In Django 1.4, there is no
  190. such file and thus the project is not a python module.
  191. **When you upgrade your Django project to the Django 1.4 layout, you need to
  192. remove the __init__.py file in the root of your project (and move any python
  193. files that reside there other than the manage.py) otherwise you will get a
  194. `ImportError: No module named urls` exception.**
  195. This happens because Nose will intelligently try to populate your sys.path, and
  196. in this particular case includes your parent directory if your project has a
  197. __init__.py file (see: https://github.com/nose-devs/nose/blob/release_1.1.2/nose/importer.py#L134).
  198. This means that even though you have set up your directory structure properly and
  199. set your `ROOT_URLCONF='my_project.urls'` to match the new structure, when running
  200. django-nose's test runner it will try to find your urls.py file in `'my_project.my_project.urls'`.
  201. Upgrading from Django < 1.2
  202. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  203. Django 1.2 switches to a `class-based test runner`_. To use django-nose
  204. with Django 1.2, change your ``TEST_RUNNER`` from ``django_nose.run_tests`` to
  205. ``django_nose.NoseTestSuiteRunner``.
  206. ``django_nose.run_tests`` will continue to work in Django 1.2 but will raise a
  207. warning. In Django 1.3, it will stop working.
  208. If you were using ``django_nose.run_gis_tests``, you should also switch to
  209. ``django_nose.NoseTestSuiteRunner`` and use one of the `spatial backends`_ in
  210. your ``DATABASES`` settings.
  211. .. _class-based test runner: http://docs.djangoproject.com/en/dev/releases/1.2/#function-based-test-runners
  212. .. _spatial backends: http://docs.djangoproject.com/en/dev/ref/contrib/gis/db-api/#id1
  213. Django 1.1
  214. ~~~~~~~~~~
  215. If you want to use django-nose with Django 1.1, use
  216. https://github.com/django-nose/django-nose/tree/django-1.1 or
  217. http://pypi.python.org/pypi/django-nose/0.0.3.
  218. Django 1.0
  219. ~~~~~~~~~~
  220. django-nose does not support Django 1.0.
  221. Recent Version History
  222. ----------------------
  223. 1.3 (2014-12-05)
  224. * Django 1.6 and 1.7 support (conrado, co3k, Nepherhotep, mbertheau)
  225. * Python 3.3 and 3.4 testing and support (frewsxcv, jsocol)
  226. 1.2 (2013-07-23)
  227. * Python 3 support (melinath and jonashaag)
  228. * Django 1.5 compat (fabiosantoscode)
  229. 1.1 (2012-05-19)
  230. * Django TransactionTestCases don't clean up after themselves; they leave
  231. junk in the DB and clean it up only on ``_pre_setup``. Thus, Django makes
  232. sure these tests run last. Now django-nose does, too. This means one fewer
  233. source of failures on existing projects. (Erik Rose)
  234. * Add support for hygienic TransactionTestCases. (Erik Rose)
  235. * Support models that are used only for tests. Just put them in any file
  236. imported in the course of loading tests. No more crazy hacks necessary.
  237. (Erik Rose)
  238. * Make the fixture bundler more conservative, fixing some conceivable
  239. situations in which fixtures would not appear as intended if a
  240. TransactionTestCase found its way into the middle of a bundle. (Erik Rose)
  241. * Fix an error that would surface when using SQLAlchemy with connection
  242. pooling. (Roger Hu)
  243. * Gracefully ignore the new ``--liveserver`` option introduced in Django 1.4;
  244. don't let it through to nose. (Adam DePue)
  245. 1.0 (2012-03-12)
  246. * New fixture-bundling plugin for avoiding needless fixture setup (Erik Rose)
  247. * Moved FastFixtureTestCase in from test-utils, so now all the
  248. fixture-bundling stuff is in one library. (Erik Rose)
  249. * Added the REUSE_DB setting for faster startup and shutdown. (Erik Rose)
  250. * Fixed a crash when printing options with certain verbosities. (Daniel Abel)
  251. * Broke hard dependency on MySQL. Support PostgreSQL. (Roger Hu)
  252. * Support SQLite, both memory- and disk-based. (Roger Hu and Erik Rose)
  253. * Nail down versions of the package requirements. (Daniel Mizyrycki)
  254. 0.1.3 (2010-04-15)
  255. * Even better coverage support (rozza)
  256. * README fixes (carljm and ionelmc)
  257. * optparse OptionGroups are handled better (outofculture)
  258. * nose plugins are loaded before listing options
  259. See more in changelog.txt.