Skip to content

Commit 045e705

Browse files
committed
Importing original material
2 parents c895bb6 + 4f7114a commit 045e705

File tree

116 files changed

+42498
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+42498
-2
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
README.html
2+
LAYOUT.html
3+
FAQ.html
4+
DESIGN.html
5+
CONTRIBUTING.html
6+
CONDUCT.html
7+
*~
8+
*.pyc
9+
_site
10+
.DS_Store

01-basics.html

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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">Basics of testing</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>Understand the place of testing in a scientific workflow.</li>
38+
<li>Understand that testing has many forms.</li>
39+
</ul>
40+
</div>
41+
</section>
42+
<p>The first step toward getting the right answers from our programs is to assume that mistakes <em>will</em> happen and to guard against them. This is called <strong>defensive programming</strong> and the most common way to do it is to add alarms and tests into our code so that it checks itself.</p>
43+
<p><strong>Testing</strong> should be a seamless part of scientific software development process. This is analogous to experiment design in the experimental science world:</p>
44+
<ul>
45+
<li>At the beginning of a new project, tests can be used to help guide the overall architecture of the project.</li>
46+
<li>The act of writing tests can help clarify how the software should be perform when you are done.</li>
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 <a href="09-tdd.html">later in the lesson</a>.)</li>
48+
</ul>
49+
<blockquote>
50+
<h3>What Kinds of Tests Exist</h3>
51+
<p>There are many ways to test software, such as:</p>
52+
<ul>
53+
<li>Assertions</li>
54+
<li>Exceptions</li>
55+
<li>Unit Tests</li>
56+
<li>Regresson Tests</li>
57+
<li>Integration Tests</li>
58+
</ul>
59+
</blockquote>
60+
<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+
<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+
<p><em>Regression Tests</em>: Regression tests defend against new bugs, or regressions, which might appear due to new software and updates.</p>
63+
<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>
73+
</ul>
74+
</blockquote>
75+
</div>
76+
</div>
77+
</article>
78+
<div class="footer">
79+
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
80+
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/git-novice">Source</a>
81+
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
82+
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
83+
</div>
84+
</div>
85+
<!-- Javascript placed at the end of the document so the pages load faster -->
86+
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
87+
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
88+
</body>
89+
</html>

01-basics.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
layout: page
3+
title: Testing
4+
subtitle: Basics of testing
5+
minutes: 5
6+
---
7+
> ## Learning Objectives {.objectives}
8+
>
9+
> * Understand the place of testing in a scientific workflow.
10+
> * Understand that testing has many forms.
11+
12+
The first step toward getting the right answers from our programs is to assume
13+
that mistakes *will* happen and to guard against them. This is called
14+
**defensive programming** and the most common way to do it is to add alarms and
15+
tests into our code so that it checks itself.
16+
17+
**Testing** should be a seamless part of scientific software development process.
18+
This is analogous to experiment design in the experimental science world:
19+
20+
- At the beginning of a new project, tests can be used to help guide the
21+
overall architecture of the project.
22+
- The act of writing tests can help clarify how the software should be perform when you are done.
23+
- In fact, starting to write the tests _before_ you even write the software
24+
might be advisable. (Such a practice is called _test-driven development_,
25+
which we will discuss in greater detail [later in the lesson](09-tdd.html).)
26+
27+
> ### What Kinds of Tests Exist
28+
> There are many ways to test software, such as:
29+
>
30+
> - Assertions
31+
> - Exceptions
32+
> - Unit Tests
33+
> - Regresson Tests
34+
> - Integration Tests
35+
36+
*Exceptions and Assertions*: While writing code, `exceptions` and `assertions`
37+
can be added to sound an alarm as runtime problems come up. These kinds of
38+
tests, are embedded in the software iteself and handle, as their name implies,
39+
exceptional cases rather than the norm.
40+
41+
*Unit Tests*: Unit tests investigate the behavior of units of code (such as
42+
functions, classes, or data structures). By validating each software unit
43+
across the valid range of its input and output parameters, tracking down
44+
unexpected behavior that may appear when the units are combined is made vastly
45+
simpler.
46+
47+
*Regression Tests*: Regression tests defend against new bugs, or regressions,
48+
which might appear due to new software and updates.
49+
50+
*Integration Tests*: Integration tests check that various pieces of the
51+
software work together as expected.
52+
53+
54+
> ## Key Points {.keypoints}
55+
>
56+
> - Tests compare that the result observed from running code is the same as what was expected ahead of time.
57+
> - Tests should be written at the same time as the code they are testing is written.
58+
> - Assertions and exceptions are like alarm systems embedded in the software, guarding against exceptional bahavior.
59+
> - Unit tests try to test the smallest pieces of code possible, usually functions and methods.
60+
> - Integration tests make sure that code units work together properly.
61+
> - Regression tests ensure that everything works the same today as it did yesterday.
62+

02-assertions.html

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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">&gt;&gt;&gt; <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 &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
47+
AssertionError</code></pre>
48+
<pre class="sourceCode python"><code class="sourceCode python"> &gt;&gt;&gt; <span class="kw">assert</span> <span class="ot">True</span> == <span class="ot">True</span></code></pre>
49+
<pre class="output"><code> &gt;&gt;&gt;</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

Comments
 (0)