如何一次性运行多个实现的 python 单元测试
我无法弄清楚使用 python 单元测试框架的正确方法
我目前有 3 种不同的数据结构类和单元测试实现来测试类中的各种内容,如下所示:
import fooHorse
import fooSnake
import fooMoose
import unittest
foo = fooHorse.foo()
#foo = fooSnake.foo()
#foo = fooMoose.foo()
class testFoo(unittest.TestCase):
def testSomething(self):
foo.do_something()
...
def testSomethingelse(self):
...
# etc
if __name__ == "__main__":
unittest.main()
如何重构代码以便所有测试都可以运行 fooSnake.foo
、fooMoose.foo
和 fooHorse.foo
?
I'm having trouble figuring out the proper way of using pythons unittest framework
I currently have 3 different implementations for a data structure class and unittests to test various things in the class as follows:
import fooHorse
import fooSnake
import fooMoose
import unittest
foo = fooHorse.foo()
#foo = fooSnake.foo()
#foo = fooMoose.foo()
class testFoo(unittest.TestCase):
def testSomething(self):
foo.do_something()
...
def testSomethingelse(self):
...
# etc
if __name__ == "__main__":
unittest.main()
How do I refactor the code so that all the tests are run for fooSnake.foo
, fooMoose.foo
and fooHorse.foo
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将所有测试分解为一个函数,然后从三个测试中调用它:
我不会尝试做任何更聪明的事情,以便测试逻辑保持足够简单,从而明显没有错误。
Just factor all the testing into a function and call it from the three tests :
I wouldn't try to do anything more clever so that the test logic stays simple enought to be obviously bug-free.
我认为最好将测试代码保留在被测试的类中(因此,在
fooHorse
类等内部)。我无法从代码段中真正看出,所以也许您已经在这样做了。然后,要合并多个同时运行的测试,或者选择其中的一些测试,您可以创建一个单独的测试类来创建一个测试套件。在那里,可以分配单独的单元测试。有关详细信息,请参阅 python 文档。I think it is better to keep the testing code inside the classes under test (so, inside the
fooHorse
classes and such). I can't really tell from the code segment, so maybe you are already doing that. Then to combine multiple tests to be run at the same time, or selecting a few of them, you could create a separate testing class that creates a test suite. In there, the individual unittests can be assigned. See the python docs for details on this.