Is test suite deprecated in 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?
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:
- 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.
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:
def load_tests(loader, tests, pattern):
return suite()
Such a method gets picked up the graphical test runner.
精彩评论