~testtools-committers/testscenarios/trunk

« back to all changes in this revision

Viewing changes to lib/testscenarios/scenarios.py

  • Committer: Robert Collins
  • Date: 2012-04-04 10:02:46 UTC
  • mfrom: (18.1.1 module-scenarios)
  • Revision ID: robertc@robertcollins.net-20120404100246-n6ap8h2x14uxg144
* New function ``per_module_scenarios`` for tests that should be applied across 
  multiple modules providing the same interface, some of which may not be 
  available at run time.  (Martin Pool, Robert Collins)

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
# Copyright (c) 2010, 2011 Martin Pool <mbp@sourcefrog.net>
6
6
7
7
# Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
8
8
# license at the users choice. A copy of both licenses are available in the
27
27
    chain,
28
28
    product,
29
29
    )
 
30
import sys
30
31
import unittest
31
32
 
32
33
from testtools.testcase import clone_test_with_new_id
129
130
            scenario_parameters.update(parameter)
130
131
        result.append((scenario_name, scenario_parameters))
131
132
    return result
 
133
 
 
134
 
 
135
def per_module_scenarios(attribute_name, modules):
 
136
    """Generate scenarios for available implementation modules.
 
137
 
 
138
    This is typically used when there is a subsystem implemented, for
 
139
    example, in both Python and C, and we want to apply the same tests to
 
140
    both, but the C module may sometimes not be available.
 
141
 
 
142
    Note: if the module can't be loaded, the sys.exc_info() tuple for the
 
143
    exception raised during import of the module is used instead of the module
 
144
    object. A common idiom is to check in setUp for that and raise a skip or
 
145
    error for that case. No special helpers are supplied in testscenarios as
 
146
    yet.
 
147
 
 
148
    :param attribute_name: A name to be set in the scenario parameter
 
149
        dictionary (and thence onto the test instance) pointing to the 
 
150
        implementation module (or import exception) for this scenario.
 
151
 
 
152
    :param modules: An iterable of (short_name, module_name), where 
 
153
        the short name is something like 'python' to put in the
 
154
        scenario name, and the long name is a fully-qualified Python module
 
155
        name.
 
156
    """
 
157
    scenarios = []
 
158
    for short_name, module_name in modules:
 
159
        try:
 
160
            mod = __import__(module_name, {}, {}, [''])
 
161
        except:
 
162
            mod = sys.exc_info()
 
163
        scenarios.append((
 
164
            short_name, 
 
165
            {attribute_name: mod}))
 
166
    return scenarios