|
179 | 179 | }, |
180 | 180 | "outputs": [], |
181 | 181 | "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", |
193 | 183 | "\"\"\"An articulated nosetest script using classes\n", |
194 | 184 | "\n", |
195 | 185 | " - setup_class() and setup() increase flexibility\n", |
196 | 186 | "\"\"\"\n", |
197 | 187 | "\n", |
198 | 188 | "import os\n", |
| 189 | + "import errno\n", |
199 | 190 | "\n", |
| 191 | + "# Import assert_* tools\n", |
| 192 | + "from nose.tools import *\n", |
200 | 193 | "\n", |
201 | | - "class TestB:\n", |
| 194 | + "class TestB(object):\n", |
202 | 195 | "\n", |
203 | 196 | " @classmethod\n", |
204 | 197 | " def setup_class(self):\n", |
205 | 198 | " # Run once at startup, eg. create database structure\n", |
206 | | - " print(\"setup testsuite environment\")\n", |
| 199 | + " print(\"\\nsetup testsuite environment\")\n", |
207 | 200 | " open(\"/tmp/test2.out\", \"w\").write(\"0\")\n", |
208 | 201 | "\n", |
209 | 202 | " @classmethod\n", |
210 | 203 | " def teardown_class(self):\n", |
211 | 204 | " # Run once at teardown, eg. drop database\n", |
212 | | - " print(\"cleanup testsuite environment\")\n", |
| 205 | + " print(\"\\ncleanup testsuite environment\")\n", |
213 | 206 | " os.unlink(\"/tmp/test2.out\")\n", |
214 | 207 | "\n", |
215 | 208 | " def setup(self):\n", |
216 | 209 | " # Before every test, like populate a table\n", |
217 | | - " print(\"before_every_test\")\n", |
| 210 | + " print(\"\\nbefore_every_test\")\n", |
218 | 211 | "\n", |
219 | 212 | " def teardown(self):\n", |
220 | 213 | " # After every test, eg truncate a table\n", |
221 | | - " print(\"after_every_test\")\n", |
| 214 | + " print(\"\\nafter_every_test\")\n", |
222 | 215 | "\n", |
223 | 216 | " def test_a(self):\n", |
224 | 217 | " assert os.path.isfile(\"/tmp/test2.out\")\n", |
225 | 218 | "\n", |
226 | 219 | " 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" |
228 | 248 | ] |
229 | 249 | }, |
| 250 | + { |
| 251 | + "cell_type": "code", |
| 252 | + "execution_count": 4, |
| 253 | + "metadata": { |
| 254 | + "collapsed": true |
| 255 | + }, |
| 256 | + "outputs": [], |
| 257 | + "source": [] |
| 258 | + }, |
230 | 259 | { |
231 | 260 | "cell_type": "code", |
232 | 261 | "execution_count": 9, |
|
0 commit comments