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
<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>
<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>
<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
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
56
<sectionclass="challenge panel panel-success">
57
57
<divclass="panel-heading">
58
-
<h2><spanclass="glyphicon glyphicon-pencil"></span>Insert an Assertion</h2>
58
+
<h2id="insert-an-assertion"><spanclass="glyphicon glyphicon-pencil"></span>Insert an Assertion</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>
80
80
<ul>
81
81
<li>My package, mynum, provides the number a.</li>
82
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
83
<li>Use the <code>assert</code> keyword to check that a is equal to 2 within an error of 0.003.</li>
85
84
</ul>
86
-
<preclass="sourceCode python"><codeclass="sourceCode python"><spanclass="ch">from</span> mynum <spanclass="ch">import</span> a
85
+
<divclass="sourceCode"><preclass="sourceCode python"><codeclass="sourceCode python"><spanclass="im">from</span> mynum <spanclass="im">import</span> a
87
86
<spanclass="co"># greater than 2 assertion here</span>
<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
-
<h2id="nose">Nose</h2>
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>
90
+
<h2id="numpy">NumPy</h2>
91
+
<p>The NumPy numerical computing library has a built-in function <code>assert_allclose</code> for comparing numbers within a tolerance:</p>
Copy file name to clipboardExpand all lines: 02-assertions.ipynb
+7-13Lines changed: 7 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -312,13 +312,10 @@
312
312
"cell_type": "markdown",
313
313
"metadata": {},
314
314
"source": [
315
-
"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.\n",
315
+
"### NumPy\n",
316
316
"\n",
317
-
"___\n",
318
-
"\n",
319
-
"### Nose\n",
320
-
"\n",
321
-
"The nose testing framework has built-in assertion types implementing `assert_almost_equal`, `assert_true`, `assert_false`, `assert_raises`, `assert_is_instance`, and others.\n"
317
+
"The NumPy numerical computing library has a built-in function `assert_allclose`\n",
318
+
"for comparing numbers within a tolerance:"
322
319
]
323
320
},
324
321
{
@@ -329,24 +326,21 @@
329
326
},
330
327
"outputs": [],
331
328
"source": [
332
-
"from nose.tools import assert_almost_equal\n",
329
+
"from numpy.testing import assert_allclose\n",
333
330
"from mynum import a\n",
334
-
"assert_almost_equal(a, 2, places=2)\n",
335
-
"assert_almost_equal(a, 2, delta=0.003)"
331
+
"assert_allclose(a, 2, atol=0.003, rtol=0)"
336
332
]
337
333
},
338
334
{
339
335
"cell_type": "markdown",
340
336
"metadata": {},
341
337
"source": [
342
-
"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 exception. We’ll learn about those in the next lesson.\n",
343
-
"\n",
344
338
"## Key Points\n",
345
339
"- Assertions are one line tests embedded in code.\n",
346
340
"- The assert keyword is used to set an assertion.\n",
347
341
"- Assertions halt execution if the argument is false.\n",
348
342
"- Assertions do nothing if the argument is true.\n",
349
-
"- The nose.tools package provides more informative assertions.\n",
343
+
"- The `numpy.testing` module provides tools for numerical testing.\n",
0 commit comments