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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#!/usr/bin/env python
"""Regression testing framework
Borrowed from http://www.diveintopython.org/
This module will search for scripts in the same directory named
XYZtest.py. Each such script should be a test suite that tests a
module through PyUnit. (As of Python 2.1, PyUnit is included in
the standard library as "unittest".) This script will aggregate all
found test suites into one big test suite and run them all at once.
* Modified to use os.path.walk to find all the test scripts
* Modified to find all python scripts, not just ones of the form *test.py
"""
import sys, os, re, unittest
import importlib.machinery
import importlib.util
import logging
rootLogger = logging.getLogger ()
rootLogger.setLevel (logging.CRITICAL)
#ensure that the module in this directory is used instead of the system one
#or else we would be testing the system one and not the one with the changes :)
import sys
sys.path.insert(0, os.path.join(os.getcwd(),'lib'))
print("System path is: " + str (sys.path))
def path_visitor(files, dirname, names):
"""Visits each file in the and appends the filename to the given list"""
if (dirname.find ("PerformanceTests") > 0):
return
for name in names:
files.append(os.path.join(dirname, name))
def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader (modname, filename)
spec = importlib.util.spec_from_file_location (modname, filename, loader=loader)
module = importlib.util.module_from_spec (spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module (module)
return module
def regressionTest():
#Find all the files to run
files = []
for curdir, dirlist, filelist in os.walk ("tests"):
path_visitor (files, curdir, filelist)
test = re.compile(".*\.py$", re.IGNORECASE)
files = list(filter(test.search, files))
#load each test into the testsuite
modules = []
modCount = 1
for filename in files:
modules.append (load_source ("TestModule%s" % str (modCount), filename))
modCount += 1
#modules = map(__import__, moduleNames)
load = unittest.defaultTestLoader.loadTestsFromModule
suite = []
for module in modules:
suite.append (load (module))
return unittest.TestSuite (suite)
#return unittest.TestSuite(map(load, modules))
if __name__ == "__main__":
unittest.main(defaultTest="regressionTest")
|