Python Forum

Full Version: Executing single unittest over a sequence
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi folks,
I have the unittest that may run over some sequence, failing occasionally on some elements.

So, instead of writing
class MyTectClass(unittest.TestCase):    .....    def TestOverSequence(self):        for elem in sequence:             <run a bunch of asserts>
I would like to have something like
class MyTectClass(unittest.TestCase):    def __init__(self):        super().__init__()        self.sequence_iter = iter(sequence)    def TestOverElem(self):        elem = next(self.sequence_iter)        <run a bunch of asserts>
Is it doable, and if it is - how?

Thanks in advance
You know what they say about RT(F)M Wall .

After getting a couple of good-intentioned - but misleading - answers on SO, I stumbled upon this useful API - unitteste.subTest()

class MyTectClass(unittest.TestCase): def _some_test(**kwargs): ....... def TestOverSequence(self): for elem in sequence: with self.subTest(elem=elem) self._some_test(elem=elem)
Problem solved!