Python Forum
Executing single unittest over a sequence
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Executing single unittest over a sequence
#1
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#2
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!
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using unittest akbarza 2 305 Feb-25-2024, 12:51 PM
Last Post: deanhystad
Question Unwanted execution of unittest ThomasFab 9 2,051 Nov-15-2022, 05:33 PM
Last Post: snippsat
  unittest.mock for an api key silver 3 1,383 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  Ran 0 tests in 0.000s - unittest Peaches 8 5,075 Dec-31-2021, 08:58 AM
Last Post: Peaches
Sad Problem with Unittest mhanusek 1 3,755 Nov-12-2020, 04:58 PM
Last Post: Gribouillis
  Unittest et patch mad31 2 2,129 Aug-09-2020, 06:16 AM
Last Post: mad31
  Unusual things to do with unittest/HTMLTestRunner AndyHolyer 0 2,141 Jul-29-2020, 02:43 PM
Last Post: AndyHolyer
  Test a class function via "unittest " Penguin827 1 1,610 Jul-10-2020, 08:31 AM
Last Post: Gribouillis
  How to use unittest module ? binhduonggttn 1 2,035 Feb-17-2020, 03:28 AM
Last Post: Larz60+
  unittest mock and patch piscvau 1 2,099 Nov-08-2019, 03:23 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020