Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Klement Sekera | 993e0ed | 2017-03-16 09:14:59 +0100 | [diff] [blame] | 3 | import sys |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 4 | import os |
| 5 | import unittest |
Klement Sekera | 993e0ed | 2017-03-16 09:14:59 +0100 | [diff] [blame] | 6 | import argparse |
| 7 | import importlib |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 8 | from framework import VppTestRunner |
| 9 | |
Klement Sekera | 993e0ed | 2017-03-16 09:14:59 +0100 | [diff] [blame] | 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 | |
Damjan Marion | f56b77a | 2016-10-03 19:44:57 +0200 | [diff] [blame] | 42 | if __name__ == '__main__': |
| 43 | try: |
| 44 | verbose = int(os.getenv("V", 0)) |
| 45 | except: |
| 46 | verbose = 0 |
Klement Sekera | 993e0ed | 2017-03-16 09:14:59 +0100 | [diff] [blame] | 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) |
Klement Sekera | 0c8ad44 | 2017-03-24 04:29:06 +0100 | [diff] [blame] | 61 | sys.exit(not VppTestRunner(verbosity=verbose, |
| 62 | failfast=failfast).run(suite).wasSuccessful()) |