~bzr-pqm/bzr/bzr.dev

6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
1
# Copyright (C) 2010, 2016 Canonical Ltd
5462.3.4 by Martin Pool
Add unite tests for variations
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
5462.3.14 by Martin Pool
Unify varations with scenario protocol
17
18
"""Tests for generating multiple tests for scenarios."""
19
5462.3.4 by Martin Pool
Add unite tests for variations
20
from bzrlib.tests import (
21
    TestCase,
22
    TestLoader,
23
    iter_suite_tests,
5462.3.14 by Martin Pool
Unify varations with scenario protocol
24
    multiply_tests,
5462.3.4 by Martin Pool
Add unite tests for variations
25
    )
26
5462.3.14 by Martin Pool
Unify varations with scenario protocol
27
from bzrlib.tests.scenarios import (
5462.3.21 by Martin Pool
Rename to load_tests_apply_scenarios
28
    load_tests_apply_scenarios,
5462.3.14 by Martin Pool
Unify varations with scenario protocol
29
    multiply_scenarios,
30
    multiply_tests_by_their_scenarios,
5462.3.4 by Martin Pool
Add unite tests for variations
31
    )
32
33
5462.3.8 by Martin Pool
Split out reusable load_tests_from_their_variations
34
# There aren't any actually parameterized tests here, but this exists as a
35
# demonstration; so that you can interactively observe them being multiplied;
36
# and so that we check everything hooks up properly.
5462.3.21 by Martin Pool
Rename to load_tests_apply_scenarios
37
load_tests = load_tests_apply_scenarios
5462.3.14 by Martin Pool
Unify varations with scenario protocol
38
39
40
def vary_by_color():
41
    """Very simple static variation example"""
42
    for color in ['red', 'green', 'blue']:
43
        yield (color, {'color': color})
44
45
46
def vary_named_attribute(attr_name):
47
    """More sophisticated: vary a named parameter"""
48
    yield ('a', {attr_name: 'a'})
49
    yield ('b', {attr_name: 'b'})
5462.3.4 by Martin Pool
Add unite tests for variations
50
51
5462.3.5 by Martin Pool
Add multiply_tests_by_their_variations and test
52
def get_generated_test_attributes(suite, attr_name):
5462.3.14 by Martin Pool
Unify varations with scenario protocol
53
    """Return the `attr_name` attribute from all tests in the suite"""
5462.3.5 by Martin Pool
Add multiply_tests_by_their_variations and test
54
    return sorted([
55
        getattr(t, attr_name) for t in iter_suite_tests(suite)])
56
57
5462.3.14 by Martin Pool
Unify varations with scenario protocol
58
class TestTestScenarios(TestCase):
5462.3.4 by Martin Pool
Add unite tests for variations
59
5462.3.14 by Martin Pool
Unify varations with scenario protocol
60
    def test_multiply_tests(self):
5462.3.4 by Martin Pool
Add unite tests for variations
61
        loader = TestLoader()
62
        suite = loader.suiteClass()
5462.3.14 by Martin Pool
Unify varations with scenario protocol
63
        multiply_tests(
5462.3.4 by Martin Pool
Add unite tests for variations
64
            self,
5462.3.14 by Martin Pool
Unify varations with scenario protocol
65
            vary_by_color(),
5462.3.4 by Martin Pool
Add unite tests for variations
66
            suite)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
67
        self.assertEqual(
5462.3.14 by Martin Pool
Unify varations with scenario protocol
68
            ['blue', 'green', 'red'],
69
            get_generated_test_attributes(suite, 'color'))
70
71
    def test_multiply_scenarios_from_generators(self):
72
        """It's safe to multiply scenarios that come from generators"""
73
        s = multiply_scenarios(
74
            vary_named_attribute('one'),
75
            vary_named_attribute('two'),
76
            )
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
77
        self.assertEqual(
5462.3.14 by Martin Pool
Unify varations with scenario protocol
78
            2*2,
79
            len(s),
80
            s)
81
82
    def test_multiply_tests_by_their_scenarios(self):
5462.3.5 by Martin Pool
Add multiply_tests_by_their_variations and test
83
        loader = TestLoader()
84
        suite = loader.suiteClass()
5462.3.14 by Martin Pool
Unify varations with scenario protocol
85
        test_instance = PretendVaryingTest('test_nothing')
86
        multiply_tests_by_their_scenarios(
87
            test_instance,
5462.3.5 by Martin Pool
Add multiply_tests_by_their_variations and test
88
            suite)
6614.1.3 by Vincent Ladeuil
Fix assertEquals being deprecated by using assertEqual.
89
        self.assertEqual(
5462.3.10 by Martin Pool
Code style cleanups
90
            ['a', 'a', 'b', 'b'],
91
            get_generated_test_attributes(suite, 'value'))
5462.3.5 by Martin Pool
Add multiply_tests_by_their_variations and test
92
5462.3.14 by Martin Pool
Unify varations with scenario protocol
93
    def test_multiply_tests_no_scenarios(self):
94
        """Tests with no scenarios attribute aren't multiplied"""
5462.3.6 by Martin Pool
Test multiple varations, and no variations
95
        suite = TestLoader().suiteClass()
5462.3.14 by Martin Pool
Unify varations with scenario protocol
96
        multiply_tests_by_their_scenarios(self,
5462.3.6 by Martin Pool
Test multiple varations, and no variations
97
            suite)
5462.3.14 by Martin Pool
Unify varations with scenario protocol
98
        self.assertLength(1, list(iter_suite_tests(suite)))
5462.3.5 by Martin Pool
Add multiply_tests_by_their_variations and test
99
5462.3.7 by Martin Pool
Add demonstration load_tests to test_variations
100
5462.3.5 by Martin Pool
Add multiply_tests_by_their_variations and test
101
class PretendVaryingTest(TestCase):
102
    
5462.3.14 by Martin Pool
Unify varations with scenario protocol
103
    scenarios = multiply_scenarios(
104
        vary_named_attribute('value'), 
105
        vary_named_attribute('other'),
106
        )
5462.3.5 by Martin Pool
Add multiply_tests_by_their_variations and test
107
108
    def test_nothing(self):
109
        """This test exists just so it can be multiplied"""
110
        pass