How to break python test cases with nets

I have several functions on the graph f (), g () and h () that implement different algorithms for the same task. I would like to do unit testing of these functions using the unittest framework.

For each algorithm, several restrictions should always be allowed (for example, an empty graph, a graph with only one node, etc.). Code for checking general restrictions should not be duplicated. So, the test architecture that I started developing was as follows:

class AbstractTest(TestCase):
  def test_empty(self):
      result = self.function(make_empty_graph())
      assertTrue(result....) # etc..
  def test_single_node(self):
      ...

Then specific test cases

class TestF(AbstractTest):
  def setup(self):
      self.function = f
  def test_random(self):
      #specific test for algorithm 'f'

class TestG(AbstractTest):
  def setup(self):
      self.function = g
  def test_complete_graph(self):
      #specific test for algorithm 'g'

... And so on for each algorithm

, nosetests, AbstractTest, , self.function . __test__ = False AbstractTest, ( , ). (abc.ABCMeta) . MixIn - ( ).

, , . Python?

.

+5
1

Nose , unittest.TestCase, :

class AlgoMixin(object):
  # Does not end in "Test"; not a subclass of unittest.TestCase.
  # You may prefer "AbstractBase" or something else.

  def test_empty(self):
    result = self.function(make_empty_graph())
    self.assertTrue(result)

class TestF(AlgoMixin, unittest.TestCase):
  function = staticmethod(f)
  # Doesn't need to be in setup, nor be an instance attribute.
  # But doesn't take either self or class parameter, so use staticmethod.

  def test_random(self):
    pass  # Specific test for algorithm 'f'.
+2

All Articles