Decorator which skips all tests except the one decorated with it. Based on original proposal on python-ideas: http://mail.python.org/pipermail/python-ideas/2010-August/007992.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | def skip_other_tests():
"""
Decorator which skips all tests except the one decorated with it.
Based on original python-ideas proposal:
http://mail.python.org/pipermail/python-ideas/2010-August/007992.html
Working with Python from 2.5 to 3.3.
Author: Giampaolo Rodola' <g.rodola [AT] gmail [DOT] com>
License: MIT
"""
import unittest
from unittest import TextTestRunner as _TextTestRunner
class CustomTestRunner(_TextTestRunner):
def run(self, test):
if test._tests:
for t1 in test._tests:
for t2 in t1._tests:
if t2._testMethodName == self._special_name:
return _TextTestRunner.run(self, t2)
raise RuntimeError("couldn't isolate test")
def outer(fun, *args, **kwargs):
# monkey patch unittest module
unittest.TextTestRunner = CustomTestRunner
if hasattr(unittest, 'runner'): # unittest2
unittest.runner.TextTestRunner = CustomTestRunner
CustomTestRunner._special_name = fun.__name__
def inner(self):
return fun(self, *args, **kwargs)
return inner
return outer
# ===================================================================
# --- test
# ===================================================================
if __name__ == '__main__':
import unittest
flag = []
class TestCase1(unittest.TestCase):
@skip_other_tests()
def test_a(self):
flag.append(None)
def test_b(self):
assert 0
def test_c(self):
assert 0
def test_d(self):
assert 0
class TestCase2(unittest.TestCase):
def test_a(self):
assert 0
def test_b(self):
assert 0
def test_c(self):
assert 0
unittest.main()
assert flag == [None]
|