Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import os |
| 5 | import unittest |
| 6 | import importlib |
| 7 | import argparse |
| 8 | |
| 9 | |
| 10 | def discover_tests(directory, callback): |
| 11 | do_insert = True |
| 12 | for _f in os.listdir(directory): |
| 13 | f = "%s/%s" % (directory, _f) |
| 14 | if os.path.isdir(f): |
| 15 | discover_tests(f, callback) |
| 16 | continue |
| 17 | if not os.path.isfile(f): |
| 18 | continue |
| 19 | if do_insert: |
| 20 | sys.path.insert(0, directory) |
| 21 | do_insert = False |
| 22 | if not _f.startswith("test_") or not _f.endswith(".py"): |
| 23 | continue |
| 24 | name = "".join(f.split("/")[-1].split(".")[:-1]) |
Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 25 | module = importlib.import_module(name) |
| 26 | for name, cls in module.__dict__.items(): |
| 27 | if not isinstance(cls, type): |
| 28 | continue |
| 29 | if not issubclass(cls, unittest.TestCase): |
| 30 | continue |
Klement Sekera | 31da2e3 | 2018-06-24 22:49:55 +0200 | [diff] [blame] | 31 | if name == "VppTestCase" or name.startswith("Template"): |
Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 32 | continue |
| 33 | for method in dir(cls): |
| 34 | if not callable(getattr(cls, method)): |
| 35 | continue |
| 36 | if method.startswith("test_"): |
| 37 | callback(_f, cls, method) |
| 38 | |
| 39 | |
| 40 | def print_callback(file_name, cls, method): |
| 41 | print("%s.%s.%s" % (file_name, cls.__name__, method)) |
| 42 | |
| 43 | |
| 44 | if __name__ == '__main__': |
| 45 | parser = argparse.ArgumentParser(description="Discover VPP unit tests") |
| 46 | parser.add_argument("-d", "--dir", action='append', type=str, |
| 47 | help="directory containing test files " |
| 48 | "(may be specified multiple times)") |
| 49 | args = parser.parse_args() |
| 50 | if args.dir is None: |
| 51 | args.dir = "." |
| 52 | |
| 53 | suite = unittest.TestSuite() |
| 54 | for d in args.dir: |
| 55 | discover_tests(d, print_callback) |