在python中对序列执行单个单元测试 [英] Executing single unittest over a sequence in python
问题描述
我有一个单元测试,它可能会在某个序列上运行,我会在其中检查序列上某些处理的结果.我不在乎是否有个别的失败——只要我能确定处理没有产生好的结果的序列元素.所以,而不是写
I have the unittest that may run over some sequence, where I check results of some processing over the sequence. I don't care if there are individual failures - as long as I can pinpoint on which element of sequence the processing did not yield good result. So, instead of writing
class MyTectClass(unittest.TestCase):
.....
def TestOverSequence(self):
for elem in sequence:
<run a bunch of asserts>
我想要类似的东西
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?
提前致谢
PS 我可能会发出警告 - 但我宁愿测试用例失败.
PS I may issue warnings - but I would rather have failed test cases.
推荐答案
感谢所有试图提供帮助的人,但再次查看文档后,我找到了我需要的东西 - unittest.subTest() 上下文管理器.
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.
我应该首先看到它
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)
谢谢你看我的肩膀 LOL(你知道,有时你需要有人看你的肩膀才能找到答案)
Thanks for looking over my shoulder LOL (you know, sometimes to find the answer you need someone to look over your shoulder)
这篇关于在python中对序列执行单个单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!