1
# Copyright (C) 2010, 2016 Canonical Ltd
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.
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.
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
18
"""Tests for generating multiple tests for scenarios."""
20
from bzrlib.tests import (
27
from bzrlib.tests.scenarios import (
28
load_tests_apply_scenarios,
30
multiply_tests_by_their_scenarios,
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
41
"""Very simple static variation example"""
42
for color in ['red', 'green', 'blue']:
43
yield (color, {'color': color})
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'})
52
def get_generated_test_attributes(suite, attr_name):
53
"""Return the `attr_name` attribute from all tests in the suite"""
55
getattr(t, attr_name) for t in iter_suite_tests(suite)])
58
class TestTestScenarios(TestCase):
60
def test_multiply_tests(self):
62
suite = loader.suiteClass()
68
['blue', 'green', 'red'],
69
get_generated_test_attributes(suite, 'color'))
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'),
82
def test_multiply_tests_by_their_scenarios(self):
84
suite = loader.suiteClass()
85
test_instance = PretendVaryingTest('test_nothing')
86
multiply_tests_by_their_scenarios(
91
get_generated_test_attributes(suite, 'value'))
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,
98
self.assertLength(1, list(iter_suite_tests(suite)))
101
class PretendVaryingTest(TestCase):
103
scenarios = multiply_scenarios(
104
vary_named_attribute('value'),
105
vary_named_attribute('other'),
108
def test_nothing(self):
109
"""This test exists just so it can be multiplied"""