~jayvdb/testscenarios/0.4

« back to all changes in this revision

Viewing changes to lib/testscenarios/scenarios.py

  • Committer: Martin Pool
  • Date: 2010-12-09 02:40:21 UTC
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: mbp@canonical.com-20101209024021-4finptl33wp3kcs5
Add per_module_scenarios

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
129
129
            scenario_parameters.update(parameter)
130
130
        result.append((scenario_name, scenario_parameters))
131
131
    return result
 
132
 
 
133
 
 
134
def per_module_scenarios(attribute_name, modules):
 
135
    """Generate scenarios for available implementation modules.
 
136
 
 
137
    This is typically used when there is a subsystem implemented, for
 
138
    example, in both Python and C, and we want to apply the same tests to
 
139
    both, but the C module may sometimes not be available.
 
140
 
 
141
    Note: if the module can't be loaded, it's silently omitted from
 
142
    testing.
 
143
 
 
144
    :param attribute_name: A name to be set in the scenario parameter
 
145
        dictionary (and thence onto the test instance) pointing to the 
 
146
        implementation module for this scenario.
 
147
 
 
148
    :param modules: An iterable of (short_name, module_name), where 
 
149
        the short name is something like 'python' to put in the
 
150
        scenario name, and the long name is a fully-qualified Python module
 
151
        name.
 
152
    """
 
153
    scenarios = []
 
154
    for short_name, module_name in modules:
 
155
        try:
 
156
            mod = __import__(module_name, {}, {}, [''])
 
157
        except ImportError:
 
158
            # TODO: optionally pass this back through a callback, so it can be
 
159
            # logged etc?
 
160
            pass
 
161
        else:
 
162
            scenarios.append((
 
163
                short_name, 
 
164
                {attribute_name: mod}))
 
165
    return scenarios