PyUnit 中是否已弃用测试套件?
按照 PyUnit 中的示例,我想出了以下运行良好的单元测试代码。
import unittest
class Board:
def __init__(self, x, y):
self.x = x; self.y = y;
def __eq__(self, other):
return self.x == other.x and self.y == other.y
class BoardTest(unittest.TestCase):
def setUp(self):
self.b10_10 = Board(10,10)
self.b10_10p = Board(10,10)
self.b10_20 = Board(10,20)
def tearDown(self):
pass
def test1(self):
self.assert_(self.b10_10 == self.b10_10p)
def test2(self):
self.assert_(not (self.b10_10 == self.b10_20))
class BoardTest2(unittest.TestCase):
def setUp(self):
self.b10_10 = Board(10,10)
self.b10_10p = Board(10,10)
self.b10_20 = Board(10,20)
def tearDown(self):
pass
def test1(self):
self.assert_(self.b10_10 == self.b10_10p)
def test2(self):
self.assert_(not (self.b10_10 == self.b10_20))
def suite():
suite1 = unittest.makeSuite(BoardTest)
suite2 = unittest.makeSuite(BoardTest2)
return unittest.TestSuite((suite1, suite2))
if __name__ == "__main__":
unittest.main()
但问题是,即使我删除
def suite():, the result is the same. In other words, it looks like that the fixture/suite is not useless with PyUnit.
这是正确的吗?
Following the example in PyUnit, I came up with the following unittest code that works fine.
import unittest
class Board:
def __init__(self, x, y):
self.x = x; self.y = y;
def __eq__(self, other):
return self.x == other.x and self.y == other.y
class BoardTest(unittest.TestCase):
def setUp(self):
self.b10_10 = Board(10,10)
self.b10_10p = Board(10,10)
self.b10_20 = Board(10,20)
def tearDown(self):
pass
def test1(self):
self.assert_(self.b10_10 == self.b10_10p)
def test2(self):
self.assert_(not (self.b10_10 == self.b10_20))
class BoardTest2(unittest.TestCase):
def setUp(self):
self.b10_10 = Board(10,10)
self.b10_10p = Board(10,10)
self.b10_20 = Board(10,20)
def tearDown(self):
pass
def test1(self):
self.assert_(self.b10_10 == self.b10_10p)
def test2(self):
self.assert_(not (self.b10_10 == self.b10_20))
def suite():
suite1 = unittest.makeSuite(BoardTest)
suite2 = unittest.makeSuite(BoardTest2)
return unittest.TestSuite((suite1, suite2))
if __name__ == "__main__":
unittest.main()
But the thing is that even if I remove the
def suite():
, the result is the same. In other words, it looks like that the fixture/suite is not useless with PyUnit.
Is this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在单个模块中运行所有测试,则不需要unittest.TestSuite,因为unittest.main()将动态检查调用它的模块并查找从unittest.TestCase派生的所有类
但是,TestSuite类仍然是在许多场景中都很方便:
unittest.TestSuite is not necessary if you want to run all the tests in a single module as unittest.main() will dynamically examine the module it is called from and find all classes that derive from unittest.TestCase
However, the TestSuite class is still handy in a number of scenarios:
除了马克的回答之外,构建自己的 suite() 的另一个原因是如果您正在动态构建测试。
另外,我花了一段时间才弄清楚如何让 PyDev 选择该套件并在图形测试运行器中运行它。诀窍是放入这样的方法:
这样的方法会被图形测试运行器拾取。
In addition to Mark's answer, one more reason to build your own suite() is if you are dynamically building tests.
Also, it took me a while to figure out how to get PyDev to pick up the suite and run it in the graphical test runner. The trick is to put in a method like so:
Such a method gets picked up the graphical test runner.