File: meta.py

package info (click to toggle)
task 2.6.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 7,076 kB
  • sloc: cpp: 45,964; python: 12,713; sh: 785; perl: 189; makefile: 21
file content (40 lines) | stat: -rw-r--r-- 1,130 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-




class MetaTest(type):
    """Helper metaclass to simplify dynamic test creation

    Creates test_methods in the TestCase class dynamically named after the
    arguments used.
    """
    @staticmethod
    def make_function(classname, *args, **kwargs):
        def test(self):
            # ### Body of the usual test_testcase ### #
            # Override and redefine this method #
            pass

        # Title of test in report
        test.__doc__ = "{0}".format(args[0])

        return test

    def __new__(meta, classname, bases, dct):
        tests = dct.get("TESTS")
        kwargs = dct.get("EXTRA", {})

        for i, args in enumerate(tests):
            func = meta.make_function(classname, *args, **kwargs)

            # Rename the function after a unique identifier
            # Name of function must start with test_ to be ran by unittest
            func.__name__ = "test_{0}".format(i)

            # Attach the new test to the testclass
            dct[func.__name__] = func

        return super(MetaTest, meta).__new__(meta, classname, bases, dct)

# vim: ai sts=4 et sw=4