~bzr-pqm/bzr/bzr.dev

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# Copyright (C) 2005-2010 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


"""Tests for generating multiple tests for scenarios."""

from bzrlib.tests import (
    TestCase,
    TestLoader,
    iter_suite_tests,
    multiply_tests,
    )

from bzrlib.tests.scenarios import (
    load_tests_apply_scenarios,
    multiply_scenarios,
    multiply_tests_by_their_scenarios,
    )


# There aren't any actually parameterized tests here, but this exists as a
# demonstration; so that you can interactively observe them being multiplied;
# and so that we check everything hooks up properly.
load_tests = load_tests_apply_scenarios


def vary_by_color():
    """Very simple static variation example"""
    for color in ['red', 'green', 'blue']:
        yield (color, {'color': color})


def vary_named_attribute(attr_name):
    """More sophisticated: vary a named parameter"""
    yield ('a', {attr_name: 'a'})
    yield ('b', {attr_name: 'b'})


def get_generated_test_attributes(suite, attr_name):
    """Return the `attr_name` attribute from all tests in the suite"""
    return sorted([
        getattr(t, attr_name) for t in iter_suite_tests(suite)])


class TestTestScenarios(TestCase):

    def test_multiply_tests(self):
        loader = TestLoader()
        suite = loader.suiteClass()
        multiply_tests(
            self,
            vary_by_color(),
            suite)
        self.assertEquals(
            ['blue', 'green', 'red'],
            get_generated_test_attributes(suite, 'color'))

    def test_multiply_scenarios_from_generators(self):
        """It's safe to multiply scenarios that come from generators"""
        s = multiply_scenarios(
            vary_named_attribute('one'),
            vary_named_attribute('two'),
            )
        self.assertEquals(
            2*2,
            len(s),
            s)

    def test_multiply_tests_by_their_scenarios(self):
        loader = TestLoader()
        suite = loader.suiteClass()
        test_instance = PretendVaryingTest('test_nothing')
        multiply_tests_by_their_scenarios(
            test_instance,
            suite)
        self.assertEquals(
            ['a', 'a', 'b', 'b'],
            get_generated_test_attributes(suite, 'value'))

    def test_multiply_tests_no_scenarios(self):
        """Tests with no scenarios attribute aren't multiplied"""
        suite = TestLoader().suiteClass()
        multiply_tests_by_their_scenarios(self,
            suite)
        self.assertLength(1, list(iter_suite_tests(suite)))


class PretendVaryingTest(TestCase):
    
    scenarios = multiply_scenarios(
        vary_named_attribute('value'), 
        vary_named_attribute('other'),
        )

    def test_nothing(self):
        """This test exists just so it can be multiplied"""
        pass