diff options
author | Martin Pool <mbp@canonical.com> | 2010-12-09 13:40:21 +1100 |
---|---|---|
committer | Martin Pool <mbp@canonical.com> | 2010-12-09 13:40:21 +1100 |
commit | da26369a7f2c602587f01f122a79e0903815f78e (patch) | |
tree | 5c017f9a31004e4733f6e1aea50837352716a463 /lib | |
parent | e1339ea008f571b21ad13c5d69926ed6d66bb290 (diff) | |
download | testscenarios-da26369a7f2c602587f01f122a79e0903815f78e.tar.gz |
Add per_module_scenarios
Diffstat (limited to 'lib')
-rw-r--r-- | lib/testscenarios/scenarios.py | 36 | ||||
-rw-r--r-- | lib/testscenarios/tests/test_scenarios.py | 18 |
2 files changed, 52 insertions, 2 deletions
diff --git a/lib/testscenarios/scenarios.py b/lib/testscenarios/scenarios.py index 80847d6..e052c22 100644 --- a/lib/testscenarios/scenarios.py +++ b/lib/testscenarios/scenarios.py @@ -2,7 +2,7 @@ # dependency injection ('scenarios') by tests. # # Copyright (c) 2009, Robert Collins <robertc@robertcollins.net> -# Copyright (c) 2010 Martin Pool <mbp@sourcefrog.net> +# Copyright (c) 2010, 2011 Martin Pool <mbp@sourcefrog.net> # # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the @@ -129,3 +129,37 @@ def multiply_scenarios(*scenarios): scenario_parameters.update(parameter) result.append((scenario_name, scenario_parameters)) return result + + +def per_module_scenarios(attribute_name, modules): + """Generate scenarios for available implementation modules. + + This is typically used when there is a subsystem implemented, for + example, in both Python and C, and we want to apply the same tests to + both, but the C module may sometimes not be available. + + Note: if the module can't be loaded, it's silently omitted from + testing. + + :param attribute_name: A name to be set in the scenario parameter + dictionary (and thence onto the test instance) pointing to the + implementation module for this scenario. + + :param modules: An iterable of (short_name, module_name), where + the short name is something like 'python' to put in the + scenario name, and the long name is a fully-qualified Python module + name. + """ + scenarios = [] + for short_name, module_name in modules: + try: + mod = __import__(module_name, {}, {}, ['']) + except ImportError: + # TODO: optionally pass this back through a callback, so it can be + # logged etc? + pass + else: + scenarios.append(( + short_name, + {attribute_name: mod})) + return scenarios diff --git a/lib/testscenarios/tests/test_scenarios.py b/lib/testscenarios/tests/test_scenarios.py index 063df51..1fe12ed 100644 --- a/lib/testscenarios/tests/test_scenarios.py +++ b/lib/testscenarios/tests/test_scenarios.py @@ -2,7 +2,7 @@ # dependency injection ('scenarios') by tests. # # Copyright (c) 2009, Robert Collins <robertc@robertcollins.net> -# Copyright (c) 2010 Martin Pool <mbp@sourcefrog.net> +# Copyright (c) 2010, 2011 Martin Pool <mbp@sourcefrog.net> # # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause # license at the users choice. A copy of both licenses are available in the @@ -239,3 +239,19 @@ class TestMultiplyScenarios(testtools.TestCase): self.assertEqual( 'a,a,a,a', scenarios[0][0]) + + +class TestPerModuleScenarios(testtools.TestCase): + + def test_per_module_scenarios(self): + """Generate scenarios for available modules""" + s = testscenarios.scenarios.per_module_scenarios( + 'the_module', [ + ('Python', 'testscenarios'), + ('unittest', 'unittest'), + ('nonexistent', 'nonexistent'), + ]) + self.assertEqual(s, [ + ('Python', {'the_module': testscenarios}), + ('unittest', {'the_module': unittest}), + ]) |