~jayvdb/testscenarios/0.4

« back to all changes in this revision

Viewing changes to lib/testscenarios/scenarios.py

  • Committer: Robert Collins
  • Date: 2010-10-12 06:57:23 UTC
  • mfrom: (17.1.3 658044-multiply)
  • Revision ID: robertc@robertcollins.net-20101012065723-13z174op8al4s6zn
Merge a tweaked version of Martins load_tests and multiply_scenarios patch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
#  dependency injection ('scenarios') by tests.
3
3
#
4
4
# Copyright (c) 2009, Robert Collins <robertc@robertcollins.net>
 
5
# Copyright (c) 2010 Martin Pool <mbp@sourcefrog.net>
5
6
6
7
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
7
8
# license at the users choice. A copy of both licenses are available in the
18
19
    'apply_scenario',
19
20
    'apply_scenarios',
20
21
    'generate_scenarios',
 
22
    'load_tests_apply_scenarios',
 
23
    'multiply_scenarios',
21
24
    ]
22
25
 
 
26
from itertools import (
 
27
    chain,
 
28
    product,
 
29
    )
23
30
import unittest
24
31
 
25
32
from testtools.testcase import clone_test_with_new_id
76
83
                yield newtest
77
84
        else:
78
85
            yield test
 
86
 
 
87
 
 
88
def load_tests_apply_scenarios(*params):
 
89
    """Adapter test runner load hooks to call generate_scenarios.
 
90
 
 
91
    If this is referenced by the `load_tests` attribute of a module, then
 
92
    testloaders that implement this protocol will automatically arrange for
 
93
    the scenarios to be expanded. This can be used instead of using
 
94
    TestWithScenarios.
 
95
 
 
96
    Two different calling conventions for load_tests have been used, and this
 
97
    function should support both. Python 2.7 passes (loader, standard_tests,
 
98
    pattern), and bzr used (standard_tests, module, loader).
 
99
 
 
100
    :param loader: A TestLoader.
 
101
    :param standard_test: The test objects found in this module before 
 
102
        multiplication.
 
103
    """
 
104
    if getattr(params[0], 'suiteClass', None) is not None:
 
105
        loader, standard_tests, pattern = params
 
106
    else:
 
107
        standard_tests, module, loader = params
 
108
    result = loader.suiteClass()
 
109
    result.addTests(generate_scenarios(standard_tests))
 
110
    return result
 
111
 
 
112
 
 
113
def multiply_scenarios(*scenarios):
 
114
    """Multiply two or more iterables of scenarios.
 
115
 
 
116
    It is safe to pass scenario generators or iterators.
 
117
 
 
118
    :returns: A list of compound scenarios: the cross-product of all 
 
119
        scenarios, with the names concatenated and the parameters
 
120
        merged together.
 
121
    """
 
122
    result = []
 
123
    scenario_lists = map(list, scenarios)
 
124
    for combination in product(*scenario_lists):
 
125
        names, parameters = zip(*combination)
 
126
        scenario_name = ','.join(names)
 
127
        scenario_parameters = {}
 
128
        for parameter in parameters:
 
129
            scenario_parameters.update(parameter)
 
130
        result.append((scenario_name, scenario_parameters))
 
131
    return result