Debian Codegraph @

tests/unittest_inspector.py

raw
1 #! /usr/bin/env python3
2 # Copyright © 2020, Canonical Ltd
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 # Authors:
17 # Marco Trevisan <[email protected]>
18
19 import argparse
20 import importlib.util
21 import inspect
22 import os
23 import unittest
24
25 def list_tests(module):
26 tests = []
27 for name, obj in inspect.getmembers(module):
28 if inspect.isclass(obj) and issubclass(obj, unittest.TestCase):
29 cases = unittest.defaultTestLoader.getTestCaseNames(obj)
30 tests += [ (obj, '{}.{}'.format(name, t)) for t in cases ]
31 return tests
32
33
34 if __name__ == '__main__':
35 parser = argparse.ArgumentParser()
36 parser.add_argument('unittest_source', type=argparse.FileType('r'))
37
38 args = parser.parse_args()
39 source_path = args.unittest_source.name
40 spec = importlib.util.spec_from_file_location(
41 os.path.basename(source_path), source_path)
42 module = importlib.util.module_from_spec(spec)
43 spec.loader.exec_module(module)
44
45 for machine, human in list_tests(module):
46 print(human)