~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_scenarios.py

  • Committer: Andrew Bennetts
  • Date: 2010-10-08 08:15:14 UTC
  • mto: This revision was merged to the branch mainline in revision 5498.
  • Revision ID: andrew.bennetts@canonical.com-20101008081514-dviqzrdfwyzsqbz2
Split NEWS into per-release doc/en/release-notes/bzr-*.txt

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010, 2016 Canonical Ltd
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
 
 
17
 
 
18
 
"""Tests for generating multiple tests for scenarios."""
19
 
 
20
 
from bzrlib.tests import (
21
 
    TestCase,
22
 
    TestLoader,
23
 
    iter_suite_tests,
24
 
    multiply_tests,
25
 
    )
26
 
 
27
 
from bzrlib.tests.scenarios import (
28
 
    load_tests_apply_scenarios,
29
 
    multiply_scenarios,
30
 
    multiply_tests_by_their_scenarios,
31
 
    )
32
 
 
33
 
 
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.
37
 
load_tests = load_tests_apply_scenarios
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'})
50
 
 
51
 
 
52
 
def get_generated_test_attributes(suite, attr_name):
53
 
    """Return the `attr_name` attribute from all tests in the suite"""
54
 
    return sorted([
55
 
        getattr(t, attr_name) for t in iter_suite_tests(suite)])
56
 
 
57
 
 
58
 
class TestTestScenarios(TestCase):
59
 
 
60
 
    def test_multiply_tests(self):
61
 
        loader = TestLoader()
62
 
        suite = loader.suiteClass()
63
 
        multiply_tests(
64
 
            self,
65
 
            vary_by_color(),
66
 
            suite)
67
 
        self.assertEqual(
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
 
            )
77
 
        self.assertEqual(
78
 
            2*2,
79
 
            len(s),
80
 
            s)
81
 
 
82
 
    def test_multiply_tests_by_their_scenarios(self):
83
 
        loader = TestLoader()
84
 
        suite = loader.suiteClass()
85
 
        test_instance = PretendVaryingTest('test_nothing')
86
 
        multiply_tests_by_their_scenarios(
87
 
            test_instance,
88
 
            suite)
89
 
        self.assertEqual(
90
 
            ['a', 'a', 'b', 'b'],
91
 
            get_generated_test_attributes(suite, 'value'))
92
 
 
93
 
    def test_multiply_tests_no_scenarios(self):
94
 
        """Tests with no scenarios attribute aren't multiplied"""
95
 
        suite = TestLoader().suiteClass()
96
 
        multiply_tests_by_their_scenarios(self,
97
 
            suite)
98
 
        self.assertLength(1, list(iter_suite_tests(suite)))
99
 
 
100
 
 
101
 
class PretendVaryingTest(TestCase):
102
 
    
103
 
    scenarios = multiply_scenarios(
104
 
        vary_named_attribute('value'), 
105
 
        vary_named_attribute('other'),
106
 
        )
107
 
 
108
 
    def test_nothing(self):
109
 
        """This test exists just so it can be multiplied"""
110
 
        pass