Merge lp:~mbp/testscenarios/module-scenarios into lp:~testtools-committers/testscenarios/trunk

Proposed by Martin Pool
Status: Superseded
Proposed branch: lp:~mbp/testscenarios/module-scenarios
Merge into: lp:~testtools-committers/testscenarios/trunk
Diff against target: 166 lines (+99/-0) (has conflicts)
4 files modified
NEWS (+6/-0)
README (+29/-0)
lib/testscenarios/scenarios.py (+41/-0)
lib/testscenarios/tests/test_scenarios.py (+23/-0)
Text conflict in lib/testscenarios/scenarios.py
Text conflict in lib/testscenarios/tests/test_scenarios.py
To merge this branch: bzr merge lp:~mbp/testscenarios/module-scenarios
Reviewer Review Type Date Requested Status
Robert Collins Pending
Review via email: mp+67401@code.launchpad.net

This proposal has been superseded by a proposal from 2011-10-25.

Description of the change

This separates out something we do in bzr, which is to multiply tests by both Python and Pyrex/C implementations of the same interface.

To post a comment you must log in.
Revision history for this message
Robert Collins (lifeless) wrote :

For some reason this has generated a merge proposal with a bunch of conflicts :(

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NEWS'
2--- NEWS 2010-10-12 06:57:23 +0000
3+++ NEWS 2011-07-08 22:26:01 +0000
4@@ -9,6 +9,12 @@
5 0.2
6 ~~~
7
8+NEW FEATURES:
9+
10+* New function ``per_module_scenarios`` for tests that should be applied across
11+ multiple modules providing the same interface, some of which may not be
12+ available at run time. (Martin Pool)
13+
14 CHANGES:
15
16 * Adjust the cloned tests ``shortDescription`` if one is present. (Ben Finney)
17
18=== modified file 'README'
19--- README 2010-10-12 06:57:23 +0000
20+++ README 2011-07-08 22:26:01 +0000
21@@ -258,6 +258,35 @@
22 selection.
23
24
25+Generating Scenarios
26+====================
27+
28+Some functions (currently one :-) are available to ease generation of scenario
29+lists for common situations.
30+
31+Testing Per Implementation Module
32++++++++++++++++++++++++++++++++++
33+
34+It is reasonably common to have multiple Python modules that provide the same
35+capabilities and interface, and to want apply the same tests to all of them.
36+
37+In some cases, not all of the statically defined implementations will be able
38+to be used in a particular testing environment. For example, there may be both
39+a C and a pure-Python implementation of a module. You want to test the C
40+module if it can be loaded, but also to have the tests pass if the C module has
41+not been compiled.
42+
43+The ``per_module_scenarios`` function generates a scenario for each named
44+module, omitting those that raise an ``ImportError``. For each test object,
45+the implementation to be tested is installed into a given attribute.
46+
47+Note that for the test to be valid, all access to the module under test must go
48+through the relevant attribute of the test object. If one of the
49+implementations is also directly imported by the test module or any other,
50+testscenarios will not magically stop it being used.
51+
52+
53+
54 Advice on Writing Scenarios
55 ===========================
56
57
58=== modified file 'lib/testscenarios/scenarios.py'
59--- lib/testscenarios/scenarios.py 2010-10-12 06:57:23 +0000
60+++ lib/testscenarios/scenarios.py 2011-07-08 22:26:01 +0000
61@@ -2,7 +2,11 @@
62 # dependency injection ('scenarios') by tests.
63 #
64 # Copyright (c) 2009, Robert Collins <robertc@robertcollins.net>
65+<<<<<<< TREE
66 # Copyright (c) 2010 Martin Pool <mbp@sourcefrog.net>
67+=======
68+# Copyright (c) 2010, Martin Pool <mbp@sourcefrog.net>
69+>>>>>>> MERGE-SOURCE
70 #
71 # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
72 # license at the users choice. A copy of both licenses are available in the
73@@ -83,6 +87,7 @@
74 yield newtest
75 else:
76 yield test
77+<<<<<<< TREE
78
79
80 def load_tests_apply_scenarios(*params):
81@@ -129,3 +134,39 @@
82 scenario_parameters.update(parameter)
83 result.append((scenario_name, scenario_parameters))
84 return result
85+=======
86+
87+
88+def per_module_scenarios(attribute_name, modules):
89+ """Generate scenarios for available implementation modules.
90+
91+ This is typically used when there is a subsystem implemented, for
92+ example, in both Python and C, and we want to apply the same tests to
93+ both, but the C module may sometimes not be available.
94+
95+ Note: if the module can't be loaded, it's silently omitted from
96+ testing.
97+
98+ :param attribute_name: A name to be set in the scenario parameter
99+ dictionary (and thence onto the test instance) pointing to the
100+ implementation module for this scenario.
101+
102+ :param modules: An iterable of (short_name, module_name), where
103+ the short name is something like 'python' to put in the
104+ scenario name, and the long name is a fully-qualified Python module
105+ name.
106+ """
107+ scenarios = []
108+ for short_name, module_name in modules:
109+ try:
110+ mod = __import__(module_name, {}, {}, [''])
111+ except ImportError:
112+ # TODO: optionally pass this back through a callback, so it can be
113+ # logged etc?
114+ pass
115+ else:
116+ scenarios.append((
117+ short_name,
118+ {attribute_name: mod}))
119+ return scenarios
120+>>>>>>> MERGE-SOURCE
121
122=== modified file 'lib/testscenarios/tests/test_scenarios.py'
123--- lib/testscenarios/tests/test_scenarios.py 2010-10-12 06:57:23 +0000
124+++ lib/testscenarios/tests/test_scenarios.py 2011-07-08 22:26:01 +0000
125@@ -2,7 +2,11 @@
126 # dependency injection ('scenarios') by tests.
127 #
128 # Copyright (c) 2009, Robert Collins <robertc@robertcollins.net>
129+<<<<<<< TREE
130 # Copyright (c) 2010 Martin Pool <mbp@sourcefrog.net>
131+=======
132+# Copyright (c) 2010, Martin Pool <mbp@sourcefrog.net>
133+>>>>>>> MERGE-SOURCE
134 #
135 # Licensed under either the Apache License, Version 2.0 or the BSD 3-clause
136 # license at the users choice. A copy of both licenses are available in the
137@@ -174,6 +178,7 @@
138 tests = list(apply_scenarios(ReferenceTest.scenarios, test))
139 self.assertEqual([('demo', {})], ReferenceTest.scenarios)
140 self.assertEqual(ReferenceTest.scenarios, tests[0].scenarios)
141+<<<<<<< TREE
142
143
144 class TestLoadTests(testtools.TestCase):
145@@ -239,3 +244,21 @@
146 self.assertEqual(
147 'a,a,a,a',
148 scenarios[0][0])
149+=======
150+
151+
152+class TestPerModuleScenarios(testtools.TestCase):
153+
154+ def test_per_module_scenarios(self):
155+ """Generate scenarios for available modules"""
156+ s = testscenarios.scenarios.per_module_scenarios(
157+ 'the_module', [
158+ ('Python', 'testscenarios'),
159+ ('unittest', 'unittest'),
160+ ('nonexistent', 'nonexistent'),
161+ ])
162+ self.assertEqual(s, [
163+ ('Python', {'the_module': testscenarios}),
164+ ('unittest', {'the_module': unittest}),
165+ ])
166+>>>>>>> MERGE-SOURCE

Subscribers

People subscribed via source and target branches