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