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 |
Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 7 | |
| 8 | |
Saima Yunus | c7f93b3 | 2022-08-10 03:25:31 -0400 | [diff] [blame] | 9 | def discover_tests(directory, callback): |
Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 10 | do_insert = True |
| 11 | for _f in os.listdir(directory): |
| 12 | f = "%s/%s" % (directory, _f) |
| 13 | if os.path.isdir(f): |
Saima Yunus | c7f93b3 | 2022-08-10 03:25:31 -0400 | [diff] [blame] | 14 | discover_tests(f, callback) |
Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 15 | continue |
| 16 | if not os.path.isfile(f): |
| 17 | continue |
| 18 | if do_insert: |
| 19 | sys.path.insert(0, directory) |
| 20 | do_insert = False |
| 21 | if not _f.startswith("test_") or not _f.endswith(".py"): |
| 22 | continue |
| 23 | name = "".join(f.split("/")[-1].split(".")[:-1]) |
Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 24 | module = importlib.import_module(name) |
| 25 | for name, cls in module.__dict__.items(): |
| 26 | if not isinstance(cls, type): |
| 27 | continue |
| 28 | if not issubclass(cls, unittest.TestCase): |
| 29 | continue |
Dave Wallace | 8800f73 | 2023-08-31 00:47:44 -0400 | [diff] [blame] | 30 | if ( |
| 31 | name == "VppTestCase" |
| 32 | or name == "VppAsfTestCase" |
| 33 | or name.startswith("Template") |
| 34 | ): |
Klement Sekera | fcbf444 | 2017-08-17 07:38:42 +0200 | [diff] [blame] | 35 | continue |
| 36 | for method in dir(cls): |
| 37 | if not callable(getattr(cls, method)): |
| 38 | continue |
| 39 | if method.startswith("test_"): |
| 40 | callback(_f, cls, method) |
| 41 | |
| 42 | |
| 43 | def print_callback(file_name, cls, method): |
| 44 | print("%s.%s.%s" % (file_name, cls.__name__, method)) |