def test_case(self): """Create a :class:`unittest.TestCase` class from this collection. Tests in the collection are added as instance methods on the ``TestCase``. Method names are prefixed with ``test_`` if they're not already. Duplicate method names are given a ``_#`` suffix. .. testsetup:: from attest import Tests Example:: >>> suite = Tests() >>> @suite.test ... def simple(): ... pass ... >>> @suite.test ... def simple(): ... pass ... >>> TestCase = suite.test_case() >>> assert TestCase.test_simple >>> assert TestCase.test_simple_2 """ from unittest import TestCase counts = counter() methods = {} for func in self: # handle <lambda> name = re.sub("[^a-zA-Z0-9_]", "", func.__name__) if not name: name = "unnamed" if not name.startswith("test_"): name = "test_%s" % (name, ) count = counts.increment(name) if count > 1: name = "%s_%s" % (name, count) methods[name] = staticmethod(func) return type("Tests", (TestCase, ), methods)
def counter(): counter = utils.counter() assert counter.increment("a") == 1 assert counter == {"a": 1}