Statistics
| Branch: | Tag: | Revision:

root / kamaki / cli / test.py @ 2fde8651

History | View | Annotate | Download (3.5 kB)

1 2fde8651 Stavros Sachtouris
# Copyright 2013 GRNET S.A. All rights reserved.
2 2fde8651 Stavros Sachtouris
#
3 2fde8651 Stavros Sachtouris
# Redistribution and use in source and binary forms, with or
4 2fde8651 Stavros Sachtouris
# without modification, are permitted provided that the following
5 2fde8651 Stavros Sachtouris
# conditions are met:
6 2fde8651 Stavros Sachtouris
#
7 2fde8651 Stavros Sachtouris
#   1. Redistributions of source code must retain the above
8 2fde8651 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
9 2fde8651 Stavros Sachtouris
#      disclaimer.
10 2fde8651 Stavros Sachtouris
#
11 2fde8651 Stavros Sachtouris
#   2. Redistributions in binary form must reproduce the above
12 2fde8651 Stavros Sachtouris
#      copyright notice, this list of conditions and the following
13 2fde8651 Stavros Sachtouris
#      disclaimer in the documentation and/or other materials
14 2fde8651 Stavros Sachtouris
#      provided with the distribution.
15 2fde8651 Stavros Sachtouris
#
16 2fde8651 Stavros Sachtouris
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 2fde8651 Stavros Sachtouris
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 2fde8651 Stavros Sachtouris
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 2fde8651 Stavros Sachtouris
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 2fde8651 Stavros Sachtouris
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 2fde8651 Stavros Sachtouris
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 2fde8651 Stavros Sachtouris
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 2fde8651 Stavros Sachtouris
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 2fde8651 Stavros Sachtouris
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 2fde8651 Stavros Sachtouris
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 2fde8651 Stavros Sachtouris
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 2fde8651 Stavros Sachtouris
# POSSIBILITY OF SUCH DAMAGE.
28 2fde8651 Stavros Sachtouris
#
29 2fde8651 Stavros Sachtouris
# The views and conclusions contained in the software and
30 2fde8651 Stavros Sachtouris
# documentation are those of the authors and should not be
31 2fde8651 Stavros Sachtouris
# interpreted as representing official policies, either expressed
32 2fde8651 Stavros Sachtouris
# or implied, of GRNET S.A.
33 2fde8651 Stavros Sachtouris
34 2fde8651 Stavros Sachtouris
from mock import patch, call
35 2fde8651 Stavros Sachtouris
from unittest import makeSuite, TestSuite, TextTestRunner, TestCase
36 2fde8651 Stavros Sachtouris
from time import sleep
37 2fde8651 Stavros Sachtouris
from inspect import getmembers, isclass
38 2fde8651 Stavros Sachtouris
from itertools import product
39 2fde8651 Stavros Sachtouris
from random import randint
40 2fde8651 Stavros Sachtouris
41 2fde8651 Stavros Sachtouris
from kamaki.cli.command_tree.test import Command
42 2fde8651 Stavros Sachtouris
43 2fde8651 Stavros Sachtouris
44 2fde8651 Stavros Sachtouris
#  TestCase auxiliary methods
45 2fde8651 Stavros Sachtouris
46 2fde8651 Stavros Sachtouris
def runTestCase(cls, test_name, args=[], failure_collector=[]):
47 2fde8651 Stavros Sachtouris
    """
48 2fde8651 Stavros Sachtouris
    :param cls: (TestCase) a set of Tests
49 2fde8651 Stavros Sachtouris

50 2fde8651 Stavros Sachtouris
    :param test_name: (str)
51 2fde8651 Stavros Sachtouris

52 2fde8651 Stavros Sachtouris
    :param args: (list) these are prefixed with test_ and used as params when
53 2fde8651 Stavros Sachtouris
        instantiating cls
54 2fde8651 Stavros Sachtouris

55 2fde8651 Stavros Sachtouris
    :param failure_collector: (list) collects info of test failures
56 2fde8651 Stavros Sachtouris

57 2fde8651 Stavros Sachtouris
    :returns: (int) total # of run tests
58 2fde8651 Stavros Sachtouris
    """
59 2fde8651 Stavros Sachtouris
    suite = TestSuite()
60 2fde8651 Stavros Sachtouris
    if args:
61 2fde8651 Stavros Sachtouris
        suite.addTest(cls('_'.join(['test'] + args)))
62 2fde8651 Stavros Sachtouris
    else:
63 2fde8651 Stavros Sachtouris
        suite.addTest(makeSuite(cls))
64 2fde8651 Stavros Sachtouris
    print('* Test * %s *' % test_name)
65 2fde8651 Stavros Sachtouris
    r = TextTestRunner(verbosity=2).run(suite)
66 2fde8651 Stavros Sachtouris
    failure_collector += r.failures
67 2fde8651 Stavros Sachtouris
    return r.testsRun
68 2fde8651 Stavros Sachtouris
69 2fde8651 Stavros Sachtouris
70 2fde8651 Stavros Sachtouris
def get_test_classes(module=__import__(__name__), name=''):
71 2fde8651 Stavros Sachtouris
    module_stack = [module]
72 2fde8651 Stavros Sachtouris
    while module_stack:
73 2fde8651 Stavros Sachtouris
        module = module_stack[-1]
74 2fde8651 Stavros Sachtouris
        module_stack = module_stack[:-1]
75 2fde8651 Stavros Sachtouris
        for objname, obj in getmembers(module):
76 2fde8651 Stavros Sachtouris
            if (objname == name or not name):
77 2fde8651 Stavros Sachtouris
                if isclass(obj) and objname != 'TestCase' and (
78 2fde8651 Stavros Sachtouris
                        issubclass(obj, TestCase)):
79 2fde8651 Stavros Sachtouris
                    yield (obj, objname)
80 2fde8651 Stavros Sachtouris
81 2fde8651 Stavros Sachtouris
82 2fde8651 Stavros Sachtouris
def main(argv):
83 2fde8651 Stavros Sachtouris
    found = False
84 2fde8651 Stavros Sachtouris
    failure_collector = list()
85 2fde8651 Stavros Sachtouris
    num_of_tests = 0
86 2fde8651 Stavros Sachtouris
    for cls, name in get_test_classes(name=argv[1] if len(argv) > 1 else ''):
87 2fde8651 Stavros Sachtouris
        found = True
88 2fde8651 Stavros Sachtouris
        num_of_tests += runTestCase(cls, name, argv[2:], failure_collector)
89 2fde8651 Stavros Sachtouris
    if not found:
90 2fde8651 Stavros Sachtouris
        print('Test "%s" not found' % ' '.join(argv[1:]))
91 2fde8651 Stavros Sachtouris
    else:
92 2fde8651 Stavros Sachtouris
        for i, failure in enumerate(failure_collector):
93 2fde8651 Stavros Sachtouris
            print('Failure %s: ' % (i + 1))
94 2fde8651 Stavros Sachtouris
            for field in failure:
95 2fde8651 Stavros Sachtouris
                print('\t%s' % field)
96 2fde8651 Stavros Sachtouris
        print('\nTotal tests run: %s' % num_of_tests)
97 2fde8651 Stavros Sachtouris
        print('Total failures: %s' % len(failure_collector))
98 2fde8651 Stavros Sachtouris
99 2fde8651 Stavros Sachtouris
100 2fde8651 Stavros Sachtouris
if __name__ == '__main__':
101 2fde8651 Stavros Sachtouris
    from sys import argv
102 2fde8651 Stavros Sachtouris
    main(argv)