dimanche 8 février 2015

assertRaises in loop: 1 test per iteration

I'm trying to use assertRaises in a loop so I can test multiple errant delimiters ([',', ':', '-']) without having to write a new test for each case. I'm having trouble using assertRaises in a loop. I made a minimum working example of a unittest.TestCase that calls assertRaises in a loop:



import sys
import unittest

def throw_error():
sys.stderr.write('In throw error!')
raise TypeError

class Test(unittest.TestCase):

def test_loop(self):
for i in range(5):
self.assertRaises(TypeError, throw_error)


This works, but it only counts as 1 test, when I'd prefer it to be understood as 5 tests. Is there a canonical way to get this type of behavior?



>>> nosetests ../footest.py
In throw error!
In throw error!
In throw error!
In throw error!
In throw error!
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK


The main reason I want this behavior is because this seems like too much of a black box, and I will eventually forget this is 5 tests in 1. Maybe writing to stderr as I've done with a custom message is good enough, or do you have a better suggestion?


Aucun commentaire:

Enregistrer un commentaire