views:

34

answers:

1

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?

+1  A: 

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:

  1. 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.
  2. 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.
Mark Roddy