mirror of
https://github.com/kennethreitz/dive-into-python3.git
synced 2026-06-05 23:10:17 +00:00
fixed FIXMEs
This commit is contained in:
+2
-2
@@ -141,7 +141,7 @@ function to_roman(n):
|
||||
'''convert integer to Roman numeral'''
|
||||
<a> pass <span class=u>①</span></a></code></pre>
|
||||
<ol>
|
||||
<li>At this stage, you want to define the <abbr>API</abbr> of the <code>to_roman()</code> function, but you don’t want to code it yet. (Your test needs to fail first.) To stub it out, use the Python reserved word <code>pass</code> [FIXME ref], which does precisely nothing.
|
||||
<li>At this stage, you want to define the <abbr>API</abbr> of the <code>to_roman()</code> function, but you don’t want to code it yet. (Your test needs to fail first.) To stub it out, use the Python reserved word <code>pass</code>, which does precisely nothing.
|
||||
</ol>
|
||||
<p>Execute <code>romantest1.py</code> on the command line to run the test. If you call it with the <code>-v</code> command-line option, it will give more verbose output so you can see exactly what’s going on as each test case runs. With any luck, your output should look like this:
|
||||
<pre class=screen>
|
||||
@@ -254,7 +254,7 @@ OK</samp></pre>
|
||||
<li>Like the previous test case, the test itself is a method of the class, with a name starting with <code>test</code>.
|
||||
<li>The <code>unittest.TestCase</code> class provides the <code>assertRaises</code> method, which takes the following arguments: the exception you’re expecting, the function you’re testing, and the arguments you’re passing to that function. (If the function you’re testing takes more than one argument, pass them all to <code>assertRaises</code>, in order, and it will pass them right along to the function you’re testing.)
|
||||
</ol>
|
||||
<p>Pay close attention to this last line of code. Instead of calling <code>to_roman()</code> directly and manually checking that it raises a particular exception (by wrapping it in a <code>try...except</code> block [FIXME xref]), the <code>assertRaises</code> method has encapsulated all of that for us. All you do is tell it what exception you’re expecting (<code>roman2.OutOfRangeError</code>), the function (<code>to_roman()</code>), and the function’s arguments (<code>4000</code>). The <code>assertRaises</code> method takes care of calling <code>to_roman()</code> and checking that it raises <code>roman2.OutOfRangeError</code>.
|
||||
<p>Pay close attention to this last line of code. Instead of calling <code>to_roman()</code> directly and manually checking that it raises a particular exception (by wrapping it in <a href=your-first-python-program.html#exceptions>a <code>try...except</code> block</a>), the <code>assertRaises</code> method has encapsulated all of that for us. All you do is tell it what exception you’re expecting (<code>roman2.OutOfRangeError</code>), the function (<code>to_roman()</code>), and the function’s arguments (<code>4000</code>). The <code>assertRaises</code> method takes care of calling <code>to_roman()</code> and checking that it raises <code>roman2.OutOfRangeError</code>.
|
||||
<p>Also note that you’re passing the <code>to_roman()</code> function itself as an argument; you’re not calling it, and you’re not passing the name of it as a string. Have I mentioned recently how handy it is that <a href=your-first-python-program.html#everythingisanobject>everything in Python is an object</a>?
|
||||
<p>So what happens when you run the test suite with this new test?
|
||||
<pre class=screen>
|
||||
|
||||
Reference in New Issue
Block a user