aboutsummaryrefslogtreecommitdiffstats
path: root/test/__init__.py
blob: cb3728133bd2b0296307b9ee33f799f7cd56f221 (plain) (blame)
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
# -*- coding: utf-8 -*-  IGNORE:C0111
from __future__ import absolute_import, print_function, unicode_literals
import logging
from test import test_reader, test_input
import yamlish
import unittest
import yaml
logging.basicConfig(level=logging.DEBUG)

def _generate_test_name(source):
    """
    Clean up human-friendly test name into a method name.
    """
    out = source.replace(' ', '_').replace(':', '').lower()
    return "test_%s" % out

def _create_test(test_src, tested_function):
    """
    Decorate tested function to be used as a method for TestCase.
    """
    def do_test_expected(self):
        """
        Execute a test by calling a tested_function on test_src data.
        """
        if ('skip' in test_src) and test_src['skip']:
            logging.info("test_src skipped!")
            return

        got = ""
        if 'error' in test_src:
            self.assertRaises(test_src['error'], tested_function,
                              test_src['in'])
        else:
            want = test_src['out']
            got = tested_function(test_src['in'])
            logging.debug("test_src['out'] = %s", unicode(test_src['out']))
            self.assertEqual(got, want, """Result matches
            expected = %s
            
            observed = %s
            """ % (want, got))

    return do_test_expected


def generate_testsuite(test_data, test_case_shell, test_fce):
    """
    Generate tests from the test data, class to build upon and function to use for testing.
    """
    for in_test in test_data:
        if ('skip' in in_test) and in_test['skip']:
            logging.info("test %s skipped!", in_test['name'])
            continue
        name = _generate_test_name(in_test['name'])
        test_method = _create_test (in_test, test_fce)
        test_method.__name__ = str('test_%s' % name)  # IGNORE:W0622
        setattr (test_case_shell, test_method.__name__, test_method)

class TestInput(unittest.TestCase):  # IGNORE:C0111
    pass

class TestReader(unittest.TestCase):  # IGNORE:C0111
    pass

if __name__ == "__main__":
    generate_testsuite(test_reader.test_data_list, TestReader, yamlish.load)
    generate_testsuite(test_input.test_data_list, TestInput, yamlish.load)
    unittest.main()