|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <meta name="generator" content="pandoc"> |
| 6 | + <title>Software Carpentry: Testing</title> |
| 7 | + <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" /> |
| 8 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 9 | + <link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" /> |
| 10 | + <link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" /> |
| 11 | + <link rel="stylesheet" type="text/css" href="css/swc.css" /> |
| 12 | + <link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/> |
| 13 | + <meta charset="UTF-8" /> |
| 14 | + <!-- HTML5 shim, for IE6-8 support of HTML5 elements --> |
| 15 | + <!--[if lt IE 9]> |
| 16 | + <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> |
| 17 | + <![endif]--> |
| 18 | + </head> |
| 19 | + <body class="lesson"> |
| 20 | + <div class="container card"> |
| 21 | + <div class="banner"> |
| 22 | + <a href="http://software-carpentry.org" title="Software Carpentry"> |
| 23 | + <img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" /> |
| 24 | + </a> |
| 25 | + </div> |
| 26 | + <article> |
| 27 | + <div class="row"> |
| 28 | + <div class="col-md-10 col-md-offset-1"> |
| 29 | + <h1 class="title">Testing</h1> |
| 30 | + <h2 class="subtitle">Assertions</h2> |
| 31 | +<section class="objectives panel panel-warning"> |
| 32 | +<div class="panel-heading"> |
| 33 | +<h2><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2> |
| 34 | +</div> |
| 35 | +<div class="panel-body"> |
| 36 | +<ul> |
| 37 | +<li>Assertions are one line tests embedded in code.</li> |
| 38 | +<li>Assertions can halt execution if something unexpected happens.</li> |
| 39 | +<li>Assertions are the building blocks of tests.</li> |
| 40 | +</ul> |
| 41 | +</div> |
| 42 | +</section> |
| 43 | +<p>Assertions are the simplest type of test. They are used as a tool for bounding acceptable behavior during runtime. The assert keyword in python has the following behavior:</p> |
| 44 | +<pre class="sourceCode python"><code class="sourceCode python">>>> <span class="kw">assert</span> <span class="ot">True</span> == <span class="ot">False</span></code></pre> |
| 45 | +<pre class="output"><code>Traceback (most recent call last): |
| 46 | + File "<stdin>", line 1, in <module> |
| 47 | + AssertionError</code></pre> |
| 48 | +<pre class="sourceCode python"><code class="sourceCode python"> >>> <span class="kw">assert</span> <span class="ot">True</span> == <span class="ot">True</span></code></pre> |
| 49 | +<pre class="output"><code> >>></code></pre> |
| 50 | +<p>That is, assertions halt code execution instantly if the comparison is false. It does nothing at all if the comparison is true. These are therefore a very good tool for guarding the function against foolish (e.g. human) input:</p> |
| 51 | +<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list): |
| 52 | + <span class="kw">assert</span> <span class="dt">len</span>(num_list) != <span class="dv">0</span> |
| 53 | + <span class="kw">return</span> <span class="dt">sum</span>(num_list)/<span class="dt">len</span>(num_list)</code></pre> |
| 54 | +<p>The advantage of assertions is their ease of use. They are rarely more than one line of code. The disadvantage is that assertions halt execution indiscriminately and the helpfulness of the resulting error message is usually quite limited.</p> |
| 55 | +<p>Also, input checking may require decending a rabbit hole of exceptional cases. What happens when the input provided to the mean function is a string, rather than a list of numbers?</p> |
| 56 | +<section class="challenge panel panel-success"> |
| 57 | +<div class="panel-heading"> |
| 58 | +<h2><span class="glyphicon glyphicon-pencil"></span>Challenge Insert an Assertion</h2> |
| 59 | +</div> |
| 60 | +<div class="panel-body"> |
| 61 | +<ol style="list-style-type: decimal"> |
| 62 | +<li>Open an IPython Notebook</li> |
| 63 | +<li>Create the following function:</li> |
| 64 | +</ol> |
| 65 | +<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list): |
| 66 | + <span class="kw">return</span> <span class="dt">sum</span>(num_list)/<span class="dt">len</span>(num_list)</code></pre> |
| 67 | +<ol start="3" style="list-style-type: decimal"> |
| 68 | +<li>In the function, insert an assertion that checks whether the input is actually a list.</li> |
| 69 | +</ol> |
| 70 | +<p>Hint: Use the <a href="https://docs.python.org/2/library/functions.html#isinstance">isinstance function</a>.</p> |
| 71 | +</div> |
| 72 | +</section> |
| 73 | +<p>Assertions are also helpful for catching abnormal behaviors, such as those that arise with floating point arithmetic.</p> |
| 74 | +<section class="challenge panel panel-success"> |
| 75 | +<div class="panel-heading"> |
| 76 | +<h2><span class="glyphicon glyphicon-pencil"></span>Challenge: Almost Equal</h2> |
| 77 | +</div> |
| 78 | +<div class="panel-body"> |
| 79 | +<p>Assertions are also helpful for catching abnormal behaviors, such as those that arise with floating point arithmetic. Using the assert keyword, how could you test whether some value is almost the same as another value?</p> |
| 80 | +<ul> |
| 81 | +<li>My package, mynum, provides the number a.</li> |
| 82 | +<li>Use the <code>assert</code> keyword to check whether the number a is greater than 2.</li> |
| 83 | +<li>Use the <code>assert</code> keyword to check whether a is equal to 2 to within 2 decimal places.</li> |
| 84 | +<li>Use the <code>assert</code> keyword to check that a is equal to 2 within an error of 0.003.</li> |
| 85 | +</ul> |
| 86 | +<pre class="sourceCode python"><code class="sourceCode python"><span class="ch">from</span> mynum <span class="ch">import</span> a |
| 87 | +<span class="co"># greater than 2 assertion here</span> |
| 88 | +<span class="co"># 2 decimal places assertion here</span> |
| 89 | +<span class="co"># 0.003 assertion here</span></code></pre> |
| 90 | +</div> |
| 91 | +</section> |
| 92 | +<p>To help with situations such as those above, there are classes of more helpful assertions that we will use often in later parts of this testing lesson as the building blocks of our tests. The nose testing package contains many of them.</p> |
| 93 | +<h3 id="nose">Nose</h3> |
| 94 | +<p>The nose testing framework has built-in assertion types implementing <code>assert_almost_equal</code>, <code>assert_true</code>, <code>assert_false</code>, <code>assert_raises</code>, <code>assert_is_instance</code>, and others.</p> |
| 95 | +<pre class="sourceCode python"><code class="sourceCode python"><span class="ch">from</span> nose.tools <span class="ch">import</span> assert_almost_equal |
| 96 | +<span class="ch">from</span> mynum <span class="ch">import</span> a |
| 97 | +assert_almost_equal(a, <span class="dv">2</span>, places=<span class="dv">2</span>) |
| 98 | +assert_almost_equal(a, <span class="dv">2</span>, delta=<span class="fl">0.003</span>)</code></pre> |
| 99 | +<p>These assertions give much more helpful error messages and have much more powerful features than the simple assert keyword. An even more powerful sibling of the assertion is the <em>exception</em>. We’ll learn about those in the next lesson.</p> |
| 100 | +<blockquote> |
| 101 | +<h2>Key Points</h2> |
| 102 | +<ul> |
| 103 | +<li>Assertions are one line tests embedded in code.</li> |
| 104 | +<li>The <code>assert</code> keyword is used to set an assertion.</li> |
| 105 | +<li>Assertions halt execution if the argument is false.</li> |
| 106 | +<li>Assertions do nothing if the argument is true.</li> |
| 107 | +<li>The <code>nose.tools</code> package provides more informative assertions.</li> |
| 108 | +<li>Assertions are the building blocks of tests.</li> |
| 109 | +</ul> |
| 110 | +</blockquote> |
| 111 | + </div> |
| 112 | + </div> |
| 113 | + </article> |
| 114 | + <div class="footer"> |
| 115 | + <a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a> |
| 116 | + <a class="label swc-blue-bg" href="https://github.com/swcarpentry/git-novice">Source</a> |
| 117 | + <a class=" label swc-blue-bg" href=" mailto:[email protected]" >Contact </a> |
| 118 | + <a class="label swc-blue-bg" href="LICENSE.html">License</a> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + <!-- Javascript placed at the end of the document so the pages load faster --> |
| 122 | + <script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script> |
| 123 | + <script src="css/bootstrap/bootstrap-js/bootstrap.js"></script> |
| 124 | + </body> |
| 125 | +</html> |
0 commit comments