Skip to content

Commit fe946f3

Browse files
committed
logging section
1 parent 880d1ac commit fe946f3

4 files changed

Lines changed: 746 additions & 54 deletions

File tree

python-for-sysadmin/notebooks/02_nosetests_simple.ipynb

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,54 +179,83 @@
179179
},
180180
"outputs": [],
181181
"source": [
182-
"%load 02_nosetests_full.py"
183-
]
184-
},
185-
{
186-
"cell_type": "code",
187-
"execution_count": 4,
188-
"metadata": {
189-
"collapsed": true
190-
},
191-
"outputs": [],
192-
"source": [
182+
"# %load 02_nosetests_full.py\n",
193183
"\"\"\"An articulated nosetest script using classes\n",
194184
"\n",
195185
" - setup_class() and setup() increase flexibility\n",
196186
"\"\"\"\n",
197187
"\n",
198188
"import os\n",
189+
"import errno\n",
199190
"\n",
191+
"# Import assert_* tools\n",
192+
"from nose.tools import *\n",
200193
"\n",
201-
"class TestB:\n",
194+
"class TestB(object):\n",
202195
"\n",
203196
" @classmethod\n",
204197
" def setup_class(self):\n",
205198
" # Run once at startup, eg. create database structure\n",
206-
" print(\"setup testsuite environment\")\n",
199+
" print(\"\\nsetup testsuite environment\")\n",
207200
" open(\"/tmp/test2.out\", \"w\").write(\"0\")\n",
208201
"\n",
209202
" @classmethod\n",
210203
" def teardown_class(self):\n",
211204
" # Run once at teardown, eg. drop database\n",
212-
" print(\"cleanup testsuite environment\")\n",
205+
" print(\"\\ncleanup testsuite environment\")\n",
213206
" os.unlink(\"/tmp/test2.out\")\n",
214207
"\n",
215208
" def setup(self):\n",
216209
" # Before every test, like populate a table\n",
217-
" print(\"before_every_test\")\n",
210+
" print(\"\\nbefore_every_test\")\n",
218211
"\n",
219212
" def teardown(self):\n",
220213
" # After every test, eg truncate a table\n",
221-
" print(\"after_every_test\")\n",
214+
" print(\"\\nafter_every_test\")\n",
222215
"\n",
223216
" def test_a(self):\n",
224217
" assert os.path.isfile(\"/tmp/test2.out\")\n",
225218
"\n",
226219
" def test_b(self):\n",
227-
" assert False"
220+
" assert False\n",
221+
"\n",
222+
" #\n",
223+
" # Bonus examples:\n",
224+
" #\n",
225+
" # - testing exceptions\n",
226+
" # - parametrized tests \n",
227+
" #\n",
228+
" @raises(IOError)\n",
229+
" def test_except(self):\n",
230+
" assert open('/tmp/MISSING')\n",
231+
"\n",
232+
" def test_except_complex(self):\n",
233+
" with assert_raises(IOError) as ex:\n",
234+
" fh = open('/tmp/MISSING')\n",
235+
" # Verify exception *and* errno!\n",
236+
" assert_equals(ex.exception.errno, errno.ENOENT)\n",
237+
"\n",
238+
" def test_parametrized(self):\n",
239+
" # Runs 3 tests using the nosetest generator syntax.\n",
240+
" for i in range(3):\n",
241+
" # You have to yield a method to make it work into \n",
242+
" # a class\n",
243+
" yield self.harn_greater, i, 0 \n",
244+
"\n",
245+
" def harn_greater(self, a, b):\n",
246+
" assert_greater(a, b)\n",
247+
"\n"
228248
]
229249
},
250+
{
251+
"cell_type": "code",
252+
"execution_count": 4,
253+
"metadata": {
254+
"collapsed": true
255+
},
256+
"outputs": [],
257+
"source": []
258+
},
230259
{
231260
"cell_type": "code",
232261
"execution_count": 9,

0 commit comments

Comments
 (0)