<?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>The Mouse Vs. The Python &#187; Testing</title>
	<atom:link href="http://www.blog.pythonlibrary.org/tag/testing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blog.pythonlibrary.org</link>
	<description>Python Programming from the Frontlines</description>
	<lastBuildDate>Sun, 08 Jan 2012 12:45:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Python 102: An Intro to TDD and unittest</title>
		<link>http://www.blog.pythonlibrary.org/2011/03/09/python-102-an-intro-to-tdd-and-unittest/</link>
		<comments>http://www.blog.pythonlibrary.org/2011/03/09/python-102-an-intro-to-tdd-and-unittest/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 21:34:25 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Cross-Platform]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[TIP]]></category>
		<category><![CDATA[unittest]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=1655</guid>
		<description><![CDATA[Python code testing is something new to me. It&#8217;s not required where I work, so I haven&#8217;t spent much time looking into it, besides reading a book on the subject and reading a few blogs. However, I decided it was high time I check this out and see what all the excitement is about. In [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2011/03/09/python-102-an-intro-to-tdd-and-unittest/" data-url="http://bit.ly/ssaGjM" data-text="Python 102: An Intro to TDD and unittest" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2011/03/09/python-102-an-intro-to-tdd-and-unittest/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div></div><p>Python code testing is something new to me. It&#8217;s not required where I work, so I haven&#8217;t spent much time looking into it, besides reading a book on the subject and reading a few blogs. However, I decided it was high time I check this out and see what all the excitement is about. In this article, you will learn about Test Driven Development (TDD) with Python using Python&#8217;s builtin unittest module. This is actually based on my one experience of TDD and pair programming (thanks Matt and Aaron!). In this article, we will be learning how to score bowling with Python!<span id="more-1655"></span></p>
<h2>Getting Started</h2>
<p>I did a search for how to score bowling on the internet. I found a helpful tutorial on <a href="http://bowling.about.com/od/rulesofthegame/a/bowlingscoring.htm">About.com</a> that you&#8217;re welcome to read too. Once you know the rules, it&#8217;s time to write some tests. In case you didn&#8217;t know, the idea behind Test Driven Development is that you write the tests BEFORE you write the actual code. In this article, we will write a test, then some code to pass the test. We will iterate back and forth between writing tests and code until we&#8217;re done. For this article, we&#8217;ll write just three tests. Let&#8217;s get started!</p>
<h2>The First Test</h2>
<p>Our first test will be to test our game object and see if it can calculate the correct total if we roll eleven times and only knock over one pin each time. This should give us a total of eleven.</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> TestBowling<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> test_all_ones<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        game = Game<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        game.<span style="color: black;">roll</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>game.<span style="color: black;">score</span>, <span style="color: #ff4500;">11</span><span style="color: black;">&#41;</span></pre>
<p>This is a pretty simple test. We create a game object and then call its <strong>roll</strong> method eleven times with a score of one each time. Then we use the <strong>assertEqual</strong> method from the <strong>unittest </strong>module to test if the game object&#8217;s score is correct (i.e. eleven). The next step is to write the simplest code you can think of to make the test pass. Here&#8217;s one example:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> Game:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">score</span> = <span style="color: #ff4500;">0</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> roll<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, numOfRolls, pins<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> roll <span style="color: #ff7700;font-weight:bold;">in</span> numOfRolls:
            <span style="color: #008000;">self</span>.<span style="color: black;">score</span> += pins</pre>
<p>For simplicity&#8217;s sake, you can just copy and paste that into the same file with your test. We&#8217;ll break them into two files for our next test. Anyway, as you can see, our <strong>Game</strong> class is super simple. All that was needed to pass the test was a score property and a <strong>roll</strong> method that can update it. If you don&#8217;t know how <strong>for</strong> loops work, then you need to go read a <a href="http://docs.python.org/tutorial/">Python tutorial</a>.</p>
<p>Let&#8217;s run the test and see if it passes! The easiest way to run the tests is to add the following two lines of code to the bottom of the file:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    <span style="color: #dc143c;">unittest</span>.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Then just run the Python file via the command line, If you do, you should get something like the following:</p>
<pre class="python">E
======================================================================
ERROR: test_all_ones <span style="color: black;">&#40;</span><span style="color: #dc143c;">__main__</span>.<span style="color: black;">TestBowling</span><span style="color: black;">&#41;</span>
Constructor
----------------------------------------------------------------------
Traceback <span style="color: black;">&#40;</span>most recent call last<span style="color: black;">&#41;</span>:
  File <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\U</span>sers<span style="color: #000099; font-weight: bold;">\M</span>ike<span style="color: #000099; font-weight: bold;">\D</span>ocuments<span style="color: #000099; font-weight: bold;">\M</span>y Dropbox<span style="color: #000099; font-weight: bold;">\S</span>cripts<span style="color: #000099; font-weight: bold;">\T</span>esting<span style="color: #000099; font-weight: bold;">\b</span>owling<span style="color: #000099; font-weight: bold;">\t</span>est_one.py&quot;</span>,
 line <span style="color: #ff4500;">27</span>, <span style="color: #ff7700;font-weight:bold;">in</span> test_all_ones
    game.<span style="color: black;">roll</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>
  File <span style="color: #483d8b;">&quot;C:<span style="color: #000099; font-weight: bold;">\U</span>sers<span style="color: #000099; font-weight: bold;">\M</span>ike<span style="color: #000099; font-weight: bold;">\D</span>ocuments<span style="color: #000099; font-weight: bold;">\M</span>y Dropbox<span style="color: #000099; font-weight: bold;">\S</span>cripts<span style="color: #000099; font-weight: bold;">\T</span>esting<span style="color: #000099; font-weight: bold;">\b</span>owling<span style="color: #000099; font-weight: bold;">\t</span>est_one.py&quot;</span>,
 line <span style="color: #ff4500;">15</span>, <span style="color: #ff7700;font-weight:bold;">in</span> roll
    <span style="color: #ff7700;font-weight:bold;">for</span> roll <span style="color: #ff7700;font-weight:bold;">in</span> numOfRolls:
<span style="color: #008000;">TypeError</span>: <span style="color: #483d8b;">'int'</span> <span style="color: #008000;">object</span> <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> iterable
&nbsp;
----------------------------------------------------------------------
Ran <span style="color: #ff4500;">1</span> <span style="color: #dc143c;">test</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #ff4500;">0</span>.001s
&nbsp;
FAILED <span style="color: black;">&#40;</span>errors=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre>
<p>Oops! We&#8217;ve got a mistake in there somewhere. It looks like we&#8217;re passing an Integer and then trying to iterate over it. That doesn&#8217;t work! We need to change our Game object&#8217;s roll method to the following to make it work:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
<span style="color: #ff7700;font-weight:bold;">def</span> roll<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, numOfRolls, pins<span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
    <span style="color: #ff7700;font-weight:bold;">for</span> roll <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>numOfRolls<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">score</span> += pins</pre>
<p>If you run the test now, you should get the following:</p>
<pre class="python">.
----------------------------------------------------------------------
Ran <span style="color: #ff4500;">1</span> <span style="color: #dc143c;">test</span> <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #ff4500;">0</span>.000s
&nbsp;
OK</pre>
<p>Note the &#8220;.&#8221; because it&#8217;s important. That little dot means that one test has run and that it passed. The &#8220;OK&#8221; at the end clues you into that fact as well. If you study the original output, you&#8217;ll notice it leads off with an &#8220;E&#8221; for error and there&#8217;s no dot! Let&#8217;s move on to test #2.</p>
<h2>The Second Test</h2>
<p>For the second test, we&#8217;ll test what happens when we get a strike. We&#8217;ll need to change the first test to use a list for the number of pins knocked down in each frame though, so we&#8217;ll look at both tests here. You&#8217;ll probably find this to be a fairly common process where you may need to edit a couple of tests due to fundamental changes in what you&#8217;re testing for. Normally this will only happen at the beginning of your coding and you will get better later on such that you shouldn&#8217;t need to do this. Since this is my first time doing this, I wasn&#8217;t thinking far enough a head. Anyway, let&#8217;s take a look at the code:</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> game <span style="color: #ff7700;font-weight:bold;">import</span> Game
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> TestBowling<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> test_all_ones<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        game = Game<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        pins = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
        game.<span style="color: black;">roll</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span>, pins<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>game.<span style="color: black;">score</span>, <span style="color: #ff4500;">11</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> test_strike<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        A strike is 10 + the value of the next two rolls. So in this case
        the first frame will be 10+5+4 or 19 and the second will be
        5+4. The total score would be 19+9 or 28.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        game = Game<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        game.<span style="color: black;">roll</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span>, <span style="color: black;">&#91;</span><span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">5</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span>game.<span style="color: black;">score</span>, <span style="color: #ff4500;">28</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    <span style="color: #dc143c;">unittest</span>.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>Let&#8217;s take a look at our first test and how it changed. Yes, we&#8217;re breaking the rules here a bit when it comes to TDD. Feel free to NOT change the first test and see what breaks. In the <strong>test_all_ones</strong> we <strong>pins</strong> to equal list comprehension which created a list of eleven ones. Then we passed that to our <strong>game </strong>object&#8217;s <strong>roll</strong> method along with the number of rolls.</p>
<p>In the second test, we roll a strike in our first roll, a five in our second and a four in our third. Note that we went a head and told it that we were passing in eleven rolls and yet we only pass in three. This means that we need to set the other eight rolls to zeros. Finally, we use our trusty <strong>assertEqual</strong> method to check if we get the right total. Finally, note that we&#8217;re now importing the Game class rather than keeping it with the tests. Now we need to implement the code necessary to pass these two tests. Let&#8217;s take a look at one possible solution:</p>
<pre class="python"><span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> Game:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">score</span> = <span style="color: #ff4500;">0</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">pins</span> = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> roll<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, numOfRolls, pins<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        x = <span style="color: #ff4500;">0</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> pin <span style="color: #ff7700;font-weight:bold;">in</span> pins:
            <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span> = pin
            x += <span style="color: #ff4500;">1</span>
        x = <span style="color: #ff4500;">0</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> roll <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>numOfRolls<span style="color: black;">&#41;</span>:
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span> == <span style="color: #ff4500;">10</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">score</span> = <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span> + <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span> + <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: #ff4500;">+2</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">score</span> += <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span>
            x += <span style="color: #ff4500;">1</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">self</span>.<span style="color: black;">score</span></pre>
<p>Right off the bat, you will notice that we have a new class attribute called <strong>self.pins</strong> that holds the default pin list, which is eleven zeroes. Then in our <strong>roll</strong> method, we add the correct scores to the correct position in the self.pins list in the first loop. Then in the second loop, we check to see if the pins knocked down equals ten. If it does, we add it and the next two scores to score. Otherwise, we do what we dd before. At the end of the method, we print out the score to check if it&#8217;s what we expect. At this point, we&#8217;re ready to code up our final test.</p>
<h2>The Third (and Final) Test</h2>
<p>Our last test will test for the correct score that would result should someone roll a spare. The test is easy, the solution is slightly more difficult. While we&#8217;re at it, we&#8217;re going to refactor the test code a bit. As usual, we will look at the test first.</p>
<pre class="python"><span style="color: #ff7700;font-weight:bold;">from</span> game_v2 <span style="color: #ff7700;font-weight:bold;">import</span> Game
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">unittest</span>
&nbsp;
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> TestBowling<span style="color: black;">&#40;</span><span style="color: #dc143c;">unittest</span>.<span style="color: black;">TestCase</span><span style="color: black;">&#41;</span>:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> setUp<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">game</span> = Game<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> test_all_ones<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        If you don't get a strike or a spare, then you just add up the
        face value of the frame. In this case, each frame is worth
        one point, so the total is eleven.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        pins = <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">game</span>.<span style="color: black;">roll</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span>, pins<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">game</span>.<span style="color: black;">score</span>, <span style="color: #ff4500;">11</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> test_spare<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        A spare is worth 10, plus the value of your next roll. So in this
        case, the first frame will be 5+5+5 or 15 and the second will be
        5+4 or 9. The total is 15+9, which equals 24,
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">game</span>.<span style="color: black;">roll</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span>, <span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span>, <span style="color: #ff4500;">5</span>, <span style="color: #ff4500;">5</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">game</span>.<span style="color: black;">score</span>, <span style="color: #ff4500;">24</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> test_strike<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;
        A strike is 10 + the value of the next two rolls. So in this case
        the first frame will be 10+5+4 or 19 and the second will be
        5+4. The total score would be 19+9 or 28.
        &quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">game</span>.<span style="color: black;">roll</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span>, <span style="color: black;">&#91;</span><span style="color: #ff4500;">10</span>, <span style="color: #ff4500;">5</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">assertEqual</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">game</span>.<span style="color: black;">score</span>, <span style="color: #ff4500;">28</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    <span style="color: #dc143c;">unittest</span>.<span style="color: black;">main</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre>
<p>First off, we added a <strong>setUp</strong> method that will create a self.game object for us for each test. If we were accessing a database or something like that, we would probably have a tear down method too for closing connections or files or that sort of thing. These are run at the beginning and end of each test respectively should they exist. The <strong>test_all_ones</strong> and <strong>test_strike</strong> tests are basically the same except that they are using &#8220;self.game&#8221; now. The only the new test is <strong>test_spare</strong>.  The docstring explains how spares work and the code is just two lines. Yes, you can figure this out. Let&#8217;s look at the code we&#8217;ll need to pass these tests:</p>
<pre class="python"><span style="color: #808080; font-style: italic;"># game_v2.py</span>
<span style="color: #808080; font-style: italic;">########################################################################</span>
<span style="color: #ff7700;font-weight:bold;">class</span> Game:
    <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;Constructor&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">score</span> = <span style="color: #ff4500;">0</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">pins</span> = <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span> <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">11</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
&nbsp;
    <span style="color: #808080; font-style: italic;">#----------------------------------------------------------------------</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> roll<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, numOfRolls, pins<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span><span style="color: #483d8b;">&quot;&quot;</span>
        x = <span style="color: #ff4500;">0</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> pin <span style="color: #ff7700;font-weight:bold;">in</span> pins:
            <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span> = pin
            x += <span style="color: #ff4500;">1</span>
        x = <span style="color: #ff4500;">0</span>
        spare_begin = <span style="color: #ff4500;">0</span>
        spare_end = <span style="color: #ff4500;">2</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> roll <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>numOfRolls<span style="color: black;">&#41;</span>:
            spare = <span style="color: #008000;">sum</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>spare_begin:spare_end<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span> == <span style="color: #ff4500;">10</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">score</span> = <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span> + <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: #ff4500;">+1</span><span style="color: black;">&#93;</span> + <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: #ff4500;">+2</span><span style="color: black;">&#93;</span>
            <span style="color: #ff7700;font-weight:bold;">elif</span> spare == <span style="color: #ff4500;">10</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">score</span> = spare + <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: #ff4500;">+2</span><span style="color: black;">&#93;</span>
                x += <span style="color: #ff4500;">1</span>
            <span style="color: #ff7700;font-weight:bold;">else</span>:
                <span style="color: #008000;">self</span>.<span style="color: black;">score</span> += <span style="color: #008000;">self</span>.<span style="color: black;">pins</span><span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span>
            x += <span style="color: #ff4500;">1</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> x == <span style="color: #ff4500;">11</span>:
                <span style="color: #ff7700;font-weight:bold;">break</span>
            spare_begin += <span style="color: #ff4500;">2</span>
            spare_end += <span style="color: #ff4500;">2</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">self</span>.<span style="color: black;">score</span></pre>
<p>For this part of the puzzle, we add to our conditional statement in our loop. To calculate the spare&#8217;s value, we use the <strong>spare_begin</strong> and <strong>spare_end</strong> list positions to get the right values from our list and then we sum them up. That&#8217;s what the <strong>spare</strong> variable is for. That may be better placed in the elif, but I&#8217;ll leave that for the reader to experiment with. Technically, that&#8217;s just the first half of the spare score. The second half are the next two rolls, which is what you&#8217;ll find in the calculation in the elif portion of the current code. The rest of the code is the same.</p>
<h2>Other Notes</h2>
<p>As you may have guessed, there is a whole lot more to the unittest module than what is covered here. There are lots of other asserts you can use to test results with. You can skip tests, run the tests from the command line, use a TestLoader to create a test suite and much, much more. Be sure to read the <a href="http://docs.python.org/library/unittest.html">full documentation</a> when you get the chance as this tutorial barely scratches the surface of this library.</p>
<h2>Wrapping Up</h2>
<p>I don&#8217;t know a whole lot about testing yet, but I learned a lot working on this article and hopefully didn&#8217;t botch anything too badly. If I did, let me know and I&#8217;ll update the post. Also feel free to let me know what other aspects of testing that I should cover in the future and I&#8217;ll take a look. I&#8217;d like to tackle some of the other unittest features I didn&#8217;t get to in this tutorial along with mock and nose. What would you suggest? </p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2011/03/09/python-102-an-intro-to-tdd-and-unittest/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Book Review: Python Testing</title>
		<link>http://www.blog.pythonlibrary.org/2010/03/06/book-review-python-testing/</link>
		<comments>http://www.blog.pythonlibrary.org/2010/03/06/book-review-python-testing/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 16:01:08 +0000</pubDate>
		<dc:creator>Mike</dc:creator>
				<category><![CDATA[Python]]></category>
		<category><![CDATA[Book Review]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.blog.pythonlibrary.org/?p=663</guid>
		<description><![CDATA[Before PyCon, I was approached by a representative from Packt Publishing to review one of their books. They wanted me to read Daniel Arbuckle&#8217;s Python Testing: Beginner&#8217;s Guide. I&#8217;m not really into testing frameworks or test driven development and thought this would be a good excuse to learn the methodology and see if it was [...]]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-left"><a href="http://twitter.com/share" class="twitter-share-button" data-counturl="http://www.blog.pythonlibrary.org/2010/03/06/book-review-python-testing/" data-url="http://bit.ly/rZcuAw" data-text="Book Review: Python Testing" data-count="vertical" data-via="socializeWP" ><!--Tweetter--></a></div><div class="socialize-in-button socialize-in-button-left"><iframe src="http://www.facebook.com/plugins/like.php?href=http://www.blog.pythonlibrary.org/2010/03/06/book-review-python-testing/&amp;layout=button_count&amp;show_faces=true&amp;width=100&amp;action=like&amp;font=arial&amp;colorscheme=light&amp;height=65" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px !important; height:65px;" allowTransparency="true"></iframe></div><div class="socialize-in-button socialize-in-button-left"><script type="text/javascript">
			<!-- 
			reddit_url = "http://www.blog.pythonlibrary.org/2010/03/06/book-review-python-testing/";
			reddit_title = "Book Review: Python Testing";	//-->
		</script><script type="text/javascript" src="http://www.reddit.com/static/button/button2.js"></script></div><div class="socialize-in-button socialize-in-button-left"><g:plusone size="small" href="http://www.blog.pythonlibrary.org/2010/03/06/book-review-python-testing/"></g:plusone></div></div><p>Before PyCon, I was approached by a representative from <a href="http://www.packtpub.com/python-testing-beginners-guide/book?utm_source=blog.pythonlibrary.org&#038;utm_medium=bookrev&#038;utm_content=blog&#038;utm_campaign=mdb_002508">Packt Publishing</a> to review one of their books. They wanted me to read Daniel Arbuckle&#8217;s <em><a href="http://www.amazon.com/Python-Testing-Beginners-Daniel-Arbuckle/dp/1847198848/ref=sr_1_1?ie=UTF8&#038;s=books&#038;qid=1267883216&#038;sr=8-1" rel="nofollow">Python Testing: Beginner&#8217;s Guide</a></em>. I&#8217;m not really into testing frameworks or test driven development and thought this would be a good excuse to learn the methodology and see if it was something that was actually valuable or just a lot of hype. The book is only 256 pages long, so I told the Packt contact that it would probably take me a week or so to review. After a week, she seemed kind of anxious that I wasn&#8217;t done. Thus, this is a partial review. I don&#8217;t like being pressured to review something fast. I want my reviews to be thorough and of the best quality I can make them. You will get a thorough review of the chapters that I did manage to finish. If you want a full review, I&#8217;m pretty sure someone else on Python Planet said they were doing one.<span id="more-663"></span></p>
<p>I read chapters 1-6 and then skimmed 7-10. The author writes in an engaging manner and the chapters are broken up into sections, which makes it easy to pick up, do a section, and put it back down without losing your place. The primary reason that I was slow to review this book is that it has lots of code examples and it takes quite a while to re-type them all. I hate technical books that are riddled with broken code, so I wanted to make sure that this book&#8217;s code and tests worked. If I review a book with lots of code in the future, I will insist on lots of time or an e-book copy so I can just copy and paste the code. </p>
<p>Anyway, the first couple of chapters went by with only minor errors in the text itself. The content was pretty good, but the editors missed simple stuff where the author used &#8220;white&#8221; instead of &#8220;write&#8221;. There are silly mistakes like this in most chapters. The real problems don&#8217;t happen until chapter 3, which is about creating unit tests with doctest. On page 45, there&#8217;s a code example that includes the following line:</p>
<pre class="python"><span style="color: #008000;">self</span>.<span style="color: black;">integrated_error</span> +q= err <span style="color: #66cc66;">*</span> delta</pre>
<p>Since the author recommends using Python 2.6, I thought that &#8220;+q=&#8221; might be some kind of new format that I was unaware of. But it&#8217;s just a typo. On page 48-50, he has a revised test from earlier in the chapter. The text clearly states that all revisions will be clearly marked. They are not and I was unable to find all the revisions to make the new version work. I didn&#8217;t want to re-type the entire thing in again, so I skipped it and decided to download the author&#8217;s code bundle from the publisher&#8217;s website.</p>
<p>Here is where things really went downhill for me. After getting the code, I noticed that the files and folders were not arranged by chapter, but by some other method. This made it very difficult to find what I was looking for. I eventually did find a chapters sub-folder in the &#8220;tests&#8221; folder (I think). My plan was to do a diff on that code versus mine. When I opened the book code though, I noticed that the author had included a bunch of mock tests in it. He doesn&#8217;t cover mock until chapter 4. I considered stopping here and just telling everyone I knew to avoid this book, but since I got it for free, I decided to soldier on.</p>
<p>Chapter 4 is on the <a href="http://labix.org/mocker">Python Mocker</a> package by Labix. This chapter was quite good and I think I learned a lot from it. The only downer was that the author mentioned that you could use easy_install to install this module and that by so doing, <a href="http://pypi.python.org/pypi/nose/0.11.3">Nose</a> would be ready to run. That was just plain dumb. Other than that, I thought this was a good chapter.</p>
<p>Chapter 5 introduced the reader to Python&#8217;s <a href="http://docs.python.org/library/unittest.html">unittest library</a>. I don&#8217;t recall if I tested all the code in this chapter or not, but I didn&#8217;t note anything that was wrong. I thought this was a good introduction to unittest, especially for a beginner. </p>
<p>For chapter 6, the topic is changed to Nose. As you may have noticed from my descriptions so far, the author is quite gradual in how he went through the topics. He started with doctests, moved to mock, then unittests and then Nose, which is kind of a test runner and enhancer. I had weird issues with Nose when I tried to easy_install it. For some reason, I ended up with the 0.10.1 version which didn&#8217;t work with all the commands in this book. Once I got the current version, everything worked better. I should note that I was unable to get the example on page 112 to pass nosetests. I have no idea what the problem is, although it has to do with the Python Mocker module. It may be that I mistyped something, but I couldn&#8217;t figure it out.</p>
<p>The author covers Nose&#8217;s configuration file briefly and goes over a few command line flags for nosetests. He also covers fixtures and how to use them to enhance your tests. Finally he shows how to use Nose&#8217;s pattern matching abilities to run tests that match a certain naming convention. Pretty cool stuff!</p>
<p>I started to skim in earnest after this chapter though. In seven, he does a step-by-step walk-through depicting how to test and create a personal scheduling program. I look forward to reading this section and seeing how complex or simple this is. There is certainly code aplenty in this section and lots of tests (or he may just be re-printing some of the examples in smaller parts for commentary). I have no idea if any of it is functional though.</p>
<p>In chapter 8, the book moves on to Web Testing with <a href="http://twill.idyll.org/">Twill</a>. I thought this was an interesting choice since Twill&#8217;s website says that it hasn&#8217;t been updated in over 3 years. I tested a few of the examples in this chapter and they worked as advertised. Much of this section is made up of explanations of Twill commands though. The last section purportedly shows how to integrate Twill tests with unittest tests. I didn&#8217;t do that part though.</p>
<p>Chapter 9 talks about Integration and System testing. I thought the idea of drawing concentric circles in a diagram to figure out how to create integrated units for testing was a good concept. That will probably appeal to the readers who like visuals. Alas, I didn&#8217;t really read anything more than the first couple of pages, but it looks like he uses his schedule planner example here as well.</p>
<p>The final chapter entitled &#8220;Other Testing Tools and Techniques&#8221;. I think this chapter looks fairly interesting, but didn&#8217;t get a chance to read it. Looking at the text now, it would seem to briefly cover the following: <a href="http://nedbatchelder.com/code/coverage/">coverage.py</a>, version control hooks (Bazaar,  Mercurial, Darcs, Subversion), and buildbot.</p>
<p>There is also an appendix with Pop Quiz answers.</p>
<p>Overall, I think the chapters that I read had a lot of good information and that is was very well organized. Unfortunately, the good was pretty badly marred by minor grammatical or sentence structure issues and the occasionally larger issue of broken code/tests. If it happens once, then I expect that there probably other code or tests that are broken as well. If this is the case, then I recommend buying it used or wait for a Packt sale of some sort. On the other hand, if the book is pristine after chapter 6&#8230;well, I guess that really doesn&#8217;t matter since that would mean that over half the book had problems.</p>
<p>Okay. Here&#8217;s a better recommendation: Find a Borders or Barnes &#038; Nobles near you that has the book and free wi-fi. Go there, find the book, and read it and mess around with the examples BEFORE you buy it. </p>
<p><strong>Rating Scale: 1 (low) &#8211; 5 (high)</strong></p>
<p>Code rating = 3<br />
Writing = 4<br />
Organization = 5</p>
<p>Average Score = 4  (take a point or two off if broken code annoys you)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blog.pythonlibrary.org/2010/03/06/book-review-python-testing/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.544 seconds -->

