Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm doing a simple script to run and test my code. How can i import dinamically and run my test classes?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
402 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is the solution that I found to import and dynamically 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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...