You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<li>The act of writing tests can help clarify how the software should be perform when you are done.</li>
47
47
<li>In fact, starting to write the tests <em>before</em> you even write the software might be advisable. (Such a practice is called <em>test-driven development</em>, which we will discuss in greater detail <ahref="09-tdd.html">later in the lesson</a>.)</li>
48
48
</ul>
49
-
<blockquote>
50
-
<h3>What Kinds of Tests Exist</h3>
49
+
<asideclass="callout panel panel-info">
50
+
<divclass="panel-heading">
51
+
<h2><spanclass="glyphicon glyphicon-pushpin"></span>What Kinds of Tests Exist</h2>
52
+
</div>
53
+
<divclass="panel-body">
51
54
<p>There are many ways to test software, such as:</p>
52
55
<ul>
53
56
<li>Assertions</li>
@@ -56,22 +59,12 @@ <h3>What Kinds of Tests Exist</h3>
56
59
<li>Regresson Tests</li>
57
60
<li>Integration Tests</li>
58
61
</ul>
59
-
</blockquote>
62
+
</div>
63
+
</aside>
60
64
<p><em>Exceptions and Assertions</em>: While writing code, <code>exceptions</code> and <code>assertions</code> can be added to sound an alarm as runtime problems come up. These kinds of tests, are embedded in the software iteself and handle, as their name implies, exceptional cases rather than the norm.</p>
61
65
<p><em>Unit Tests</em>: Unit tests investigate the behavior of units of code (such as functions, classes, or data structures). By validating each software unit across the valid range of its input and output parameters, tracking down unexpected behavior that may appear when the units are combined is made vastly simpler.</p>
62
66
<p><em>Regression Tests</em>: Regression tests defend against new bugs, or regressions, which might appear due to new software and updates.</p>
63
67
<p><em>Integration Tests</em>: Integration tests check that various pieces of the software work together as expected.</p>
64
-
<blockquote>
65
-
<h2>Key Points</h2>
66
-
<ul>
67
-
<li>Tests compare that the result observed from running code is the same as what was expected ahead of time.</li>
68
-
<li>Tests should be written at the same time as the code they are testing is written.</li>
69
-
<li>Assertions and exceptions are like alarm systems embedded in the software, guarding against exceptional bahavior.</li>
70
-
<li>Unit tests try to test the smallest pieces of code possible, usually functions and methods.</li>
71
-
<li>Integration tests make sure that code units work together properly.</li>
72
-
<li>Regression tests ensure that everything works the same today as it did yesterday.</li>
<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
56
<sectionclass="challenge panel panel-success">
57
57
<divclass="panel-heading">
58
-
<h2><spanclass="glyphicon glyphicon-pencil"></span>Challenge Insert an Assertion</h2>
58
+
<h2><spanclass="glyphicon glyphicon-pencil"></span>Insert an Assertion</h2>
59
59
</div>
60
60
<divclass="panel-body">
61
61
<olstyle="list-style-type: decimal">
@@ -73,7 +73,7 @@ <h2><span class="glyphicon glyphicon-pencil"></span>Challenge Insert an Assertio
73
73
<p>Assertions are also helpful for catching abnormal behaviors, such as those that arise with floating point arithmetic.</p>
74
74
<sectionclass="challenge panel panel-success">
75
75
<divclass="panel-heading">
76
-
<h2><spanclass="glyphicon glyphicon-pencil"></span>Challenge: Almost Equal</h2>
<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>
@@ -90,24 +90,13 @@ <h2><span class="glyphicon glyphicon-pencil"></span>Challenge: Almost Equal</h2>
90
90
</div>
91
91
</section>
92
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
-
<h3id="nose">Nose</h3>
93
+
<h2id="nose">Nose</h2>
94
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>
<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>
<h2><spanclass="glyphicon glyphicon-pencil"></span>Challenge: What Else Can Go Wrong?</h2>
74
+
<h2><spanclass="glyphicon glyphicon-pencil"></span>What Else Can Go Wrong?</h2>
75
75
</div>
76
76
<divclass="panel-body">
77
77
<olstyle="list-style-type: decimal">
@@ -82,7 +82,7 @@ <h2><span class="glyphicon glyphicon-pencil"></span>Challenge: What Else Can Go
82
82
</section>
83
83
<sectionclass="challenge panel panel-success">
84
84
<divclass="panel-heading">
85
-
<h2><spanclass="glyphicon glyphicon-pencil"></span>Challenge: Cause all of the errors</h2>
85
+
<h2><spanclass="glyphicon glyphicon-pencil"></span>Cause All of the Errors</h2>
86
86
</div>
87
87
<divclass="panel-body">
88
88
<ul>
@@ -91,14 +91,6 @@ <h2><span class="glyphicon glyphicon-pencil"></span>Challenge: Cause all of the
91
91
</div>
92
92
</section>
93
93
<p>Exceptions have the advantage of being simple to include and powerfully helpful to the user. However, not all behaviors can or should be found with runtime exceptions. Most behaviors should be validated with unit tests.</p>
94
-
<blockquote>
95
-
<h2>Key Points</h2>
96
-
<ul>
97
-
<li>Exceptions are effectively specialized runtime tests</li>
98
-
<li>Exceptions can be caught and handled with a try-except block</li>
99
-
<li>Many built-in Exception types are available</li>
Copy file name to clipboardExpand all lines: 05-nose.html
-11Lines changed: 0 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -110,17 +110,6 @@ <h2><span class="glyphicon glyphicon-pencil"></span>Fix The Failing Code</h2>
110
110
</div>
111
111
</section>
112
112
<p>As we write more code, we would write more tests, and <em>nosetests</em> would produce more dots. Each passing test is a small, satisfying reward for having written quality scientific software. Now that you know how to write tests, let’s go into what can go wrong.</p>
113
-
<blockquote>
114
-
<h2>Key Points</h2>
115
-
<ul>
116
-
<li>The <code>nosetests</code> command collects and runs tests starting with <code>Test-</code>, <code>test_</code>, and similar names.</li>
117
-
<li><code>.</code> means the test passed</li>
118
-
<li><code>F</code> means the test failed</li>
119
-
<li><code>E</code> encountered an error</li>
120
-
<li><code>K</code> is a known failure</li>
121
-
<li><code>S</code> is a purposefully skipped test</li>
0 commit comments