[Numpy-svn] r5289 - in trunk: . numpy/testing

numpy-svn@scip... numpy-svn@scip...
Mon Jun 16 21:17:39 CDT 2008


Author: alan.mcintyre
Date: 2008-06-16 21:17:34 -0500 (Mon, 16 Jun 2008)
New Revision: 5289

Modified:
   trunk/README.txt
   trunk/numpy/testing/__init__.py
   trunk/numpy/testing/decorators.py
   trunk/numpy/testing/nosetester.py
   trunk/numpy/testing/pkgtester.py
   trunk/numpy/testing/utils.py
Log:
Update README.txt to indicate nose version dependency, and port SciPy r4424 to NumPy 
(prevent import of nose until actual execution of tests).  Restored 
"raises" function to numpy/testing/utils.py until it can be replaced with the function of 
the same name from nose.tools after the lazy import.


Modified: trunk/README.txt
===================================================================
--- trunk/README.txt	2008-06-17 01:11:43 UTC (rev 5288)
+++ trunk/README.txt	2008-06-17 02:17:34 UTC (rev 5289)
@@ -13,8 +13,9 @@
 
 python -c 'import numpy; numpy.test()'
 
-Please note that you must have the 'nose' test framework installed in order to
-run the tests.  More information about nose is available here: 
+Please note that you must have version 0.10 or later of the 'nose' test
+framework installed in order to run the tests.  More information about nose is
+available here: 
 
 http://somethingaboutorange.com/mrl/projects/nose/ 
 

Modified: trunk/numpy/testing/__init__.py
===================================================================
--- trunk/numpy/testing/__init__.py	2008-06-17 01:11:43 UTC (rev 5288)
+++ trunk/numpy/testing/__init__.py	2008-06-17 02:17:34 UTC (rev 5289)
@@ -10,14 +10,6 @@
 
 import decorators as dec
 from utils import *
-
-try:
-    import nose
-    from nose.tools import raises
-except ImportError:
-    pass
-
 from numpytest import *
-
 from pkgtester import Tester
 test = Tester().test

Modified: trunk/numpy/testing/decorators.py
===================================================================
--- trunk/numpy/testing/decorators.py	2008-06-17 01:11:43 UTC (rev 5288)
+++ trunk/numpy/testing/decorators.py	2008-06-17 02:17:34 UTC (rev 5289)
@@ -10,11 +10,6 @@
 
 """
 
-try:
-    import nose
-except ImportError:
-    pass
-
 def slow(t):
     """Labels a test as 'slow'.
 
@@ -76,6 +71,9 @@
     if msg is None:
         msg = 'Test skipped due to test condition'
     def skip_decorator(f):
+        # Local import to avoid a hard nose dependency and only incur the 
+        # import time overhead at actual test-time. 
+        import nose
         def skipper(*args, **kwargs):
             if skip_condition:
                 raise nose.SkipTest, msg
@@ -87,6 +85,9 @@
 def skipknownfailure(f):
     ''' Decorator to raise SkipTest for test known to fail
     '''
+    # Local import to avoid a hard nose dependency and only incur the 
+    # import time overhead at actual test-time. 
+    import nose
     def skipper(*args, **kwargs):
         raise nose.SkipTest, 'This test is known to fail'
     return nose.tools.make_decorator(f)(skipper)

Modified: trunk/numpy/testing/nosetester.py
===================================================================
--- trunk/numpy/testing/nosetester.py	2008-06-17 01:11:43 UTC (rev 5288)
+++ trunk/numpy/testing/nosetester.py	2008-06-17 02:17:34 UTC (rev 5289)
@@ -7,8 +7,26 @@
 import sys
 import re
 
-import nose
+def import_nose():
+    """ Import nose only when needed.
+    """
+    fine_nose = True
+    try:
+        import nose
+        from nose.tools import raises
+    except ImportError:
+        fine_nose = False
+    else:
+        nose_version = nose.__versioninfo__
+        if nose_version[0] < 1 and nose_version[1] < 10:
+            fine_nose = False
 
+    if not fine_nose:
+        raise ImportError('Need nose >=0.10 for tests - see '
+            'http://somethingaboutorange.com/mrl/projects/nose')
+
+    return nose
+
 class NoseTester(object):
     """ Nose test runner.
 
@@ -113,6 +131,7 @@
         doctests : boolean
             If True, run doctests in module, default False
         '''
+        nose = import_nose()
         argv = self._test_argv(label, verbose, extra_argv)
         if doctests:
             argv+=['--with-doctest','--doctest-tests']
@@ -135,6 +154,7 @@
         ''' Run benchmarks for module using nose
 
         %(test_header)s'''
+        nose = import_nose()
         argv = self._test_argv(label, verbose, extra_argv)
         argv += ['--match', r'(?:^|[\\b_\\.%s-])[Bb]ench' % os.sep]
         nose.run(argv=argv)

Modified: trunk/numpy/testing/pkgtester.py
===================================================================
--- trunk/numpy/testing/pkgtester.py	2008-06-17 01:11:43 UTC (rev 5288)
+++ trunk/numpy/testing/pkgtester.py	2008-06-17 02:17:34 UTC (rev 5289)
@@ -11,17 +11,4 @@
 See nosetester module for test implementation
 
 '''
-fine_nose = True
-try:
-    import nose
-except ImportError:
-    fine_nose = False
-else:
-    nose_version = nose.__versioninfo__
-    if nose_version[0] < 1 and nose_version[1] < 10:
-        fine_nose = False
-
-if fine_nose:
-    from numpy.testing.nosetester import NoseTester as Tester
-else:
-    from numpy.testing.nulltester import NullTester as Tester
+from numpy.testing.nosetester import NoseTester as Tester

Modified: trunk/numpy/testing/utils.py
===================================================================
--- trunk/numpy/testing/utils.py	2008-06-17 01:11:43 UTC (rev 5288)
+++ trunk/numpy/testing/utils.py	2008-06-17 02:17:34 UTC (rev 5289)
@@ -11,7 +11,7 @@
 __all__ = ['assert_equal', 'assert_almost_equal','assert_approx_equal',
            'assert_array_equal', 'assert_array_less', 'assert_string_equal',
            'assert_array_almost_equal', 'build_err_msg', 'jiffies', 'memusage',
-           'rand', 'rundocs', 'runstring']
+           'raises', 'rand', 'rundocs', 'runstring']
 
 def rand(*args):
     """Returns an array of random numbers with the given shape.
@@ -317,3 +317,34 @@
         for test in tests:
             runner.run(test)
     return
+
+
+def raises(*exceptions):
+    """ Assert that a test function raises one of the specified exceptions to
+    pass.
+    """
+    # FIXME: when we transition to nose, just use its implementation. It's
+    # better.
+    def deco(function):
+        def f2(*args, **kwds):
+            try:
+                function(*args, **kwds)
+            except exceptions:
+                pass
+            except:
+                # Anything else.
+                raise
+            else:
+                raise AssertionError('%s() did not raise one of (%s)' %
+                    (function.__name__, ', '.join([e.__name__ for e in exceptions])))
+        try:
+            f2.__name__ = function.__name__
+        except TypeError:
+            # Python 2.3 does not permit this.
+            pass
+        f2.__dict__ = function.__dict__
+        f2.__doc__ = function.__doc__
+        f2.__module__ = function.__module__
+        return f2
+
+    return deco



More information about the Numpy-svn mailing list