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