tests: Add support for getting corefile patterns on FreeBSD
[vpp.git] / test / discover_tests.py
1 #!/usr/bin/env python3
2
3 import sys
4 import os
5 import unittest
6 import importlib
7
8
9 def discover_tests(directory, callback):
10     do_insert = True
11     for _f in os.listdir(directory):
12         f = "%s/%s" % (directory, _f)
13         if os.path.isdir(f):
14             discover_tests(f, callback)
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])
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
30             if (
31                 name == "VppTestCase"
32                 or name == "VppAsfTestCase"
33                 or name.startswith("Template")
34             ):
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))