make test: support out-of-tree tests
[vpp.git] / test / run_tests.py
1 #!/usr/bin/env python
2
3 import sys
4 import os
5 import unittest
6 import argparse
7 import importlib
8 from framework import VppTestRunner
9
10
11 def add_from_dir(suite, directory):
12     do_insert = True
13     for _f in os.listdir(directory):
14         f = "%s/%s" % (directory, _f)
15         if os.path.isdir(f):
16             add_from_dir(suite, f)
17             continue
18         if not os.path.isfile(f):
19             continue
20         if do_insert:
21             sys.path.insert(0, directory)
22             do_insert = False
23         if not _f.startswith("test_") or not _f.endswith(".py"):
24             continue
25         name = "".join(f.split("/")[-1].split(".")[:-1])
26         if name in sys.modules:
27             raise Exception("Duplicate test module `%s' found!" % name)
28         module = importlib.import_module(name)
29         for name, cls in module.__dict__.items():
30             if not isinstance(cls, type):
31                 continue
32             if not issubclass(cls, unittest.TestCase):
33                 continue
34             if name == "VppTestCase":
35                 continue
36             for method in dir(cls):
37                 if not callable(getattr(cls, method)):
38                     continue
39                 if method.startswith("test_"):
40                     suite.addTest(cls(method))
41
42 if __name__ == '__main__':
43     try:
44         verbose = int(os.getenv("V", 0))
45     except:
46         verbose = 0
47
48     parser = argparse.ArgumentParser(description="VPP unit tests")
49     parser.add_argument("-f", "--failfast", action='count',
50                         help="fast failure flag")
51     parser.add_argument("-d", "--dir", action='append', type=str,
52                         help="directory containing test files "
53                              "(may be specified multiple times)")
54     args = parser.parse_args()
55     failfast = True if args.failfast == 1 else False
56
57     suite = unittest.TestSuite()
58     for d in args.dir:
59         print("Adding tests from directory tree %s" % d)
60         add_from_dir(suite, d)
61     VppTestRunner(verbosity=verbose, failfast=failfast).run(suite)