Executing Single Unittest Over A Sequence In Python
Solution 1:
That, what you are talking about, is called parametric testing. You add some annotational parameters to your tests, and test system repeats test with accordance to your parameters. In your example it would be a list of values, and test system would repeat test for each of them.
Looks like python still has not such feature in it's testing system: https://bugs.python.org/issue7897
But I found some self help solution here https://gist.github.com/mfazekas/1710455
I also found that separate testing framework pytest has some support for parametric testing https://docs.pytest.org/en/latest/example/parametrize.html
Solution 2:
If you want to assert separately, then why use a loop in first place?
So either mention all asserts
separately or try to add the message to assert statements to identify which one has failed.
# ...deftestOverSequence(self):
for elem in sequence:
self.assertEqual(elem, 3, "{} is not equal to 3".format(elem)) # for instance# Something on these lines
Solution 3:
Thanks to all who've tried to help, but after looking at the documentation again, I've found exactly what I need - unittest.subTest() context manager.
I should have seen it in the first place
classMyTectClass(unittest.TestCase):
def_some_test(**kwargs):
.......
defTestOverSequence(self):
for elem in sequence:
with self.subTest(elem=elem)
self._some_test(elem=elem)
Thanks for looking over my shoulder LOL (you know, sometimes to find the answer you need someone to look over your shoulder)
Post a Comment for "Executing Single Unittest Over A Sequence In Python"