##// END OF EJS Templates
Don't monkeypatch nose in Python 3, as it must be a newer version.
Thomas Kluyver -
Show More
@@ -1,68 +1,71 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 Note: merely importing this module causes the monkeypatch to be applied."""
7
7
8 #-----------------------------------------------------------------------------
8 #-----------------------------------------------------------------------------
9 # Copyright (C) 2009 The IPython Development Team
9 # Copyright (C) 2009 The IPython Development Team
10 #
10 #
11 # Distributed under the terms of the BSD License. The full license is in
11 # Distributed under the terms of the BSD License. The full license is in
12 # the file COPYING, distributed as part of this software.
12 # the file COPYING, distributed as part of this software.
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14
14
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 # Imports
16 # Imports
17 #-----------------------------------------------------------------------------
17 #-----------------------------------------------------------------------------
18
18
19 import unittest
19 import unittest
20 import sys
20 import nose.loader
21 import nose.loader
21 from inspect import ismethod, isfunction
22 from inspect import ismethod, isfunction
22
23
23 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
24 # Classes and functions
25 # Classes and functions
25 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
26
27
27 def getTestCaseNames(self, testCaseClass):
28 def getTestCaseNames(self, testCaseClass):
28 """Override to select with selector, unless
29 """Override to select with selector, unless
29 config.getTestCaseNamesCompat is True
30 config.getTestCaseNamesCompat is True
30 """
31 """
31 if self.config.getTestCaseNamesCompat:
32 if self.config.getTestCaseNamesCompat:
32 return unittest.TestLoader.getTestCaseNames(self, testCaseClass)
33 return unittest.TestLoader.getTestCaseNames(self, testCaseClass)
33
34
34 def wanted(attr, cls=testCaseClass, sel=self.selector):
35 def wanted(attr, cls=testCaseClass, sel=self.selector):
35 item = getattr(cls, attr, None)
36 item = getattr(cls, attr, None)
36 # MONKEYPATCH: replace this:
37 # MONKEYPATCH: replace this:
37 #if not ismethod(item):
38 #if not ismethod(item):
38 # return False
39 # return False
39 # return sel.wantMethod(item)
40 # return sel.wantMethod(item)
40 # With:
41 # With:
41 if ismethod(item):
42 if ismethod(item):
42 return sel.wantMethod(item)
43 return sel.wantMethod(item)
43 # static method or something. If this is a static method, we
44 # static method or something. If this is a static method, we
44 # can't get the class information, and we have to treat it
45 # can't get the class information, and we have to treat it
45 # as a function. Thus, we will miss things like class
46 # as a function. Thus, we will miss things like class
46 # attributes for test selection
47 # attributes for test selection
47 if isfunction(item):
48 if isfunction(item):
48 return sel.wantFunction(item)
49 return sel.wantFunction(item)
49 return False
50 return False
50 # END MONKEYPATCH
51 # END MONKEYPATCH
51
52
52 cases = filter(wanted, dir(testCaseClass))
53 cases = filter(wanted, dir(testCaseClass))
53 for base in testCaseClass.__bases__:
54 for base in testCaseClass.__bases__:
54 for case in self.getTestCaseNames(base):
55 for case in self.getTestCaseNames(base):
55 if case not in cases:
56 if case not in cases:
56 cases.append(case)
57 cases.append(case)
57 # add runTest if nothing else picked
58 # add runTest if nothing else picked
58 if not cases and hasattr(testCaseClass, 'runTest'):
59 if not cases and hasattr(testCaseClass, 'runTest'):
59 cases = ['runTest']
60 cases = ['runTest']
60 if self.sortTestMethodsUsing:
61 if self.sortTestMethodsUsing:
61 cases.sort(self.sortTestMethodsUsing)
62 cases.sort(self.sortTestMethodsUsing)
62 return cases
63 return cases
63
64
64
65
65 ##########################################################################
66 ##########################################################################
66 # Apply monkeypatch here
67 # Apply monkeypatch here
68 # Python 3 must be running with newer version of Nose, so don't touch anything.
69 if sys.version_info[0] < 3:
67 nose.loader.TestLoader.getTestCaseNames = getTestCaseNames
70 nose.loader.TestLoader.getTestCaseNames = getTestCaseNames
68 ##########################################################################
71 ##########################################################################
General Comments 0
You need to be logged in to leave comments. Login now