aboutsummaryrefslogtreecommitdiff
path: root/dev/tests.py
blob: a065c38b36902fc34bdead314dbb055f43eb5678 (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
# coding: utf-8
from __future__ import unicode_literals, division, absolute_import, print_function

import unittest
import re
import sys

from . import requires_oscrypto
from ._import import _preload

from tests import test_classes

if sys.version_info < (3,):
    range = xrange  # noqa
    from cStringIO import StringIO
else:
    from io import StringIO


def run(matcher=None, repeat=1, ci=False):
    """
    Runs the tests

    :param matcher:
        A unicode string containing a regular expression to use to filter test
        names by. A value of None will cause no filtering.

    :param repeat:
        An integer - the number of times to run the tests

    :param ci:
        A bool, indicating if the tests are being run as part of CI

    :return:
        A bool - if the tests succeeded
    """

    _preload(requires_oscrypto, not ci)

    loader = unittest.TestLoader()
    # We have to manually track the list of applicable tests because for
    # some reason with Python 3.4 on Windows, the tests in a suite are replaced
    # with None after being executed. This breaks the repeat functionality.
    test_list = []
    for test_class in test_classes():
        if matcher:
            names = loader.getTestCaseNames(test_class)
            for name in names:
                if re.search(matcher, name):
                    test_list.append(test_class(name))
        else:
            test_list.append(loader.loadTestsFromTestCase(test_class))

    stream = sys.stdout
    verbosity = 1
    if matcher and repeat == 1:
        verbosity = 2
    elif repeat > 1:
        stream = StringIO()

    for _ in range(0, repeat):
        suite = unittest.TestSuite()
        for test in test_list:
            suite.addTest(test)
        result = unittest.TextTestRunner(stream=stream, verbosity=verbosity).run(suite)

        if len(result.errors) > 0 or len(result.failures) > 0:
            if repeat > 1:
                print(stream.getvalue())
            return False

        if repeat > 1:
            stream.truncate(0)

    return True