title: Skipping all unit tests but one in Python by using decorators and metaclasses
body: <p>I am writing unit tests for an MCU that communicates commands through the USB port and checks their response.
  If one unit test fails it makes sense for me to do some debugging in the MCU.
  Therefore I would like to disable all unittests except for the one that I would like to debug on the MCU side because if I set a breakpoint somewhere it might get triggered by another unittest with different commands.</p>
  
  <p>I went to the python docs and found this code which is a decorator that will skip all unittests that don't have an attribute.</p>
  
  <pre><code>def skipUnlessHasattr(obj, attr):
      if hasattr(obj, attr):
          return lambda func: func
  return unittest.skip("{!r} doesn't have {!r}".format(obj, attr))
  </code></pre>
  
  <p>In order to make it more simple I deleted the attr argument and statically changed it to 'StepDebug' which is an attribute that I want to set in only one unittest in order to debug it.</p>
  
  <p>So the next step is for me to apply this for all my class methods automatically.
  After reading around on the webs I found the following code that uses a metaclass in order to decorate all methods with the above decorator.
  <a href="https://stackoverflow.com/a/6308016/3257551">https://stackoverflow.com/a/6308016/3257551</a></p>
  
  <pre><code>def decorating_meta(decorator):
  class DecoratingMetaclass(type):
      def __new__(self, class_name, bases, namespace):
          for key, value in list(namespace.items()):
              if callable(value):
                  namespace[key] = decorator(value)
          return type.__new__(self, class_name, bases, namespace)
  
  return DecoratingMetaclass
  </code></pre>
  
  <p>So my minimum working example is</p>
  
  <pre><code>import unittest
  
  def decorating_meta(decorator):
      class DecoratingMetaclass(type):
          def __new__(self, class_name, bases, namespace):
              for key, value in list(namespace.items()):
                  if callable(value):
                      namespace[key] = decorator(value)
              return type.__new__(self, class_name, bases, namespace)
  
      return DecoratingMetaclass
  
  def skipUnlessHasattr(obj):
      if hasattr(obj, 'StepDebug'):
          return lambda func : func
      return unittest.skip("{!r} doesn't have {!r}".format(obj, 'StepDebug'))
  
  class Foo(unittest.TestCase):
      __metaclass__ = decorating_meta(skipUnlessHasattr)
      def test_Sth(self):
          self.assertTrue(False)
  
  if __name__ == '__main__':
      unittest.main()
  </code></pre>
  
  <p>and the error that I get is:</p>
  
  <pre><code>AttributeError: 'Foo' object has no attribute '__name__'
  </code></pre>
  
  <p>From what I read this is something that happens when instead of a class you have an instance but I don't quite understand how I can use this information to solve my problem.</p>
  
  <p>Can someone please help?</p>