mardi 28 juillet 2015

Python unittesting: Importing test classes dinamically and run

This is the solution that i found to import dinamically and run my test classes.

import glob
import os
import imp
import unittest

def execute_all_tests(tests_folder):
    test_file_strings = glob.glob(os.path.join(tests_folder, 'test_*.py'))
    suites = []
    for test in test_file_strings:
        mod_name, file_ext = os.path.splitext(os.path.split(test)[-1])
        py_mod = imp.load_source(mod_name, test)
        suites.append(unittest.defaultTestLoader.loadTestsFromModule(py_mod))
    text_runner = unittest.TextTestRunner().run(unittest.TestSuite(suites))

Aucun commentaire:

Enregistrer un commentaire