2015-01-26 22:00:11 +08:00
|
|
|
{
|
|
|
|
"metadata": {
|
|
|
|
"name": "",
|
2015-01-26 22:31:24 +08:00
|
|
|
"signature": "sha256:b98de97a0c9883157d25cd7ed2b6345daca13d7a67bed9ec4a72b8bec85d671b"
|
2015-01-26 22:00:11 +08:00
|
|
|
},
|
|
|
|
"nbformat": 3,
|
|
|
|
"nbformat_minor": 0,
|
|
|
|
"worksheets": [
|
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"# Nose Unit Tests with IPython Notebook"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Install Nose"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"```bash\n",
|
|
|
|
"pip install nose\n",
|
|
|
|
"```"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Create the Code"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"%%file type_util.py\n",
|
|
|
|
"class TypeUtil:\n",
|
|
|
|
"\n",
|
|
|
|
" @classmethod\n",
|
|
|
|
" def is_iterable(cls, obj):\n",
|
|
|
|
" \"\"\"Determines if obj is iterable.\n",
|
|
|
|
"\n",
|
|
|
|
" Useful when writing functions that can accept multiple types of\n",
|
|
|
|
" input (list, tuple, ndarray, iterator). Pairs well with\n",
|
|
|
|
" convert_to_list.\n",
|
|
|
|
" \"\"\"\n",
|
|
|
|
" try:\n",
|
|
|
|
" iter(obj)\n",
|
|
|
|
" return True\n",
|
|
|
|
" except TypeError:\n",
|
|
|
|
" return False\n",
|
|
|
|
"\n",
|
|
|
|
" @classmethod\n",
|
|
|
|
" def convert_to_list(cls, obj):\n",
|
|
|
|
" \"\"\"Converts obj to a list if it is not a list and it is iterable, \n",
|
|
|
|
" else returns the original obj.\n",
|
|
|
|
" \"\"\"\n",
|
|
|
|
" if not isinstance(obj, list) and cls.is_iterable(obj):\n",
|
|
|
|
" obj = list(obj)\n",
|
|
|
|
" return obj\n"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"output_type": "stream",
|
|
|
|
"stream": "stdout",
|
|
|
|
"text": [
|
|
|
|
"Overwriting type_util.py\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"prompt_number": 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Create the Nose Tests"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"%%file tests/test_type_util.py\n",
|
|
|
|
"from nose.tools import assert_equal\n",
|
2015-01-26 22:31:24 +08:00
|
|
|
"from ..type_util import TypeUtil\n",
|
2015-01-26 22:00:11 +08:00
|
|
|
"\n",
|
|
|
|
"\n",
|
|
|
|
"class TestUtil():\n",
|
|
|
|
"\n",
|
|
|
|
" def test_is_iterable(self):\n",
|
|
|
|
" assert_equal(TypeUtil.is_iterable('foo'), True)\n",
|
|
|
|
" assert_equal(TypeUtil.is_iterable(7), False)\n",
|
|
|
|
"\n",
|
|
|
|
" def test_convert_to_list(self):\n",
|
|
|
|
" assert_equal(isinstance(TypeUtil.convert_to_list('foo'), list), True)\n",
|
|
|
|
" assert_equal(isinstance(TypeUtil.convert_to_list(7), list), False)"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"output_type": "stream",
|
|
|
|
"stream": "stdout",
|
|
|
|
"text": [
|
|
|
|
"Overwriting tests/test_type_util.py\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"prompt_number": 2
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "markdown",
|
|
|
|
"metadata": {},
|
|
|
|
"source": [
|
|
|
|
"## Run the Nose Tests"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"collapsed": false,
|
|
|
|
"input": [
|
|
|
|
"!nosetests tests/test_type_util.py"
|
|
|
|
],
|
|
|
|
"language": "python",
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [
|
|
|
|
{
|
|
|
|
"output_type": "stream",
|
|
|
|
"stream": "stdout",
|
|
|
|
"text": [
|
|
|
|
"..\r\n",
|
|
|
|
"----------------------------------------------------------------------\r\n",
|
|
|
|
"Ran 2 tests in 0.001s\r\n",
|
|
|
|
"\r\n",
|
|
|
|
"OK\r\n"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"prompt_number": 3
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"metadata": {}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|