djangosnippets.org: Latest snippets tagged with 'unittest'https://djangosnippets.org/tags/unittest/2016-05-03T12:05:40.084157-05:00Testing for pending migrations in Django
2016-05-03T12:05:40.084157-05:00dlespiauhttps://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 redistributableUnit Test Profiling for Django 1.3/1.4
2012-04-22T11:14:27.254272-05:00hoffmajehttps://djangosnippets.org/snippets/2734/<p>The snippet is a modification of <a href="http://djangosnippets.org/snippets/1315/">snippet 1315</a> to fit the needs for Django 1.3 and 1.4. You can follow the explanations and instructions there.</p>
<p>To plot a nice and so useful call-graph with timings, call:</p>
<p>$ gprof2dot -f pstats unittest.profile | dot -Tpng -o unittest.profile.graph.png</p>
<p>where 'unittest.profile' is the …</p>
Freely redistributablechange settings locally in an individual test
2010-08-19T04:41:55.030302-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 …</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 redistributableQuery printer coroutine
2009-04-25T11:49:30.573748-05:00fnlhttps://djangosnippets.org/snippets/1465/<p>If you would like to see the latest queries you have done when running a unittest, this is not so easy. You have to initialize the queries list and set DEBUG to True manually. Then you have to figure out a way to print the queries you want to see …</p>
Freely redistributableUnitTesting without create/destroy database
2009-02-11T07:20:11.258283-06:00crucialfelixhttps://djangosnippets.org/snippets/1318/<p>This test runner is invoked with its own command:</p>
<p>./manage.py quicktest {usual test args follow}</p>
<p>this creates a test database if it needs to and then DOES NOT delete it. subsequent uses of it start with the same database. this is for rapid test/development cycles usually running a single test. …</p>
Freely redistributableUnit Test Profiling
2009-02-08T11:21:04.474656-06:00justquickhttps://djangosnippets.org/snippets/1315/<p>A test runner for Django unittests which profiles the tests run, and saves the result. Very useful for diagnosing your apps. Place the top portion of the code into a file called <code>profiling.py</code> someplace in your python path. Update your <code>settings.py</code> file with the bottom two lines of the code. …</p>
Freely redistributableUnit Tests That Write Fixtures
2009-02-04T13:26:40.989164-06:00justquickhttps://djangosnippets.org/snippets/1309/<p>This is a skeleton framework of a unittest for an app which will write out a fixture of the test database once the test has been done. I run this once for all apps, but you can limit which apps get serialized by modifying the self.apps value from get_apps (all …</p>
Freely redistributableUnittest: Compare max_size with column size of database
2008-08-18T08:25:22.564520-05:00guettlihttps://djangosnippets.org/snippets/990/<p>Changing the size of max_length in the model is fast. But sometimes you forget to
update all running systems which use this model.</p>
<p>This unittest helps you to find the difference between Model and DB before the users get uncaught exceptions.</p>
Freely redistributableStop tests at the first failure
2008-01-03T06:28:37.002861-06:00akaiholahttps://djangosnippets.org/snippets/534/<p><strong>Note</strong>: The <code>--failfast</code> argument in Django since version 1.2 does this. Use this snippet for earlier versions.
</p>
<p>If a large number of your unit tests get "out of sync", it's often annoying to scan through a large number of test failures which overflow the terminal window's scroll buffer.
</p>
<p>This …</p>
Freely redistributableDb Mock
2007-07-27T09:46:55.788099-05:00akhavrhttps://djangosnippets.org/snippets/345/<p>I hate when my unittest hits database. Especially when each test case needs different dataset.
</p>
<p>So I wrote this db mock, that's local to specific test and uses sqlite3 in-memory db.
</p>
<p>Usage (nosetests):
</p>
<pre><code>class TestMainNoData(DbMock):
'testing main function with no meaningful data'
def test_no_posts(self):
'there are no posts'
assert models.Post.objects.count() …</code></pre>
Freely redistributableTestCase helpers
2007-03-27T13:59:50.706824-05:00pbxhttps://djangosnippets.org/snippets/137/<p>I use these helper methods in my unit tests. They turn many simple getting-and-posting tests into one-liners. Definitely a work in progress, and I can't be the only person who has done this sort of thing -- comments are more than welcome.
</p>
Freely redistributable