PyUnit 中是否弃用了测试套件? [英] Is test suite deprecated in PyUnit?
问题描述
按照 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():
,结果也是一样的.换句话说,看起来夹具/套件对 PyUnit 来说不是无用的.
, the result is the same. In other words, it looks like that the fixture/suite is not useless with PyUnit.
这是正确的吗?
推荐答案
unittest.TestSuite 如果您想在单个模块中运行所有测试,则不需要,因为 unittest.main() 将动态检查从中调用它的模块并找到所有派生自 unittest.TestCase 的类
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
然而,TestSuite 类在许多情况下仍然很方便:
However, the TestSuite class is still handy in a number of scenarios:
- 您想要构建一组逻辑测试分组.例如,一套单元测试、集成测试、特定子系统的测试等.
- 您的测试跨越多个模块/包.在这种情况下,让您可以运行的单个脚本执行所有测试很有用.这可以通过构建一套所有测试来实现.请注意,这与诸如 discovery 之类的库无关.
- You want to build a set of logical groupings of tests. For instance, a suite of unit tests, integration tests, tests for a specific subsystem, etc.
- You tests span multiple modules/packages. In this scenario, it is useful to have a single script you can run execute all your tests. This can be accomplished by building up a suite of all your tests. Note that this becomes irrelevant with libraries such as discovery.
这篇关于PyUnit 中是否弃用了测试套件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!