summaryrefslogtreecommitdiff
path: root/pypers/marelli/modulo4/code2utest.py
blob: 32e7335d2f8556035593a71e388dfa57add0dc1d (plain)
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
"""
Convert tests from modules into test cases. 
"""

import sys, unittest, time
from ms.misc_utils import import_

def makeSuite(name, code):
    dic = {}
    exec compile(code, name, 'exec') in dic
    tests = []
    for name, test in dic.iteritems():
        if name.startswith("test"):
            tests.append([name, test])
    TC = type(name, (unittest.TestCase,), dict(tests))
    return unittest.makeSuite(TC)

class Printer(object):
    def write(self, text):
        sys.stdout.write(text)
        
def run(modulenames, stream=Printer(), descriptions=1, verbosity=1):
    runner = unittest.TextTestRunner(stream, descriptions, verbosity)
    suites = [makeSuite(name, code) for name, code in modulenames]
    return runner.run(unittest.TestSuite(suites))

def fnames2list(fnames):
    return [(fname[:-3], file(fname).read()) for fname in fnames]

if __name__ == "__main__": # example
    print run(fnames2list('utest_1.py utest_2.py'.split()))