djangosnippets.org: Latest snippets tagged with 'testing'https://djangosnippets.org/tags/testing/2015-02-19T05:07:37-06:00Testing email sending2015-02-19T05:07:37-06:00monikasulikhttps://djangosnippets.org/snippets/10466/<p>How I assert email is being sent correctly</p>
Freely redistributableTest runner that installs &#39;tests&#39; packages as apps2013-09-24T15:23:38-05:00adrian_lchttps://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_APPS2012-11-08T04:40:40-06:00vdboorhttps://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></li></ol>
Freely redistributableSyntax highlighting for tracebacks in console output2012-08-20T08:57:44-05:00jezdezhttps://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 ...</code></pre>
Freely redistributableLoad response.content in browser (for debugging)2012-05-07T22:03:59-05:00tin_nqnhttps://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 ...</p>
Freely redistributableTest Suite URL Coverage2012-02-24T11:24:28-06:00jterracehttps://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 mixins2012-01-26T13:43:07-06:00michaelmiorhttps://djangosnippets.org/snippets/2663/<p>This facilitates unit tests for model mixins in Django. For more info, see <a href="http://michael.mior.ca/2012/01/14/unit-testing-django-model-mixins/">my blog</a>.</p>
Freely redistributableLazy Man&#39;s Django test case2011-11-13T15:49:20-06:00prestontimmonshttps://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 assertNumQueries2011-01-12T10:41:59-06:00spookylukeyhttps://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 ...</p>
Freely redistributableModify requests in your unit tests (improvement on RequestFactory)2010-10-07T17:21:44-05:00vaughnkochhttps://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 ...</p>
Freely redistributableQuery count decorator for TestCase2010-10-04T14:01:18-05:00coleiferhttps://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 Testing2010-09-29T14:35:59-05:00cronosahttps://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 ...</p>
Freely redistributablechange settings locally in an individual test2010-08-19T04:41:55-05:00akaiholahttps://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 values ...</p>
Freely redistributableassertQuerysetEqual2010-05-07T23:26:59-05:00coleiferhttps://djangosnippets.org/snippets/2013/<p>I often find myself testing that the queryset returned by a method contains the instances I expect. I use a custom method, <strong>assertQuerysetEqual()</strong>, to test the equality of two querysets or lists::</p>
<pre><code>def test_some_values(self):
qs = get_user_list()
self.assertQuerysetEqual(qs, [normal_user, super_user])
</code></pre>
<p>Makes it easy to test small querysets against ...</p>
Freely redistributableFake SSL Middleware for Tests and Local Development2010-05-05T17:19:15-05:00DrMeershttps://djangosnippets.org/snippets/2009/<p>Add <code>FakeSSLMiddleware</code> to the top of your <code>MIDDLEWARE_CLASSES</code> stack when running tests or developing locally to allow https:// links to operate correctly. Can be used in conjunction with other SSL middleware to allow critical tests to be performed.</p>
Freely redistributable