Skip to content

Instantly share code, notes, and snippets.

Created May 6, 2013 03:46
Show Gist options
  • Save anonymous/5523248 to your computer and use it in GitHub Desktop.
Save anonymous/5523248 to your computer and use it in GitHub Desktop.
This is an IPython Notebook demonstrating some concepts about automated testing from the "Getting started with automated testing" at PyCon 2013. The talk can be seen on YouTube.
{
"metadata": {
"name": "Automated Tests"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Code Testing"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"The function you want to maintain with tests."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def divide(a,b): #simple example, e.g. 'hello world'\n",
" return a/b"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Testing Approaches"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Simple Original"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This is based on the original example found in this [Youtube video](https://www.youtube.com/watch?v=ukm64IUANwE) about the \"Getting started with automated testing\" talk given by Carl Meyer at PyCon 2013 in Santa Clara. The video can also be watched at the bottom of this notebook."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def test_unity():\n",
" assert divide(1,1)==1\n",
" \n",
"def test_bad(): #this is just to show a bad test example\n",
" assert divide(1,2)==2\n",
"\n",
"def test_zero_divide(): #this should through an error\n",
" assert divide(1,0)==inf\n",
"\n",
"for func in test_unity, test_bad, test_zero_divide:\n",
" try:\n",
" func()\n",
" except Exception as e:\n",
" print(\"{} FAILED: {}\".format(func.__name__, e))\n",
" else:\n",
" print(\"{} Passed.\".format(func.__name__))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"test_unity Passed.\n",
"test_bad FAILED: \n",
"test_zero_divide FAILED: division by zero\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Possible Notebook Alternative"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The advantage of this version is that is takes up less screen space in the notebook since each additional test takes up only one line."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Each test in the list of tests is a dictionary that contains\n",
"# the test name as a key with the value being a tuple, of a \n",
"# tuple of the function arguments, and the expected result of\n",
"# the function.\n",
"tests = [{'bad test':((1,2),2)},\n",
" {'unity test':((1,1),1)},\n",
" {'Zero divide':((1,0),inf)}]\n",
"for Test in tests:\n",
" Name = list(Test.keys())[0]\n",
" arg ,result = Test[Name]\n",
" try:\n",
" assert divide(*arg)==result #the test is implemented here\n",
" except Exception as e:\n",
" print(\"{} - FAILED: {}\".format(Name, e))\n",
" else:\n",
" print(\"{} - Passed.\".format(Name))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"bad test - FAILED: \n",
"unity test - Passed.\n",
"Zero divide - FAILED: division by zero\n"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"from IPython.display import YouTubeVideo\n",
"YouTubeVideo('ukm64IUANwE')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"\n",
" <iframe\n",
" width=\"400\"\n",
" height=\"300\"\n",
" src=\"http://www.youtube.com/embed/ukm64IUANwE\"\n",
" frameborder=\"0\"\n",
" allowfullscreen\n",
" ></iframe>\n",
" "
],
"output_type": "pyout",
"prompt_number": 4,
"text": [
"<IPython.lib.display.YouTubeVideo at 0x3d83310>"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 4
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment