Skip to content

Instantly share code, notes, and snippets.

@bcavagnolo
Last active October 22, 2015 18:47
Show Gist options
  • Save bcavagnolo/eb2c73e8b5264e54bf9f to your computer and use it in GitHub Desktop.
Save bcavagnolo/eb2c73e8b5264e54bf9f to your computer and use it in GitHub Desktop.
seriously skip my unittest class...even the setUpClass method
import unittest
import os
FOO_IS_ON=os.environ.get('FOO')
def seriously_skip_this_if(condition, reason):
'''skip the test class (including the setUpClass method
nose (and friends) will run your setUpClass and tearDownClass methods even
if you skip those tests using the usual decorators. This decorator
seriously skips those parts if the class is marked to skip. This is
described here:
https://github.com/nose-devs/nose/issues/946
'''
def class_maker(cls):
@unittest.skipIf(condition, reason)
class ClassWithoutSetups(cls):
__name__ = cls.__name__
@classmethod
def setUpClass(c):
print 'I overrode the setUpClass!!'
@unittest.skipIf(condition, reason)
class ClassWithSetups(cls):
pass
ClassWithSetups.__name__ = cls.__name__
ClassWithoutSetups.__name__ = cls.__name__
if condition:
return ClassWithoutSetups
else:
return ClassWithSetups
return class_maker
@seriously_skip_this_if(FOO_IS_ON, 'foo is on. Duh.')
class TestMyClass(unittest.TestCase):
@classmethod
def setUpClass(cls):
print 'Doing the actual class setup!'
def test_my_stuff(self):
print 'Did the test!'
@iorbitearth
Copy link

Is it possible to conserve the original class name in the output? For example:

test_my_stuff (foo.TestMyClass) ... Did the test!

vs

test_my_stuff (foo.ClassWithSetups) ... Did the test!

@bcavagnolo
Copy link
Author

Good idea @iorbitearth. Updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment