xref: /openbmc/openbmc/poky/meta/lib/oeqa/core/utils/test.py (revision c342db35)
1#
2# Copyright (C) 2016 Intel Corporation
3#
4# SPDX-License-Identifier: MIT
5#
6
7import os
8import inspect
9import unittest
10
11def getSuiteCases(suite):
12    """
13        Returns individual test from a test suite.
14    """
15    tests = []
16
17    if isinstance(suite, unittest.TestCase):
18        tests.append(suite)
19    elif isinstance(suite, unittest.suite.TestSuite):
20        for item in suite:
21            tests.extend(getSuiteCases(item))
22
23    return tests
24
25def getSuiteModules(suite):
26    """
27        Returns modules in a test suite.
28    """
29    modules = set()
30    for test in getSuiteCases(suite):
31        modules.add(getCaseModule(test))
32    return modules
33
34def getSuiteCasesInfo(suite, func):
35    """
36        Returns test case info from suite. Info is fetched from func.
37    """
38    tests = []
39    for test in getSuiteCases(suite):
40        tests.append(func(test))
41    return tests
42
43def getSuiteCasesNames(suite):
44    """
45        Returns test case names from suite.
46    """
47    return getSuiteCasesInfo(suite, getCaseMethod)
48
49def getSuiteCasesIDs(suite):
50    """
51        Returns test case ids from suite.
52    """
53    return getSuiteCasesInfo(suite, getCaseID)
54
55def getSuiteCasesFiles(suite):
56    """
57        Returns test case files paths from suite.
58    """
59    return getSuiteCasesInfo(suite, getCaseFile)
60
61def getCaseModule(test_case):
62    """
63        Returns test case module name.
64    """
65    return test_case.__module__
66
67def getCaseClass(test_case):
68    """
69        Returns test case class name.
70    """
71    return test_case.__class__.__name__
72
73def getCaseID(test_case):
74    """
75        Returns test case complete id.
76    """
77    return test_case.id()
78
79def getCaseFile(test_case):
80    """
81        Returns test case file path.
82    """
83    return inspect.getsourcefile(test_case.__class__)
84
85def getCaseMethod(test_case):
86    """
87        Returns test case method name.
88    """
89    return getCaseID(test_case).split('.')[-1]
90