import unittest


class DataDrivenTestCase(unittest.TestCase):
    cases = []

    @classmethod
    def generateCases(cls):
        return cls.cases

    @classmethod
    def loadTests(cls):
        tests = []
        for case in cls.generateCases():
            if isinstance(case, tuple):
                test = cls(*case)
            elif isinstance(case, dict):
                test = cls(**case)
            else:
                test = cls(case)
            tests.append(test)
        return tests

    def __init__(self, description):
        super(DataDrivenTestCase, self).__init__(self, 'runOneTest')
        self.description = description

    def shortDescription(self):
        return '%s for %s' % (self.__class__.__name__, self.description)


def loadTests(module_name):
    loader = unittest.defaultTestLoader
    this_module = __import__(module_name, {}, {}, [None])

    tests = []
    for name in dir(this_module):
        obj = getattr(this_module, name)
        if isinstance(obj, unittest.TestCase):
            if hasattr(obj, 'loadTests'):
                tests.extend(obj.loadTests())
            else:
                tests.append(loader.loadTestsFromTestCase(obj))

    return unittest.TestSuite(tests)

import unittest


class DataDrivenTestCase(unittest.TestCase):
    cases = []

    @classmethod
    def generateCases(cls):
        return cls.cases

    @classmethod
    def loadTests(cls):
        tests = []
        for case in cls.generateCases():
            if isinstance(case, tuple):
                test = cls(*case)
            elif isinstance(case, dict):
                test = cls(**case)
            else:
                test = cls(case)
            tests.append(test)
        return tests

    def __init__(self, description):
        super(DataDrivenTestCase, self).__init__(self, 'runOneTest')
        self.description = description

    def shortDescription(self):
        return '%s for %s' % (self.__class__.__name__, self.description)


def loadTests(module_name):
    loader = unittest.defaultTestLoader
    tests = loader.loadTestsFromName(module_name)
    if not tests:
        raise AssertionError("No tests for {0}".format(module_name))
    return unittest.TestSuite(tests)

Replace ad-hoc pain with builtin methods
Replace ad-hoc pain with builtin methods

  import unittest
  
  
  class DataDrivenTestCase(unittest.TestCase):
      cases = []
  
      @classmethod
      def generateCases(cls):
          return cls.cases
  
      @classmethod
      def loadTests(cls):
          tests = []
          for case in cls.generateCases():
              if isinstance(case, tuple):
                  test = cls(*case)
              elif isinstance(case, dict):
                  test = cls(**case)
              else:
                  test = cls(case)
              tests.append(test)
          return tests
  
      def __init__(self, description):
          super(DataDrivenTestCase, self).__init__(self, 'runOneTest')
          self.description = description
  
      def shortDescription(self):
          return '%s for %s' % (self.__class__.__name__, self.description)
  
  
  def loadTests(module_name):
      loader = unittest.defaultTestLoader
+     tests = loader.loadTestsFromName(module_name)
+     if not tests:
+         raise AssertionError("No tests for {0}".format(module_name))
-     this_module = __import__(module_name, {}, {}, [None])
- 
-     tests = []
-     for name in dir(this_module):
-         obj = getattr(this_module, name)
-         if isinstance(obj, unittest.TestCase):
-             if hasattr(obj, 'loadTests'):
-                 tests.extend(obj.loadTests())
-             else:
-                 tests.append(loader.loadTestsFromTestCase(obj))
- 
      return unittest.TestSuite(tests)
  
Replace ad-hoc pain with builtin methods
## Code Before:
import unittest


class DataDrivenTestCase(unittest.TestCase):
    cases = []

    @classmethod
    def generateCases(cls):
        return cls.cases

    @classmethod
    def loadTests(cls):
        tests = []
        for case in cls.generateCases():
            if isinstance(case, tuple):
                test = cls(*case)
            elif isinstance(case, dict):
                test = cls(**case)
            else:
                test = cls(case)
            tests.append(test)
        return tests

    def __init__(self, description):
        super(DataDrivenTestCase, self).__init__(self, 'runOneTest')
        self.description = description

    def shortDescription(self):
        return '%s for %s' % (self.__class__.__name__, self.description)


def loadTests(module_name):
    loader = unittest.defaultTestLoader
    this_module = __import__(module_name, {}, {}, [None])

    tests = []
    for name in dir(this_module):
        obj = getattr(this_module, name)
        if isinstance(obj, unittest.TestCase):
            if hasattr(obj, 'loadTests'):
                tests.extend(obj.loadTests())
            else:
                tests.append(loader.loadTestsFromTestCase(obj))

    return unittest.TestSuite(tests)

## Instruction:
Replace ad-hoc pain with builtin methods
## Code After:
import unittest


class DataDrivenTestCase(unittest.TestCase):
    cases = []

    @classmethod
    def generateCases(cls):
        return cls.cases

    @classmethod
    def loadTests(cls):
        tests = []
        for case in cls.generateCases():
            if isinstance(case, tuple):
                test = cls(*case)
            elif isinstance(case, dict):
                test = cls(**case)
            else:
                test = cls(case)
            tests.append(test)
        return tests

    def __init__(self, description):
        super(DataDrivenTestCase, self).__init__(self, 'runOneTest')
        self.description = description

    def shortDescription(self):
        return '%s for %s' % (self.__class__.__name__, self.description)


def loadTests(module_name):
    loader = unittest.defaultTestLoader
    tests = loader.loadTestsFromName(module_name)
    if not tests:
        raise AssertionError("No tests for {0}".format(module_name))
    return unittest.TestSuite(tests)
