djangosnippets.org: Latest snippets tagged with 'test'https://djangosnippets.org/tags/test/2012-11-08T04:40:40.371984-06:00Run a testcase with custom INSTALLED_APPS
2012-11-08T04:40:40.371984-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> package.</li>
<li>Use …</li></ol>
Freely redistributableSyntax highlighting for tracebacks in console output
2012-08-20T08:57:44.305316-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 = 'dotted.path.to.highlighted.runner.HighlightedDiscoverRunner'
</code></pre>
<p>Where …</p>
Freely redistributableCheck condition in Form Queryset
2011-12-02T10:57:46.242375-06:00felipecruzhttps://djangosnippets.org/snippets/2620/<p>Check a condition (in this example only equality) on every queryset item in a django form</p>
Freely redistributableAutomatic testing of add and changelist admin views
2011-08-20T03:33:24.948531-05:00Taifuhttps://djangosnippets.org/snippets/2518/<p>If you want to test for trivial error in your add and changelist admin views, use this snippet.</p>
<p>Save the snippet in admintests.py and put it anywhere in your pythonpath.</p>
<p>Put this code in your tests.py:</p>
<pre><code>from django.test import TestCase
from admintest import adminviews_test
class TestAdminViews(TestCase):
def test_admin_views(self):
adminviews_test(self)
</code></pre>
Freely redistributableClass that converts object to view.
2011-05-30T06:48:55.952636-05:00Pickelshttps://djangosnippets.org/snippets/2448/<p>Class that takes a normal none derived class and converts it into a view. The methods return simple datastructures which makes it easier to test.</p>
Freely redistributableView response's content in a browser while testing
2011-01-13T04:39:21.144636-06:00ryankask1https://djangosnippets.org/snippets/2321/<p>I often insert <code>pdb.set_trace()</code> in my test cases to debug and examine behavior. When tests fail with assertions like <code>assertContains(response, 'Some text')</code>, it would be useful to see the response's contents in a browser window. This snippet does just that. Simply put this code in a python script on …</p>
Freely redistributableMonkey-patch Django's test client to return WSGIRequest objects
2010-11-12T11:11:45.721242-06:00robmadolehttps://djangosnippets.org/snippets/2259/<p>Testing low-level functionality sometimes requires a WSGIRequest object. An example of this is testing template tags.
</p>
<p>This will monkey-patch the test Client object to return WSGIRequest objects
</p>
<p>Normal Django behavior:
</p>
<pre><code>>>> client.get('/')
<HttpResponse >
</code></pre><p>With this code, get the request object:
</p>
<pre><code>>>> client.request_from.get('/')
<WSGIRequest >
</code></pre><p>Installation:
</p>
<p>For this to work, …</p>
Freely redistributableClass-based coverage test runner
2010-06-04T08:43:16.169000-05:00brutassehttps://djangosnippets.org/snippets/2052/<p>A coverage test runner that uses the class-based runner introduced with Django 1.2.</p>
<p>Put it in your python path and add to your <code>settings.py</code>:</p>
<pre><code>TEST_RUNNER = 'path_to.CoverageRunner'
COVERAGE_MODULES = [
'blog.views',
'projects.views',
'middleware',
]
</code></pre>
<p>Compatible with Django 1.2 and higher. You also need Ned Batchelder's <code>coverage.py</code> module (<code>pip …</code></p>
Freely redistributableassertQuerysetEqual
2010-05-07T23:26:59.499097-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 lists …</p>
Freely redistributableFake SSL Middleware for Tests and Local Development
2010-05-05T17:19:15.980802-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 redistributableBreaking tests.py into multiple files
2010-04-01T12:31:41.778267-05:00gsakkishttps://djangosnippets.org/snippets/1972/<p>Django loads tests found in models.py and tests.py (if present) or actually a module or package named 'tests' under the app. Since tests can be a package, one can create a 'tests' directory, split the test cases across multiple files under 'tests' and import them from tests/<strong>init</strong>.py with: …</p>
Freely redistributableExternal service Test Client
2010-03-04T02:25:13.814144-06:00thejuhttps://djangosnippets.org/snippets/1950/<p>The inbuilt test client can be used to only test single domain applications ie no support for supplying absolute URLs.</p>
<p>But there are cases where one might like to test against URL rewrites, external domains/services like OpenID, OAuth etc.</p>
<p>This client has an external dependency on httplib2, to maintain the …</p>
Freely redistributableDynamic Test Loading
2010-01-07T12:19:13.495702-06:00cronosahttps://djangosnippets.org/snippets/1853/<p>Ok... this is really a hack. But I love it. I hate setting up all of my test cases into suites, and making sure that I remember to add them each time I add a new python file... annoying! This allows me to have a tests package and then just …</p>
Freely redistributableTesting with unmanaged models
2009-06-25T14:56:04.242842-05:00manfrehttps://djangosnippets.org/snippets/1596/<p>Many of my projects heavily depend on other non-django projects to create the databases. To simplify setting up a test environment, I modified the simple test runner so that it will treat all models as managed.</p>
<p>This will also allow for easier test set up against models that point to …</p>
Freely redistributableTest Server Thread
2009-06-17T11:43:53.868766-05:00adamloftshttps://djangosnippets.org/snippets/1570/<p>This class runs a django test server in another thread. This is very useful for e.g. selenium integration. Simple to integrate into django test framework.</p>
<p>Usage:</p>
<pre><code>server = TestServerThread("127.0.0.1", "8081")
server.start()
# Run tests e.g.
req = urllib.urlopen("http://127.0.0.1:8081")
contents = req.read()
server.stop()
</code></pre>
<p>ps. I don't actually double space my code …</p>
Freely redistributable