summaryrefslogtreecommitdiffstats
path: root/test/run_test.py
diff options
context:
space:
mode:
authorAndrej Kozemcak <andrej.kozemcak@pantheon.tech>2019-05-30 13:53:07 +0200
committerAndrej Kozemcak <andrej.kozemcak@pantheon.tech>2019-06-03 10:01:30 +0200
commite77922662052f0caec4129ee43ab9d176b806769 (patch)
treeb66dbd8426c9d485e10ea5499748d5824a7f247b /test/run_test.py
parentadf44f2a5eeb056c5fece0454d3e09d08df160fe (diff)
[TEST] - automatic test run
- skript find all test file - start test function from test file Change-Id: I3b37247c960afa6bf788cd14f1d8d240af3100c6 Signed-off-by: Andrej Kozemcak <andrej.kozemcak@pantheon.tech>
Diffstat (limited to 'test/run_test.py')
-rwxr-xr-xtest/run_test.py93
1 files changed, 81 insertions, 12 deletions
diff --git a/test/run_test.py b/test/run_test.py
index a4cce6d..7acd152 100755
--- a/test/run_test.py
+++ b/test/run_test.py
@@ -19,23 +19,92 @@
import unittest
import util
+import argparse
+import os
+import importlib
+import sys
+import fnmatch
-from framework import SweetcombTestCase
-from test_ietf_interfaces import TestIetfInterfaces
-from test_oc_interfaces import TestOcInterfaces
+from framework import SweetcombTestCase, SweetcombTestRunner
-def suite():
- suite = unittest.TestSuite()
- suite.addTest(TestIetfInterfaces('test_ipv4'))
- suite.addTest(TestIetfInterfaces('test_interface'))
- suite.addTest(TestOcInterfaces('test_interface'))
- suite.addTest(TestOcInterfaces('test_interface_ipv4'))
- return suite
+
+class SplitToSuitesCallback:
+ def __init__(self):
+ self.suites = {}
+ self.suite_name = 'default'
+
+ def __call__(self, file_name, cls, method):
+ test_method = cls(method)
+
+ self.suite_name = file_name + cls.__name__
+ if self.suite_name not in self.suites:
+ self.suites[self.suite_name] = unittest.TestSuite()
+ self.suites[self.suite_name].addTest(test_method)
+
+
+def discover_tests(directory, callback, ignore_path):
+ do_insert = True
+ for _f in os.listdir(directory):
+ f = "%s/%s" % (directory, _f)
+ if os.path.isdir(f):
+ if ignore_path is not None and f.startswith(ignore_path):
+ continue
+ discover_tests(f, callback, ignore_path)
+ continue
+ if not os.path.isfile(f):
+ continue
+ if do_insert:
+ sys.path.insert(0, directory)
+ do_insert = False
+ if not _f.startswith("test_") or not _f.endswith(".py"):
+ continue
+ name = "".join(f.split("/")[-1].split(".")[:-1])
+ module = importlib.import_module(name)
+ for name, cls in module.__dict__.items():
+ if not isinstance(cls, type):
+ continue
+ if not issubclass(cls, unittest.TestCase):
+ continue
+ if name == "SweetcombTestCase" or name.startswith("Template"):
+ continue
+
+ for method in dir(cls):
+ if not callable(getattr(cls, method)):
+ continue
+ if method.startswith("test_"):
+ callback(_f, cls, method)
if __name__ == '__main__':
+ parser = argparse.ArgumentParser(description="Sweetcomb tests")
+ parser.add_argument("-d", "--dir", action='append', type=str,
+ help="directory containing test files "
+ "(may be specified multiple times)")
+ args = parser.parse_args()
+
util.import_yang_modules()
- runner = unittest.TextTestRunner()
- runner.run(suite())
+
+ ddir = list()
+ if args.dir is None:
+ ddir.append(os.getcwd())
+ else:
+ ddir = args.dir
+
+ cb = SplitToSuitesCallback()
+
+ ignore_path = 'conf'
+ for d in ddir:
+ print("Adding tests from directory tree {}".format(d))
+ discover_tests(d, cb, ignore_path)
+
+ suites = []
+ for testcase_suite in cb.suites.values():
+ suites.append(testcase_suite)
+
+ full_suite = unittest.TestSuite()
+ #map(full_suite.addTests, suites)
+ for suite in suites:
+ full_suite.addTests(suite)
+ result = SweetcombTestRunner(print_summary=True).run(full_suite)