summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README10
-rw-r--r--lib/testscenarios/scenarios.py23
-rw-r--r--lib/testscenarios/tests/__init__.py3
-rw-r--r--lib/testscenarios/tests/test_scenarios.py4
4 files changed, 24 insertions, 16 deletions
diff --git a/README b/README
index 90b418e..002c340 100644
--- a/README
+++ b/README
@@ -111,7 +111,7 @@ implementations::
>>> mytests = loader.loadTestsFromNames(['doc.test_sample'])
>>> test_suite.addTests(generate_scenarios(mytests))
>>> runner.run(test_suite)
- <unittest._TextTestResult run=1 errors=0 failures=0>
+ <unittest...TextTestResult run=1 errors=0 failures=0>
Testloaders
+++++++++++
@@ -277,8 +277,11 @@ module if it can be loaded, but also to have the tests pass if the C module has
not been compiled.
The ``per_module_scenarios`` function generates a scenario for each named
-module, omitting those that raise an ``ImportError``. For each test object,
-the implementation to be tested is installed into a given attribute.
+module. The module object of the imported module is set in the supplied
+attribute name of the resulting scenario.
+Modules which raise ``ImportError`` during import will have the
+``sys.exc_info()`` of the exception set instead of the module object. Tests
+can check for the attribute being a tuple to decide what to do (e.g. to skip).
Note that for the test to be valid, all access to the module under test must go
through the relevant attribute of the test object. If one of the
@@ -286,7 +289,6 @@ implementations is also directly imported by the test module or any other,
testscenarios will not magically stop it being used.
-
Advice on Writing Scenarios
===========================
diff --git a/lib/testscenarios/scenarios.py b/lib/testscenarios/scenarios.py
index e052c22..9538b33 100644
--- a/lib/testscenarios/scenarios.py
+++ b/lib/testscenarios/scenarios.py
@@ -27,6 +27,7 @@ from itertools import (
chain,
product,
)
+import sys
import unittest
from testtools.testcase import clone_test_with_new_id
@@ -138,12 +139,15 @@ def per_module_scenarios(attribute_name, modules):
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.
+ Note: if the module can't be loaded, the sys.exc_info() tuple for the
+ exception raised during import of the module is used instead of the module
+ object. A common idiom is to check in setUp for that and raise a skip or
+ error for that case. No special helpers are supplied in testscenarios as
+ yet.
: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.
+ implementation module (or import exception) for this scenario.
:param modules: An iterable of (short_name, module_name), where
the short name is something like 'python' to put in the
@@ -154,12 +158,9 @@ def per_module_scenarios(attribute_name, modules):
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}))
+ except:
+ mod = sys.exc_info()
+ scenarios.append((
+ short_name,
+ {attribute_name: mod}))
return scenarios
diff --git a/lib/testscenarios/tests/__init__.py b/lib/testscenarios/tests/__init__.py
index e5e2bbe..6230212 100644
--- a/lib/testscenarios/tests/__init__.py
+++ b/lib/testscenarios/tests/__init__.py
@@ -38,5 +38,6 @@ def load_tests(standard_tests, module, loader):
test_mod_names = [prefix + test_module for test_module in test_modules]
standard_tests.addTests(loader.loadTestsFromNames(test_mod_names))
doctest.set_unittest_reportflags(doctest.REPORT_ONLY_FIRST_FAILURE)
- standard_tests.addTest(doctest.DocFileSuite("../../../README"))
+ standard_tests.addTest(
+ doctest.DocFileSuite("../../../README", optionflags=doctest.ELLIPSIS))
return standard_tests
diff --git a/lib/testscenarios/tests/test_scenarios.py b/lib/testscenarios/tests/test_scenarios.py
index 1fe12ed..97aa17f 100644
--- a/lib/testscenarios/tests/test_scenarios.py
+++ b/lib/testscenarios/tests/test_scenarios.py
@@ -251,7 +251,11 @@ class TestPerModuleScenarios(testtools.TestCase):
('unittest', 'unittest'),
('nonexistent', 'nonexistent'),
])
+ self.assertEqual('nonexistent', s[-1][0])
+ self.assertIsInstance(s[-1][1]['the_module'], tuple)
+ s[-1][1]['the_module'] = None
self.assertEqual(s, [
('Python', {'the_module': testscenarios}),
('unittest', {'the_module': unittest}),
+ ('nonexistent', {'the_module': None}),
])