Putting test functions in their own test module too.
authorMatt Davis <jiffyclub@gmail.com>
Sat, 11 May 2013 05:22:58 +0000 (22:22 -0700)
committerW. Trevor King <wking@tremily.us>
Sat, 9 Nov 2013 17:58:12 +0000 (09:58 -0800)
python/sw_engineering/mean_animals.ipynb
python/sw_engineering/test_meananimals.py [new file with mode: 0644]

index 2cbbae4597c5e58514827dbe78f113e5d7b89038..762c9d39a8cc51c0df3c84187d38cc6a1730f875 100644 (file)
      ],
      "prompt_number": 1
     },
+    {
+     "cell_type": "code",
+     "collapsed": false,
+     "input": [
+      "%%file test_meananimals.py\n",
+      "import nose.tools as nt\n",
+      "from meananimals import read_file, calc_mean, filter_animals, mean_animals\n",
+      "\n",
+      "def test_read_animals():\n",
+      "    date, time, animal, count = read_file('animals.txt')\n",
+      "    ref_date = ['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23']\n",
+      "    ref_time = ['21:06', '14:12', '10:24', '20:08', '18:46']\n",
+      "    ref_animal = ['Grizzly', 'Elk', 'Elk', 'Wolverine', 'Muskox']\n",
+      "    ref_count = [36, 25, 26, 31, 20]\n",
+      "    \n",
+      "    assert date == ref_date, 'Dates do not match!'\n",
+      "    assert time == ref_time, 'Times do not match!'\n",
+      "    assert animal == ref_animal, 'Animals do not match!'\n",
+      "    assert count == ref_count, 'Counts do not match!'\n",
+      "\n",
+      "def test_mean1():\n",
+      "    m = calc_mean([1, 2, 3])\n",
+      "    assert m == 2\n",
+      "    \n",
+      "def test_mean2():\n",
+      "    m = calc_mean([1])\n",
+      "    assert m == 1\n",
+      "\n",
+      "def test_mean3():\n",
+      "    m = calc_mean([3.4, 3.5, 3.6])\n",
+      "    assert m == 3.5\n",
+      "\n",
+      "@nt.raises(ValueError)\n",
+      "def test_mean4():\n",
+      "    m = calc_mean([])\n",
+      "\n",
+      "def test_filter_animals1():\n",
+      "    date, time, animal, count = read_file('animals.txt')\n",
+      "    fdate, ftime, fanimal, fcount = filter_animals('Elk', date, time, animal, count)\n",
+      "    \n",
+      "    assert fdate == ['2011-04-23', '2011-04-23']\n",
+      "    assert ftime == ['14:12', '10:24']\n",
+      "    assert fanimal == ['Elk', 'Elk']\n",
+      "    assert fcount == [25, 26]\n",
+      "\n",
+      "def test_mean_animals1():\n",
+      "    m = mean_animals('animals.txt', 'Elk')\n",
+      "    assert m == 25.5\n",
+      "\n",
+      "def test_mean_animals2():\n",
+      "    m = mean_animals('animals.txt', 'Grizzly')\n",
+      "    assert m == 36"
+     ],
+     "language": "python",
+     "metadata": {},
+     "outputs": [
+      {
+       "output_type": "stream",
+       "stream": "stdout",
+       "text": [
+        "Overwriting test_meananimals.py\n"
+       ]
+      }
+     ],
+     "prompt_number": 1
+    },
     {
      "cell_type": "code",
      "collapsed": false,
diff --git a/python/sw_engineering/test_meananimals.py b/python/sw_engineering/test_meananimals.py
new file mode 100644 (file)
index 0000000..7e6fac8
--- /dev/null
@@ -0,0 +1,47 @@
+import nose.tools as nt
+from meananimals import read_file, calc_mean, filter_animals, mean_animals
+
+def test_read_animals():
+    date, time, animal, count = read_file('animals.txt')
+    ref_date = ['2011-04-22', '2011-04-23', '2011-04-23', '2011-04-23', '2011-04-23']
+    ref_time = ['21:06', '14:12', '10:24', '20:08', '18:46']
+    ref_animal = ['Grizzly', 'Elk', 'Elk', 'Wolverine', 'Muskox']
+    ref_count = [36, 25, 26, 31, 20]
+    
+    assert date == ref_date, 'Dates do not match!'
+    assert time == ref_time, 'Times do not match!'
+    assert animal == ref_animal, 'Animals do not match!'
+    assert count == ref_count, 'Counts do not match!'
+
+def test_mean1():
+    m = calc_mean([1, 2, 3])
+    assert m == 2
+    
+def test_mean2():
+    m = calc_mean([1])
+    assert m == 1
+
+def test_mean3():
+    m = calc_mean([3.4, 3.5, 3.6])
+    assert m == 3.5
+
+@nt.raises(ValueError)
+def test_mean4():
+    m = calc_mean([])
+
+def test_filter_animals1():
+    date, time, animal, count = read_file('animals.txt')
+    fdate, ftime, fanimal, fcount = filter_animals('Elk', date, time, animal, count)
+    
+    assert fdate == ['2011-04-23', '2011-04-23']
+    assert ftime == ['14:12', '10:24']
+    assert fanimal == ['Elk', 'Elk']
+    assert fcount == [25, 26]
+
+def test_mean_animals1():
+    m = mean_animals('animals.txt', 'Elk')
+    assert m == 25.5
+
+def test_mean_animals2():
+    m = mean_animals('animals.txt', 'Grizzly')
+    assert m == 36
\ No newline at end of file