Last active
October 22, 2015 18:47
-
-
Save bcavagnolo/eb2c73e8b5264e54bf9f to your computer and use it in GitHub Desktop.
seriously skip my unittest class...even the setUpClass method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!' |
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!
Good idea @iorbitearth. Updated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output: