Merge lp:~javier.collado/mago/unittest-trick into lp:mago

Proposed by Javier Collado
Status: Merged
Merged at revision: 23
Proposed branch: lp:~javier.collado/mago/unittest-trick
Merge into: lp:mago
Diff against target: 46 lines (+15/-11)
2 files modified
mago/__init__.py (+14/-0)
mago/case.py (+1/-11)
To merge this branch: bzr merge lp:~javier.collado/mago/unittest-trick
Reviewer Review Type Date Requested Status
James Tatum Approve
Review via email: mp+50158@code.launchpad.net

Description of the change

Right no the mago.case module checks the python interpreter version and imports unittest2 as unittest when it's lower than 2.7.

While this solution works well for this module, there are other modules that might use unittest (mago.core does) and also all test cases (for example, the ones in lp:~mago-contributors/mago/mago-testsuite).

Since it would be too verbose to add the same kind of check to every module and test that needs unittest, this branch takes another approach. It does the check the first thing when mago module is imported and then sets sys.modules['unittest'] to the right module. This way, there's no need for more checks and the right module is imported everywhere (as long as reload isn't used).

To post a comment you must log in.
Revision history for this message
James Tatum (jtatum) wrote :

That's clever!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'mago/__init__.py'
2--- mago/__init__.py 2011-01-07 08:22:59 +0000
3+++ mago/__init__.py 2011-02-17 14:32:54 +0000
4@@ -16,6 +16,20 @@
5
6 """Testing framework extensions for desktop testing"""
7
8+import sys
9+if sys.version_info < (2, 7):
10+ try:
11+ import unittest2 as unittest
12+ except ImportError:
13+ print """unittest2 is required. Install the package python-unittest2.\nExiting..."""
14+ sys.exit(1)
15+else:
16+ import unittest
17+
18+# Make sure that any other modules
19+# that import unittest will get the right version
20+sys.modules['unittest'] = unittest
21+
22 from mago.core import main, TestResult, TextTestRunner, magoConfig
23 from mago.case import TestCase
24 from mago.config import get_config
25
26=== modified file 'mago/case.py'
27--- mago/case.py 2011-02-05 22:48:24 +0000
28+++ mago/case.py 2011-02-17 14:32:54 +0000
29@@ -15,17 +15,7 @@
30 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31
32 """Main TestCase class"""
33-
34-import sys
35-if sys.version_info < (2, 7):
36- try:
37- import unittest2 as unittest
38- except ImportError:
39- print """unittest2 is required. Install the package python-unittest2.\nExiting..."""
40- sys.exit(1)
41-else:
42- import unittest
43-
44+import unittest
45 from mago.application import TestApplication
46 from mago.config import get_config
47

Subscribers

People subscribed via source and target branches