<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>if(is_geek)... &#187; testing</title>
	<atom:link href="http://www.ifisgeek.com/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ifisgeek.com</link>
	<description>Look! A New Doot!</description>
	<lastBuildDate>Thu, 03 Sep 2009 04:51:49 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Running JSLint in Automated Build Scripts</title>
		<link>http://www.ifisgeek.com/2009/05/05/running-jslint-in-automated-build-scripts/</link>
		<comments>http://www.ifisgeek.com/2009/05/05/running-jslint-in-automated-build-scripts/#comments</comments>
		<pubDate>Tue, 05 May 2009 18:29:14 +0000</pubDate>
		<dc:creator>Jeffery Read</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[VendAsta]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[build scripts]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.ifisgeek.com/?p=224</guid>
		<description><![CDATA[At VendAsta we use automated build scripts and a TeamCity server for continuous integration. Whenever someone commits code to a project, a whole suite of automated build targets are run against the code base to check things like unit tests, python syntax (via pylint) and code coverage. One thing that was missing from the mix [...]]]></description>
			<content:encoded><![CDATA[<p>At <a href="http://www.vendasta.com">VendAsta</a> we use automated build scripts and a <a href="http://www.jetbrains.com/teamcity/">TeamCity</a> server for continuous integration. Whenever someone commits code to a project, a whole suite of automated build targets are run against the code base to check things like unit tests, python syntax (via pylint) and code coverage. One thing that was missing from the mix was the automated running of jslint to check the syntax of our ever-increasing corpus of JavaScript code. As it turns out, adding this to our ANT targets wasn&#8217;t all that difficult.</p>
<p>The only real difficulty you are likely to encounter in creating a JSLint target for your build scripts is that JSLint is, itself, written in JavaScript. Other helpful libraries such as JSMin or the YUICompressor from Yahoo are distributed as jar files and can be executed easily with an <code>exec</code> block with the executable set to java. With JSLint, however, you are going to need a JavaScript engine which you can execute from the command line in order to run the JSLint script to validate your JavaScript code. THis actually sounds harder than it is.</p>
<p>For our implementation, I chose to use the <a href="http://www.mozilla.org/rhino/">Rhino</a> engine since there is documented support for JSLint on it, and even a handy helper file to include in your system: <a href="http://www.JSLint.com/rhino/rhino.js">rhino.js</a>. Grab a copy of Rhino (the actual file you&#8217;ll want to include is js.jar), the Rhino helper script I just mentioned and <a href="http://www.JSLint.com/fulljslint.js">JSLint itself</a>. Place them all together into some sort of tools directory in your project. Ours is located in /tools/ant.</p>
<p>Now comes the fun part, writing the actual target in ANT. For our purposes, we want to process all .js files contained in a particular location, as defined in a build property called <code>jsdir</code>. We also want to ignore any files that are contained in a vendor subdirectory of <code>jsdir</code>, as we should not need to worry about third-party scripts that don&#8217;t pass JSLint. Let&#8217;s get things started with our basic, empty target:</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- </span>
<span style="color: #808080; font-style: italic;">jslint - Runs lint checks on js files</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jslint&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h5>Listing 1: The basic, empty jslint target</h5>
</p>
<p>Within the target there are two things we are going to need. First a way to reference all of the files we want to operate over and second, the actual call to run JSLint on those files. For the first requirement we&#8217;ll use a <code>pathconvert</code> tag with a nested <code>fileset</code>. The <code>pathconvert</code> will allow us to generate a space-separated list of files and stuff that into a property which we&#8217;ll call <code>jsfiles</code>. The <code>fileset</code> block will do the actual work of finding the files in question. It will operate over <code>${jsdir}</code> including all .js files and excluding all .js files in the vendor subdirectory. Listing 2 shows the <code>pathconvert</code> block added to our empty target.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- </span>
<span style="color: #808080; font-style: italic;">jslint - Runs lint checks on js files</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jslint&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pathconvert</span> <span style="color: #000066;">pathsep</span>=<span style="color: #ff0000;">&quot; &quot;</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;jsfiles&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${jsdir}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;**/*.js&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exclude</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;vendor/**/*.js&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pathconvert<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h5>Listing 2: The <code>pathconvert</code> block added to our target.</h5>
</p>
<p>Finally, it is time to add the actual call to JSLint. To allow our build scripts to work for multiple projects, we have pushed as much information into properties that get defined in project-specific files as possible. The location of the Rhino jar (js.jar) is placed into the property <code>js.jar</code> and the location of the JSLint script (jslint.js) is placed into the property <code>jslint.js</code>. With those properties suitably defined elsewhere, we have the final piece of our puzzle: the <code>exec</code> block. We have the executable set to java and failonerror set to true, so that our target will appropriately fail when JSLint fails to give our code a passing grade. The final target, including the <code>exec</code> block can be seen in listing 3.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- </span>
<span style="color: #808080; font-style: italic;">jslint - Runs lint checks on js files</span>
<span style="color: #808080; font-style: italic;">--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;jslint&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pathconvert</span> <span style="color: #000066;">pathsep</span>=<span style="color: #ff0000;">&quot; &quot;</span> <span style="color: #000066;">property</span>=<span style="color: #ff0000;">&quot;jsfiles&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${jsdir}&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;**/*.js&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exclude</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;vendor/**/*.js&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pathconvert<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exec</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${jsdir}&quot;</span> <span style="color: #000066;">executable</span>=<span style="color: #ff0000;">&quot;java&quot;</span> <span style="color: #000066;">failonerror</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arg</span> <span style="color: #000066;">line</span>=<span style="color: #ff0000;">&quot;-jar ${js.jar} ${jslint.js} ${jsfiles}&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exec<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h5>Listing 3: The final target.</h5>
</p>
<p>Put all together, the <code>pathconvert</code> block stuffs the list of files to work on into the property <code>${jsfiles}</code> which is then passed as an argument to the jslint.js file. The jslint.js file is itself the first argument passed to the Rhino JavaScript engine (js.jar) which is run directly by java. Running your <code>ant jslint</code> target should give you some nice feedback on the syntax of your JavaScript code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ifisgeek.com/2009/05/05/running-jslint-in-automated-build-scripts/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Django Unittesting on Dreamhost</title>
		<link>http://www.ifisgeek.com/2008/12/27/django-unittesting-on-dreamhost/</link>
		<comments>http://www.ifisgeek.com/2008/12/27/django-unittesting-on-dreamhost/#comments</comments>
		<pubDate>Sun, 28 Dec 2008 04:56:08 +0000</pubDate>
		<dc:creator>Jeffery Read</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[dreamhost]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://www.ifisgeek.com/?p=150</guid>
		<description><![CDATA[The default test behaviour for a Django site creates and then destroys a whole test database on each test run. Normally this is not a problem, but the way Dreamhost has their MySQL setup running, the commands to create and drop databases are not available outside of their administration panel. What follows is the solution I have come up with and am currently using to run Django unittests with my Dreamhost MySQL databases.]]></description>
			<content:encoded><![CDATA[<p>Unit testing. It&#8217;s important. We all know that, even if we don&#8217;t practice what we preach. There was zero policy on unit testing at my last job, but now that I have started working at VendAsta, there is plenty of support for proper testing at the highest levels. Now that I am getting back into the habit of writing unit tests for my code at work I have been starting to focus on proper testing for my code at home. Combining Django and hosting with Dreamhost has proven to be a bit of a challenge.
<p>The first hurdle is actually getting a Django site running on Dreamhost. Luckily, there is this <a href="http://www.soasi.com/2008/09/django-10-on-dreamhost-with-passenger-mod_rails/">blog post</a> which details exactly how to get your site up and running on Dreamhost. I have used this method a couple of times (once modified to get a Satchmo install running) and have met with success each time.</p>
<p>Once the site is running, setting up tests poses another problem. The default test behaviour for a Django site creates and then destroys a whole test database on each test run. Normally this is not a problem, but the way Dreamhost has their MySQL setup running, the commands to create and drop databases are not available outside of their administration panel. What follows is the solution I have come up with and am currently using to run Django unittests with my Dreamhost MySQL databases.</p>
<p>First of all, it is important to understand a little about the default testing setup that Django uses. When you run the command <code>python manage.py test</code>, Django looks at the TEST_RUNNER setting to determine what to do. By default this setting points to django.test.simple.run_tests. A quick look and we can see that this is the functionality we want to alter. Specifically, lines 2 and 4 shown in listing #1 are what we need to replace.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> connection
connection.<span style="color: black;">creation</span>.<span style="color: black;">create_test_db</span><span style="color: black;">&#40;</span>verbosity, autoclobber=<span style="color: #ff7700;font-weight:bold;">not</span> interactive<span style="color: black;">&#41;</span>
result = <span style="color: #dc143c;">unittest</span>.<span style="color: black;">TextTestRunner</span><span style="color: black;">&#40;</span>verbosity=verbosity<span style="color: black;">&#41;</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span>suite<span style="color: black;">&#41;</span>
connection.<span style="color: black;">creation</span>.<span style="color: black;">destroy_test_db</span><span style="color: black;">&#40;</span>old_name, verbosity<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<h5>Listing 1: Excerpt from django.test.simple.run_tests</h5>
</p>
<p>It is these lines which make the calls to create and then drop the test database. The functionality needed, however, in order to work with Dreamhost&#8217;s setup, is to &#8220;flush&#8221; and existing test database both before and after the test run is complete. I have accomplished this through the creation of a method called <code>flush_test_db</code>, shown in Listing #2.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> flush_test_db<span style="color: black;">&#40;</span>test_db<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Method to flush out the test database, removing tables, before
    and after test runs&quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> connection 
    cursor = connection.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
&nbsp;
    current_tables =  connection.<span style="color: black;">introspection</span>.<span style="color: black;">table_names</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> table <span style="color: #ff7700;font-weight:bold;">in</span> current_tables:
        cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;use %s&quot;</span> <span style="color: #66cc66;">%</span> test_db<span style="color: black;">&#41;</span>
        cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;drop table %s&quot;</span> <span style="color: #66cc66;">%</span> table<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<h5>Listing 2: New method to remove all tables from the test db</h5>
</p>
<p>With this method, we can drop all of the tables in the test db both before and after a test run. It is necessary to do both just in case something goes horribly awry during a test run and we end up with leftover data from a previous test run. With this new method, we can then create a new version of the run_tests method and place both methods into a new file. I called my file test_runner.py and placed it in a tests directory within my app. The complete test_runner.py is shown in Listing 3.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #483d8b;">&quot;&quot;&quot;Override of the simple tests module from django in order to skip
database creation and use an existing db&quot;&quot;&quot;</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span>.<span style="color: black;">models</span> <span style="color: #ff7700;font-weight:bold;">import</span> get_app, get_apps
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span>.<span style="color: black;">backends</span>.<span style="color: black;">creation</span> <span style="color: #ff7700;font-weight:bold;">import</span> TEST_DATABASE_PREFIX
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: #dc143c;">test</span> <span style="color: #ff7700;font-weight:bold;">import</span> simple
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: #dc143c;">test</span>.<span style="color: black;">utils</span> <span style="color: #ff7700;font-weight:bold;">import</span> setup_test_environment, teardown_test_environment
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span> <span style="color: #ff7700;font-weight:bold;">import</span> management 
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> flush_test_db<span style="color: black;">&#40;</span>test_db<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;Method to flush out the test database, removing tables, before
    and after test runs&quot;&quot;&quot;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">db</span> <span style="color: #ff7700;font-weight:bold;">import</span> connection 
    cursor = connection.<span style="color: black;">cursor</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> 
&nbsp;
    current_tables =  connection.<span style="color: black;">introspection</span>.<span style="color: black;">table_names</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> table <span style="color: #ff7700;font-weight:bold;">in</span> current_tables:
        cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;use %s&quot;</span> <span style="color: #66cc66;">%</span> test_db<span style="color: black;">&#41;</span>
        cursor.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;drop table %s&quot;</span> <span style="color: #66cc66;">%</span> table<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> run_tests<span style="color: black;">&#40;</span>test_labels, verbosity=1, interactive=<span style="color: #008000;">True</span>, extra_tests=<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;&quot;
    Replacement for the run_tests in Django.
    &quot;&quot;&quot;</span>
    setup_test_environment<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    settings.<span style="color: black;">DEBUG</span> = <span style="color: #008000;">False</span>    
    suite = <span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestSuite</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> test_labels:
        <span style="color: #ff7700;font-weight:bold;">for</span> label <span style="color: #ff7700;font-weight:bold;">in</span> test_labels:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #483d8b;">'.'</span> <span style="color: #ff7700;font-weight:bold;">in</span> label:
                suite.<span style="color: black;">addTest</span><span style="color: black;">&#40;</span>simple.<span style="color: black;">build_test</span><span style="color: black;">&#40;</span>label<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                app = get_app<span style="color: black;">&#40;</span>label<span style="color: black;">&#41;</span>
                suite.<span style="color: black;">addTest</span><span style="color: black;">&#40;</span>simple.<span style="color: black;">build_suite</span><span style="color: black;">&#40;</span>app<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> app <span style="color: #ff7700;font-weight:bold;">in</span> get_apps<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
            suite.<span style="color: black;">addTest</span><span style="color: black;">&#40;</span>simple.<span style="color: black;">build_suite</span><span style="color: black;">&#40;</span>app<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">for</span> <span style="color: #dc143c;">test</span> <span style="color: #ff7700;font-weight:bold;">in</span> extra_tests:
        suite.<span style="color: black;">addTest</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">test</span><span style="color: black;">&#41;</span>
&nbsp;
    old_name = settings.<span style="color: black;">DATABASE_NAME</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">if</span> settings.<span style="color: black;">TEST_DATABASE_NAME</span>:
        test_database_name = settings.<span style="color: black;">TEST_DATABASE_NAME</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        test_database_name = TEST_DATABASE_PREFIX + settings.<span style="color: black;">DATABASE_NAME</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#replace the normal DATABASE_NAME with the name of the test db</span>
    settings.<span style="color: black;">DATABASE_NAME</span> = test_database_name
&nbsp;
    <span style="color: #808080; font-style: italic;">#flush the test db</span>
    flush_test_db<span style="color: black;">&#40;</span>test_database_name<span style="color: black;">&#41;</span>
    <span style="color: #808080; font-style: italic;">#load the test db with models</span>
    management.<span style="color: black;">call_command</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'syncdb'</span><span style="color: black;">&#41;</span>
&nbsp;
    result = <span style="color: #dc143c;">unittest</span>.<span style="color: black;">TextTestRunner</span><span style="color: black;">&#40;</span>verbosity=verbosity<span style="color: black;">&#41;</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span>suite<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#flush the test db again</span>
    flush_test_db<span style="color: black;">&#40;</span>test_database_name<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#reset the value of DATABASE_NAME in settings</span>
    settings.<span style="color: black;">DATABASE_NAME</span> = old_name
    teardown_test_environment<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>result.<span style="color: black;">failures</span><span style="color: black;">&#41;</span> + <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>result.<span style="color: black;">errors</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<h5>Listing 3: The complete test_runner.py</h5>
</p>
<p>The final step is to point the test framework to your new test runner by placing a new line in your settings.py. The exact value will vary based on where in your project you have placed the new test runner. In this example I have placed the test_runner.py file, containing the run_tests method in the tests directory of an app called &#8216;your_app&#8217;. This is shown in listing 4.</p>
<p>
<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="python" style="font-family:monospace;">TEST_RUNNER = <span style="color: #483d8b;">'your_app.tests.test_runner.run_tests'</span></pre></td></tr></table></div>

<h5>Listing 4: Settings.py setting pointing the test framework towards your new test_runner</h5>
</p>
<p>Finally a few words of caveat. I have omitted any comments I am using to tell pylint to ignore certain things and I have done little in this sample code to properly handle Exceptions that might arise. Nevertheless, this should get you a good start on running unit tests in Django if you host your site with Dreamhost.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ifisgeek.com/2008/12/27/django-unittesting-on-dreamhost/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
