Skip to content

Commit 3e23f1b

Browse files
committed
Added IPython Notebook demonstrating nose unit tests.
1 parent 5afd0cd commit 3e23f1b

File tree

2 files changed

+159
-1
lines changed

2 files changed

+159
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ Python data analysis IPython Notebooks (core Python, NumPy, pandas, matplotlib,
66
IPython Notebooks demonstrating core Python functionality geared towards data analysis.
77

88
* [basics](http://nbviewer.ipython.org/github/donnemartin/pydatanotebooks/blob/master/core/basics.ipynb)
9-
* [datetime](http://nbviewer.ipython.org/github/donnemartin/pydatanotebooks/blob/master/core/datetime.ipynb)
109
* [data structures](http://nbviewer.ipython.org/github/donnemartin/pydatanotebooks/blob/master/core/structs.ipynb)
1110
* [data structures utilities](http://nbviewer.ipython.org/github/donnemartin/pydatanotebooks/blob/master/core/structs_utils.ipynb)
11+
* [datetime](http://nbviewer.ipython.org/github/donnemartin/pydatanotebooks/blob/master/core/datetime.ipynb)
12+
* [unit tests](http://nbviewer.ipython.org/github/donnemartin/pydatanotebooks/blob/master/core/unit_tests.ipynb)
1213

1314
## numpy
1415

core/unit_tests.ipynb

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:bdca080b7a3c3e13406cff167e5e8a56e199b53e7c3b965c6583665afd737e4e"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Nose Unit Tests with IPython Notebook"
16+
]
17+
},
18+
{
19+
"cell_type": "markdown",
20+
"metadata": {},
21+
"source": [
22+
"## Install Nose"
23+
]
24+
},
25+
{
26+
"cell_type": "markdown",
27+
"metadata": {},
28+
"source": [
29+
"```bash\n",
30+
"pip install nose\n",
31+
"```"
32+
]
33+
},
34+
{
35+
"cell_type": "markdown",
36+
"metadata": {},
37+
"source": [
38+
"## Create the Code"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"collapsed": false,
44+
"input": [
45+
"%%file type_util.py\n",
46+
"class TypeUtil:\n",
47+
"\n",
48+
" @classmethod\n",
49+
" def is_iterable(cls, obj):\n",
50+
" \"\"\"Determines if obj is iterable.\n",
51+
"\n",
52+
" Useful when writing functions that can accept multiple types of\n",
53+
" input (list, tuple, ndarray, iterator). Pairs well with\n",
54+
" convert_to_list.\n",
55+
" \"\"\"\n",
56+
" try:\n",
57+
" iter(obj)\n",
58+
" return True\n",
59+
" except TypeError:\n",
60+
" return False\n",
61+
"\n",
62+
" @classmethod\n",
63+
" def convert_to_list(cls, obj):\n",
64+
" \"\"\"Converts obj to a list if it is not a list and it is iterable, \n",
65+
" else returns the original obj.\n",
66+
" \"\"\"\n",
67+
" if not isinstance(obj, list) and cls.is_iterable(obj):\n",
68+
" obj = list(obj)\n",
69+
" return obj\n"
70+
],
71+
"language": "python",
72+
"metadata": {},
73+
"outputs": [
74+
{
75+
"output_type": "stream",
76+
"stream": "stdout",
77+
"text": [
78+
"Overwriting type_util.py\n"
79+
]
80+
}
81+
],
82+
"prompt_number": 1
83+
},
84+
{
85+
"cell_type": "markdown",
86+
"metadata": {},
87+
"source": [
88+
"## Create the Nose Tests"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"collapsed": false,
94+
"input": [
95+
"%%file tests/test_type_util.py\n",
96+
"from nose.tools import assert_equal\n",
97+
"from pydatasnippets.core.type_util import TypeUtil\n",
98+
"\n",
99+
"\n",
100+
"class TestUtil():\n",
101+
"\n",
102+
" def test_is_iterable(self):\n",
103+
" assert_equal(TypeUtil.is_iterable('foo'), True)\n",
104+
" assert_equal(TypeUtil.is_iterable(7), False)\n",
105+
"\n",
106+
" def test_convert_to_list(self):\n",
107+
" assert_equal(isinstance(TypeUtil.convert_to_list('foo'), list), True)\n",
108+
" assert_equal(isinstance(TypeUtil.convert_to_list(7), list), False)"
109+
],
110+
"language": "python",
111+
"metadata": {},
112+
"outputs": [
113+
{
114+
"output_type": "stream",
115+
"stream": "stdout",
116+
"text": [
117+
"Overwriting tests/test_type_util.py\n"
118+
]
119+
}
120+
],
121+
"prompt_number": 2
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {},
126+
"source": [
127+
"## Run the Nose Tests"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"collapsed": false,
133+
"input": [
134+
"!nosetests tests/test_type_util.py"
135+
],
136+
"language": "python",
137+
"metadata": {},
138+
"outputs": [
139+
{
140+
"output_type": "stream",
141+
"stream": "stdout",
142+
"text": [
143+
"..\r\n",
144+
"----------------------------------------------------------------------\r\n",
145+
"Ran 2 tests in 0.001s\r\n",
146+
"\r\n",
147+
"OK\r\n"
148+
]
149+
}
150+
],
151+
"prompt_number": 3
152+
}
153+
],
154+
"metadata": {}
155+
}
156+
]
157+
}

0 commit comments

Comments
 (0)