aboutsummaryrefslogtreecommitdiffstats
path: root/test/run_test.py
blob: 870e6c56ff74a714482feb782de435c37abaf34d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
#
#  Copyright (c) 2019 PANTHEON.tech.
#  Copyright (c) 2019 Cisco and/or its affiliates.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at:
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#
#

import unittest
import util
import argparse
import os
import importlib
import sys
import fnmatch


from framework import SweetcombTestCase, SweetcombTestRunner


class SplitToSuitesCallback:
    def __init__(self):
        self.suites = {}
        self.suite_name = 'default'

    def __call__(self, file_name, cls, method):
        test_method = cls(method)

        self.suite_name = file_name + cls.__name__
        if self.suite_name not in self.suites:
            self.suites[self.suite_name] = unittest.TestSuite()
        self.suites[self.suite_name].addTest(test_method)


def discover_tests(directory, callback, ignore_path):
    do_insert = True
    for _f in os.listdir(directory):
        f = "%s/%s" % (directory, _f)
        if os.path.isdir(f):
            if ignore_path is not None and f.startswith(ignore_path):
                continue
            discover_tests(f, callback, ignore_path)
            continue
        if not os.path.isfile(f):
            continue
        if do_insert:
            sys.path.insert(0, directory)
            do_insert = False
        if not _f.startswith("test_") or not _f.endswith(".py"):
            continue
        name = "".join(f.split("/")[-1].split(".")[:-1])
        module = importlib.import_module(name)
        for name, cls in module.__dict__.items():
            if not isinstance(cls, type):
                continue
            if not issubclass(cls, unittest.TestCase):
                continue
            if name == "SweetcombTestCase" or name.startswith("Template"):
                continue

            for method in dir(cls):
                if not callable(getattr(cls, method)):
                    continue
                if method.startswith("test_"):
                    callback(_f, cls, method)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="Sweetcomb tests")
    parser.add_argument("-d", "--dir", action='append', type=str,
                        help="directory containing test files "
                             "(may be specified multiple times)")
    args = parser.parse_args()

    ddir = list()
    if args.dir is None:
        ddir.append(os.getcwd())
    else:
        ddir = args.dir

    cb = SplitToSuitesCallback()

    ignore_path = 'conf'
    for d in ddir:
        print("Adding tests from directory tree {}".format(d))
        discover_tests(d, cb, ignore_path)

    suites = []
    for testcase_suite in cb.suites.values():
        suites.append(testcase_suite)

    full_suite = unittest.TestSuite()
    #map(full_suite.addTests, suites)
    for suite in suites:
        full_suite.addTests(suite)
    result = SweetcombTestRunner(verbosity=1,
                                 print_summary=True).run(full_suite)
    was_successful = result.wasSuccessful()
    #if not was_successful:
        #for test_case_info in result.failed_test_cases_info:
            #handle_failed_suite(test_case_info.logger,
                                #test_case_info.tempdir,
                                    #test_case_info.vpp_pid)
    sys.exit(not was_successful)