Python Forum
Executing single unittest over a sequence - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Executing single unittest over a sequence (/thread-3284.html)



Executing single unittest over a sequence - volcano63 - May-11-2017

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


RE: Executing single unittest over a sequence - volcano63 - May-11-2017

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!