Skip to content

Commit

Permalink
adds new files, ipython notebooks and support python
Browse files Browse the repository at this point in the history
  • Loading branch information
katyhuff committed Jun 5, 2015
1 parent 7951656 commit 21aaae8
Show file tree
Hide file tree
Showing 7 changed files with 783 additions and 0 deletions.
281 changes: 281 additions & 0 deletions 02-assertions.ipynb
@@ -0,0 +1,281 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Testing\n",
"##Assertions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# when the argument is true, nothing happens\n",
"assert True == True"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"# when the argument is false, an assertion error is raised\n",
"assert True == False"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false,
"scrolled": true
},
"outputs": [],
"source": [
"# an error message can even be added to the assertion\n",
"assert True == False, \"True is not False\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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. A simple mean function below, results in a confusing error if the input is empty.\n",
"\n",
"### Assertions within code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def mean(num_list):\n",
" return sum(num_list)/len(num_list)\n",
"\n",
"mean([])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If we catch the error earlier, then it is more clear that the issue is not division by zero, but will point at the line above, where it is clear that the list is empty."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def mean(num_list):\n",
" assert len(num_list) != 0, \"the input is empty\"\n",
" return sum(num_list)/len(num_list)\n",
"\n",
"mean([])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"mean([\"nonempty\",\"but\",\"not\", \"numbers\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"____\n",
"### Challenge: Insert an Assertion\n",
"In the following code, insert an assertion that checks whether the list is made of numbers."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def mean(num_list):\n",
" assert len(num_list) != 0, \"the input is empty\"\n",
" # insert your assertion here\n",
" return sum(num_list)/len(num_list)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Does it give the right answer?\n",
"mean([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Does this cause it to halt with a type message?\n",
"mean([\"test\"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"# Does this cause it to halt with an emptiness message?\n",
"mean([])"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"----\n",
"### Challenge: Almost Equal\n",
"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?\n",
"\n",
"- My package, mynum, provides the number a. \n",
"- Use the `assert` keyword to check whether the number a is greater than 2.\n",
"- Use the `assert` keyword to check whether a is equal to 2 to within 2 decimal places.\n",
"- Use the `assert` keyword to check that a is equal to 2 within an error of 0.003."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from mynum import a\n",
"# greater than 2 assertion here\n",
"# 2 decimal places assertion here\n",
"# 0.003 assertion here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"\n",
"___\n",
"\n",
"### Nose\n",
"\n",
"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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from nose.tools import assert_almost_equal\n",
"from mynum import a\n",
"assert_almost_equal(a, 2, places=2)\n",
"assert_almost_equal(a, 2, delta=0.003)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"\n",
"## Key Points\n",
"- Assertions are one line tests embedded in code.\n",
"- The assert keyword is used to set an assertion.\n",
"- Assertions halt execution if the argument is false.\n",
"- Assertions do nothing if the argument is true.\n",
"- The nose.tools package provides more informative assertions.\n",
"- Assertions are the building blocks of tests."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.3.5"
}
},
"nbformat": 4,
"nbformat_minor": 0
}

0 comments on commit 21aaae8

Please sign in to comment.