##// END OF EJS Templates
Small cleanups and documentation to testing support code....
Fernando Perez -
Show More
@@ -1,71 +1,76 b''
1 """Monkeypatch nose to accept any callable as a method.
1 """Monkeypatch nose to accept any callable as a method.
2
2
3 By default, nose's ismethod() fails for static methods.
3 By default, nose's ismethod() fails for static methods.
4 Once this is fixed in upstream nose we can disable it.
4 Once this is fixed in upstream nose we can disable it.
5
5
6 Note: merely importing this module causes the monkeypatch to be applied."""
6 Notes:
7
8 - As of Nose 1.0.0, the problem persists so this monkeypatch is still
9 needed.
10
11 - Merely importing this module causes the monkeypatch to be applied."""
7
12
8 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
9 # Copyright (C) 2009-2011 The IPython Development Team
14 # Copyright (C) 2009-2011 The IPython Development Team
10 #
15 #
11 # Distributed under the terms of the BSD License. The full license is in
16 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
17 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
18 #-----------------------------------------------------------------------------
14
19
15 #-----------------------------------------------------------------------------
20 #-----------------------------------------------------------------------------
16 # Imports
21 # Imports
17 #-----------------------------------------------------------------------------
22 #-----------------------------------------------------------------------------
18
23
19 import unittest
24 import unittest
20 import sys
25 import sys
21 import nose.loader
26 import nose.loader
22 from inspect import ismethod, isfunction
27 from inspect import ismethod, isfunction
23
28
24 #-----------------------------------------------------------------------------
29 #-----------------------------------------------------------------------------
25 # Classes and functions
30 # Classes and functions
26 #-----------------------------------------------------------------------------
31 #-----------------------------------------------------------------------------
27
32
28 def getTestCaseNames(self, testCaseClass):
33 def getTestCaseNames(self, testCaseClass):
29 """Override to select with selector, unless
34 """Override to select with selector, unless
30 config.getTestCaseNamesCompat is True
35 config.getTestCaseNamesCompat is True
31 """
36 """
32 if self.config.getTestCaseNamesCompat:
37 if self.config.getTestCaseNamesCompat:
33 return unittest.TestLoader.getTestCaseNames(self, testCaseClass)
38 return unittest.TestLoader.getTestCaseNames(self, testCaseClass)
34
39
35 def wanted(attr, cls=testCaseClass, sel=self.selector):
40 def wanted(attr, cls=testCaseClass, sel=self.selector):
36 item = getattr(cls, attr, None)
41 item = getattr(cls, attr, None)
37 # MONKEYPATCH: replace this:
42 # MONKEYPATCH: replace this:
38 #if not ismethod(item):
43 #if not ismethod(item):
39 # return False
44 # return False
40 # return sel.wantMethod(item)
45 # return sel.wantMethod(item)
41 # With:
46 # With:
42 if ismethod(item):
47 if ismethod(item):
43 return sel.wantMethod(item)
48 return sel.wantMethod(item)
44 # static method or something. If this is a static method, we
49 # static method or something. If this is a static method, we
45 # can't get the class information, and we have to treat it
50 # can't get the class information, and we have to treat it
46 # as a function. Thus, we will miss things like class
51 # as a function. Thus, we will miss things like class
47 # attributes for test selection
52 # attributes for test selection
48 if isfunction(item):
53 if isfunction(item):
49 return sel.wantFunction(item)
54 return sel.wantFunction(item)
50 return False
55 return False
51 # END MONKEYPATCH
56 # END MONKEYPATCH
52
57
53 cases = filter(wanted, dir(testCaseClass))
58 cases = filter(wanted, dir(testCaseClass))
54 for base in testCaseClass.__bases__:
59 for base in testCaseClass.__bases__:
55 for case in self.getTestCaseNames(base):
60 for case in self.getTestCaseNames(base):
56 if case not in cases:
61 if case not in cases:
57 cases.append(case)
62 cases.append(case)
58 # add runTest if nothing else picked
63 # add runTest if nothing else picked
59 if not cases and hasattr(testCaseClass, 'runTest'):
64 if not cases and hasattr(testCaseClass, 'runTest'):
60 cases = ['runTest']
65 cases = ['runTest']
61 if self.sortTestMethodsUsing:
66 if self.sortTestMethodsUsing:
62 cases.sort(self.sortTestMethodsUsing)
67 cases.sort(self.sortTestMethodsUsing)
63 return cases
68 return cases
64
69
65
70
66 ##########################################################################
71 ##########################################################################
67 # Apply monkeypatch here
72 # Apply monkeypatch here
68 # Python 3 must be running with newer version of Nose, so don't touch anything.
73 # Python 3 must be running with newer version of Nose, so don't touch anything.
69 if sys.version_info[0] < 3:
74 if sys.version_info[0] < 3:
70 nose.loader.TestLoader.getTestCaseNames = getTestCaseNames
75 nose.loader.TestLoader.getTestCaseNames = getTestCaseNames
71 ##########################################################################
76 ##########################################################################
@@ -1,21 +1,38 b''
1 """This decorator marks that a doctest should be skipped.
1 """Decorators marks that a doctest should be skipped, for both python 2 and 3.
2
2
3 The IPython.testing.decorators module triggers various extra imports, including
3 The IPython.testing.decorators module triggers various extra imports, including
4 numpy and sympy if they're present. Since this decorator is used in core parts
4 numpy and sympy if they're present. Since this decorator is used in core parts
5 of IPython, it's in a separate module so that running IPython doesn't trigger
5 of IPython, it's in a separate module so that running IPython doesn't trigger
6 those imports."""
6 those imports."""
7
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2009-2011 The IPython Development Team
10 #
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
14
15 #-----------------------------------------------------------------------------
16 # Imports
17 #-----------------------------------------------------------------------------
18
7 import sys
19 import sys
8
20
21 #-----------------------------------------------------------------------------
22 # Decorators
23 #-----------------------------------------------------------------------------
24
9 def skip_doctest(f):
25 def skip_doctest(f):
10 """Decorator - mark a function or method for skipping its doctest.
26 """Decorator - mark a function or method for skipping its doctest.
11
27
12 This decorator allows you to mark a function whose docstring you wish to
28 This decorator allows you to mark a function whose docstring you wish to
13 omit from testing, while preserving the docstring for introspection, help,
29 omit from testing, while preserving the docstring for introspection, help,
14 etc."""
30 etc."""
15 f.skip_doctest = True
31 f.skip_doctest = True
16 return f
32 return f
17
33
34
18 def skip_doctest_py3(f):
35 def skip_doctest_py3(f):
19 """Decorator - skip the doctest under Python 3."""
36 """Decorator - skip the doctest under Python 3."""
20 f.skip_doctest = (sys.version_info[0] >= 3)
37 f.skip_doctest = (sys.version_info[0] >= 3)
21 return f
38 return f
General Comments 0
You need to be logged in to leave comments. Login now