djangosnippets.org: Latest snippets tagged with 'testing'http://djangosnippets.org/tags/testing/2016-05-03T12:05:40.084157-05:00Testing for pending migrations in Django
2016-05-03T12:05:40.084157-05:00dlespiauhttp://djangosnippets.org/snippets/10567/<p>DB migration support has been added in Django 1.7+, superseding South. More specifically, it's possible to automatically generate migrations steps when one or more changes in the application models are detected. Definitely a nice feature!</p>
<p>I've written a small generic unit-test that one should be able to drop into the …</p>
Freely redistributableTesting (and mocking) http requests to APIs etc.
2015-03-09T10:07:22.419835-05:00monikasulikhttp://djangosnippets.org/snippets/10470/<p>You can test how your django app behaves depending on what kind of response it gets from the API. It assumes were're using the python requests library.</p>
Freely redistributableTesting email sending
2015-02-19T05:07:37.125086-06:00monikasulikhttp://djangosnippets.org/snippets/10466/<p>How I assert email is being sent correctly</p>
Freely redistributableTest runner that installs 'tests' packages as apps
2013-09-24T15:23:38.949323-05:00adrian_lchttp://djangosnippets.org/snippets/2994/<p>Ever tried to unit test custom fields or abstract models? If so, you probably used a solution like <a href="http://djangosnippets.org/snippets/2843/">this one</a>. It surely works, but it has some issues:</p>
<ol>
<li>Runs 'syncdb' several times.</li>
<li>It's not automatic. You must add the mixin or copy the code to each of the TestCases. …</li></ol>
Freely redistributableRun a testcase with custom INSTALLED_APPS
2012-11-08T04:40:40.371984-06:00vdboorhttp://djangosnippets.org/snippets/2843/<p>This code allows you to register a model to Django that is only used for unit testing.
It will not exist in the regular Django workflow. After the tests executed, the Django settings are restored.</p>
<p>Usage:</p>
<ol>
<li>Change <code>tests.py</code> into a <code>tests</code> package.</li>
<li>Place a <code>models.py</code> in the <code>tests</code> package.</li>
<li>Use …</li></ol>
Freely redistributableSyntax highlighting for tracebacks in console output
2012-08-20T08:57:44.305316-05:00jezdezhttp://djangosnippets.org/snippets/2805/<p>This is hardcoded to use <a href="http://pypi.python.org/pypi/django-discover-runner">django-discover-runner</a> since that's my main test runner but could easily be adopted to use Django's own test runner. If you're using a terminal that is capable of showing 256 colors use the <code>Terminal256Formatter</code> formatter instead.</p>
<p>Enabled it with the <code>TEST_RUNNER</code> setting:</p>
<pre><code>TEST_RUNNER = 'dotted.path.to.highlighted.runner.HighlightedDiscoverRunner'
</code></pre>
<p>Where …</p>
Freely redistributableLoad response.content in browser (for debugging)
2012-05-07T22:03:59.220874-05:00tin_nqnhttp://djangosnippets.org/snippets/2745/<p>When debugging tests you frequently need to inspect response content, making a pdb. set_trace() breakpoint and printing response.content </p>
<p>but html isn't enough human readable (even for programmers :D) so, why not open it in your browser? Suposse you save this code in utils.py and you break your testcase as this: …</p>
Freely redistributableTest Suite URL Coverage
2012-02-24T11:24:28.944697-06:00jterracehttp://djangosnippets.org/snippets/2694/<p>This custom test suite runner will record all of the URLs accessed during your test suite. It will compare it to the list of all URLs you have configured for your site and produce a report of any URLs missed. It requires that all URLs are named (using the <code>name= …</code></p>
Freely redistributableUnit testing Django model mixins
2012-01-26T13:43:07.672809-06:00michaelmiorhttp://djangosnippets.org/snippets/2663/<p>This facilitates unit tests for model mixins in Django. For more info, see <a href="https://michael.mior.ca/blog/unit-testing-django-model-mixins/">my blog</a>.</p>
Freely redistributableLazy Man's Django test case
2011-11-13T15:49:20.879018-06:00prestontimmonshttp://djangosnippets.org/snippets/2600/<p>Decrease the verbosity of writing view tests.</p>
<p>Also available here: https://gist.github.com/1310549.</p>
Freely redistributableFuzzy testing with assertNumQueries
2011-01-12T10:41:59.698608-06:00spookylukeyhttp://djangosnippets.org/snippets/2320/<p>Django 1.3 has an assertNumQueries method which will allows you to simply specify the number of queries you expect. Sometimes, however, specifying the exact number of queries is overkill, and makes the test too brittle. This code provides a way to make more forgiving tests.
</p>
<p>See http://lukeplant.me.uk/blog/posts/fuzzy-testing-with-assertnumqueries/
</p>
Freely redistributableModify requests in your unit tests (improvement on RequestFactory)
2010-10-07T17:21:44.816144-05:00vaughnkochhttp://djangosnippets.org/snippets/2231/<p>This is an update to Simon Willison's snippet http://djangosnippets.org/snippets/963/, along with one of the comments in that snippet.<br />
</p>
<p>This class lets you create a Request object that's gone through all the middleware. Suitable for unit testing when you need to modify something on the request directly, or pass in a …</p>
Freely redistributableQuery count decorator for TestCase
2010-10-04T14:01:18.671390-05:00coleiferhttp://djangosnippets.org/snippets/2219/<p>Sometimes you may want to check how many queries a specific set of operations is taking. This TestCase decorator allows you to do that.
</p>
Freely redistributableExclude Apps When Testing
2010-09-29T14:35:59.735403-05:00cronosahttp://djangosnippets.org/snippets/2211/<p>This allows you to exclude certain apps when doing standard tests (manage.py test) by default. You set the settings/local_settings variable EXCLUDE_APPS and it will exclude those apps (like django, registration, south... etc). This makes running tests much faster and you don't have to wait for a bunch of tests you …</p>
Freely redistributablechange settings locally in an individual test
2010-08-19T04:41:55.030302-05:00akaiholahttp://djangosnippets.org/snippets/2156/<p>So you need to change some settings when running an individual test in a test case. You could just wrap the test between <code>old_value = settings.MY_SETTING</code> and <code>settings.MY_SETTING = old_value</code>. This snippet provides a helper which makes this a bit more convenient, since settings are restored to their old …</p>
Freely redistributable