~coreygoldberg/uci-engine/subunit-results

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
# Ubuntu CI Engine
# Copyright 2014 Canonical Ltd.

# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License version 3, as
# published by the Free Software Foundation.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU Affero General Public License for more details.

# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import unittest


from testing import run_tests


def create_tests_from_ids(ids):
    """Create TestCase objects from a list of ids (strings)."""
    suite = unittest.TestSuite()

    def test_id(name):
        return lambda: name

    for tid in ids:
        # We need an existing method to create a test. Arbitrarily, we use
        # id(), that shouldn't fail ;) We won't run the test anyway.
        test = unittest.TestCase(methodName='id')
        # We can't define the lambda here or 'name' stay bound to the
        # variable instead of the value, use a proxy to capture the value.
        test.id = test_id(tid)
        suite.addTest(test)
    return suite


class TestRenameTests(unittest.TestCase):

    def assertRenamed(self, expected, ids, renamer):
        """Check that the tests created from ``ids`` are renamed ."""
        suite = create_tests_from_ids(ids)
        run_tests.rename_tests(suite, renamer)
        self.assertEqual(expected, [t.id() for t in suite])

    def test_rename_empty_suite(self):
        self.assertRenamed([], [], lambda: lambda old_id: old_id())

    def test_add_suffix(self):
        self.assertRenamed(['foobaz', 'barbaz'], ['foo', 'bar'],
                           lambda old_id: lambda: old_id() + 'baz')

    def test_add_prefix(self):
        self.assertRenamed(['baz.foo', 'baz.bar'], ['foo', 'bar'],
                           lambda old_id: lambda: 'baz.' + old_id())


class TestLoadingTestingTests(unittest.TestCase):

    def test_filering_works(self):
        suite = run_tests.load_testing_tests(['^foo'], None)
        self.assertEqual(0, suite.countTestCases())


class TestArgParsing(unittest.TestCase):

    def test_no_skip(self):
        parser = run_tests.RunTestsArgParser()
        ns = parser.parse_args(['--no-skip'])
        self.assertTrue(ns.no_skip)