blob: 1b9c677df267a7504246bcfb55d1319120cd2f09 [file] [log] [blame]
Damjan Marionf56b77a2016-10-03 19:44:57 +02001#!/usr/bin/env python
2
Klement Sekera993e0ed2017-03-16 09:14:59 +01003import sys
Damjan Marionf56b77a2016-10-03 19:44:57 +02004import os
5import unittest
Klement Sekera993e0ed2017-03-16 09:14:59 +01006import argparse
7import importlib
Damjan Marionf56b77a2016-10-03 19:44:57 +02008from framework import VppTestRunner
9
Klement Sekera993e0ed2017-03-16 09:14:59 +010010
11def 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
Damjan Marionf56b77a2016-10-03 19:44:57 +020042if __name__ == '__main__':
43 try:
44 verbose = int(os.getenv("V", 0))
45 except:
46 verbose = 0
Klement Sekera993e0ed2017-03-16 09:14:59 +010047
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)
Klement Sekera0c8ad442017-03-24 04:29:06 +010061 sys.exit(not VppTestRunner(verbosity=verbose,
62 failfast=failfast).run(suite).wasSuccessful())