test_main.py 4.22 KB
Newer Older
1
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

import sys
import getopt
import os
import unittest
import calc_tests
import importlib
import importlib.machinery

16 17
import uitest.config

18
from uitest.framework import UITestCase
19

20
from libreoffice.connection import OfficeConnection
21

22 23
test_name_limit_found = False

24
def parseArgs(argv):
25
    (optlist,args) = getopt.getopt(argv[1:], "hdr",
26
            ["help", "debug", "soffice=", "userdir=", "dir=", "file=", "gdb"])
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    return (dict(optlist), args)

def usage():
    message = """usage: {program} [option]... [task_file]..."
 -h | --help:      print usage information
 {connection_params}
 the 'task_file' parameters should be
  full absolute pathnames, not URLs."""
    print(message.format(program = os.path.basename(sys.argv[0]), \
        connection_params = OfficeConnection.getHelpText()))


def find_test_files(dir_path):
    valid_files = []
    for f in os.listdir(dir_path):
        file_path = os.path.join(dir_path, f)

        # don't go through the sub-directories
        if not os.path.isfile(file_path):
            continue

48
        # fail on any non .py files
49
        if not os.path.splitext(file_path)[1] == ".py":
50
            raise Exception("file with an extension which is not .py: " + file_path)
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

        # ignore the __init__.py file
        # it is obviously not a test file
        if f is "__init__.py":
            continue

        valid_files.append(file_path)

    return valid_files

def get_classes_of_module(module):
    md = module.__dict__
    return [ md[c] for c in md if (
            isinstance(md[c], type) and md[c].__module__ == module.__name__ ) ]

def get_test_case_classes_of_module(module):
    classes = get_classes_of_module(module)
    return [ c for c in classes if issubclass(c, UITestCase) ]

70
def add_tests_for_file(test_file, test_suite):
71
    test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
72
    test_loader = unittest.TestLoader()
73
    module_name = os.path.splitext(os.path.split(test_file)[1])[0]
74

75 76 77
    loader = importlib.machinery.SourceFileLoader(module_name, test_file)
    mod = loader.load_module()
    classes = get_test_case_classes_of_module(mod)
78
    global test_name_limit_found
79 80 81
    for c in classes:
        test_names = test_loader.getTestCaseNames(c)
        for test_name in test_names:
82
            full_name = ".".join([module_name, c.__name__, test_name])
83 84 85 86
            if len(test_name_limit) > 0:
                if not test_name_limit.startswith(full_name):
                    continue
                test_name_limit_found = True
87

88 89 90 91
            obj = c(test_name, opts)
            test_suite.addTest(obj)

def get_test_suite_for_dir(opts):
92 93 94 95
    test_suite = unittest.TestSuite()

    valid_test_files = find_test_files(opts['--dir'])
    for test_file in valid_test_files:
96
        add_tests_for_file(test_file, test_suite)
97 98 99 100 101 102 103 104 105 106 107
    return test_suite


if __name__ == '__main__':
    (opts,args) = parseArgs(sys.argv)
    if "-h" in opts or "--help" in opts:
        usage()
        sys.exit()
    elif not "--soffice" in opts:
        usage()
        sys.exit(1)
108 109
    elif "--dir" in opts:
        test_suite = get_test_suite_for_dir(opts)
110 111 112 113 114
        test_name_limit = os.environ.get('UITEST_TEST_NAME', '')
        print(test_name_limit_found)
        if len(test_name_limit) > 0 and not test_name_limit_found:
            print("UITEST_TEST_NAME '%s' does not match any test" % test_name_limit)
            sys.exit(1)
115 116 117 118
    elif "--file" in opts:
        test_suite = unittest.TestSuite()
        add_tests_for_file(opts['--file'], test_suite)
    else:
119 120 121
        usage()
        sys.exit()

122 123 124
    if "-d" in opts or "--debug" in opts:
        uitest.config.use_sleep = True

125
    result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(test_suite)
126 127 128
    print("Tests run: %d" % result.testsRun)
    print("Tests failed: %d" % len(result.failures))
    print("Tests errors: %d" % len(result.errors))
129
    print("Tests skipped: %d" % len(result.skipped))
130 131 132
    if not result.wasSuccessful():
        sys.exit(1)
    sys.exit(0)
133

134
# vim: set shiftwidth=4 softtabstop=4 expandtab: