##// END OF EJS Templates
Commiting fixes for running our test suite using trial and nose....
Brian Granger -
Show More
@@ -0,0 +1,132 b''
1 # encoding: utf-8
2 """
3 Testing related decorators for use with twisted.trial.
4
5 The decorators in this files are designed to follow the same API as those
6 in the decorators module (in this same directory). But they can be used
7 with twisted.trial
8 """
9
10 #-----------------------------------------------------------------------------
11 # Copyright (C) 2008-2009 The IPython Development Team
12 #
13 # Distributed under the terms of the BSD License. The full license is in
14 # the file COPYING, distributed as part of this software.
15 #-----------------------------------------------------------------------------
16
17 #-----------------------------------------------------------------------------
18 # Imports
19 #-----------------------------------------------------------------------------
20
21 import os
22 import sys
23
24 from IPython.testing.decorators import make_label_dec
25
26 #-----------------------------------------------------------------------------
27 # Testing decorators
28 #-----------------------------------------------------------------------------
29
30
31 def skipif(skip_condition, msg=None):
32 """Create a decorator that marks a test function for skipping.
33
34 The is a decorator factory that returns a decorator that will
35 conditionally skip a test based on the value of skip_condition. The
36 skip_condition argument can either be a boolean or a callable that returns
37 a boolean.
38
39 Parameters
40 ----------
41 skip_condition : boolean or callable
42 If this evaluates to True, the test is skipped.
43 msg : str
44 The message to print if the test is skipped.
45
46 Returns
47 -------
48 decorator : function
49 The decorator function that can be applied to the test function.
50 """
51
52 def skip_decorator(f):
53
54 # Allow for both boolean or callable skip conditions.
55 if callable(skip_condition):
56 skip_val = lambda : skip_condition()
57 else:
58 skip_val = lambda : skip_condition
59
60 if msg is None:
61 out = 'Test skipped due to test condition.'
62 else:
63 out = msg
64 final_msg = "Skipping test: %s. %s" % (f.__name__,out)
65
66 if skip_val():
67 f.skip = final_msg
68
69 return f
70 return skip_decorator
71
72
73 def skip(msg=None):
74 """Create a decorator that marks a test function for skipping.
75
76 This is a decorator factory that returns a decorator that will cause
77 tests to be skipped.
78
79 Parameters
80 ----------
81 msg : str
82 Optional message to be added.
83
84 Returns
85 -------
86 decorator : function
87 Decorator, which, when applied to a function, sets the skip
88 attribute of the function causing `twisted.trial` to skip it.
89 """
90
91 return skipif(True,msg)
92
93
94 def numpy_not_available():
95 """Can numpy be imported? Returns true if numpy does NOT import.
96
97 This is used to make a decorator to skip tests that require numpy to be
98 available, but delay the 'import numpy' to test execution time.
99 """
100 try:
101 import numpy
102 np_not_avail = False
103 except ImportError:
104 np_not_avail = True
105
106 return np_not_avail
107
108 #-----------------------------------------------------------------------------
109 # Decorators for public use
110 #-----------------------------------------------------------------------------
111
112 # Decorators to skip certain tests on specific platforms.
113 skip_win32 = skipif(sys.platform == 'win32',
114 "This test does not run under Windows")
115 skip_linux = skipif(sys.platform == 'linux2',
116 "This test does not run under Linux")
117 skip_osx = skipif(sys.platform == 'darwin',"This test does not run under OS X")
118
119 # Decorators to skip tests if not on specific platforms.
120 skip_if_not_win32 = skipif(sys.platform != 'win32',
121 "This test only runs under Windows")
122 skip_if_not_linux = skipif(sys.platform != 'linux2',
123 "This test only runs under Linux")
124 skip_if_not_osx = skipif(sys.platform != 'darwin',
125 "This test only runs under OSX")
126
127 # Other skip decorators
128 skipif_not_numpy = skipif(numpy_not_available,"This test requires numpy")
129
130 skipknownfailure = skip('This test is known to fail')
131
132
@@ -0,0 +1,52 b''
1 # encoding: utf-8
2 """
3 Tests for decorators_trial.py
4 """
5
6 #-----------------------------------------------------------------------------
7 # Copyright (C) 2008-2009 The IPython Development Team
8 #
9 # Distributed under the terms of the BSD License. The full license is in
10 # the file COPYING, distributed as part of this software.
11 #-----------------------------------------------------------------------------
12
13 #-----------------------------------------------------------------------------
14 # Imports
15 #-----------------------------------------------------------------------------
16
17 # Tell nose to skip this module
18 __test__ = {}
19
20 import os
21 import sys
22
23 from twisted.trial import unittest
24 import IPython.testing.decorators_trial as dec
25
26 #-----------------------------------------------------------------------------
27 # Tests
28 #-----------------------------------------------------------------------------
29
30 class TestDecoratorsTrial(unittest.TestCase):
31
32 @dec.skip()
33 def test_deliberately_broken(self):
34 """A deliberately broken test - we want to skip this one."""
35 1/0
36
37 @dec.skip('Testing the skip decorator')
38 def test_deliberately_broken2(self):
39 """Another deliberately broken test - we want to skip this one."""
40 1/0
41
42 @dec.skip_linux
43 def test_linux(self):
44 self.assertNotEquals(sys.platform,'linux2',"This test can't run under linux")
45
46 @dec.skip_win32
47 def test_win32(self):
48 self.assertNotEquals(sys.platform,'win32',"This test can't run under windows")
49
50 @dec.skip_osx
51 def test_osx(self):
52 self.assertNotEquals(sys.platform,'darwin',"This test can't run under osx") No newline at end of file
@@ -2,29 +2,27 b''
2 2
3 3 """This file contains unittests for the interpreter.py module."""
4 4
5 __docformat__ = "restructuredtext en"
6
7 5 #-----------------------------------------------------------------------------
8 # Copyright (C) 2008-2009 The IPython Development Team
9 #
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
6 # Copyright (C) 2008-2009 The IPython Development Team
7 #
8 # Distributed under the terms of the BSD License. The full license is
9 # in the file COPYING, distributed as part of this software.
12 10 #-----------------------------------------------------------------------------
13 11
14 12 #-----------------------------------------------------------------------------
15 # Imports
13 # Imports
16 14 #-----------------------------------------------------------------------------
17 15
18 import unittest
16 # Tell nose to skip this module
17 __test__ = {}
18
19 from twisted.trial import unittest
19 20 from IPython.kernel.core.interpreter import Interpreter
20 21
21 22 #-----------------------------------------------------------------------------
22 # Tests
23 # Tests
23 24 #-----------------------------------------------------------------------------
24 25
25 # Tell nose to skip this module
26 __test__ = {}
27
28 26 class TestInterpreter(unittest.TestCase):
29 27
30 28 def test_unicode(self):
@@ -2,29 +2,26 b''
2 2
3 3 """This file contains unittests for the notification.py module."""
4 4
5 __docformat__ = "restructuredtext en"
6
7 5 #-----------------------------------------------------------------------------
8 # Copyright (C) 2008 The IPython Development Team
9 #
10 # Distributed under the terms of the BSD License. The full license is in
11 # the file COPYING, distributed as part of this software.
6 # Copyright (C) 2008-2009 The IPython Development Team
7 #
8 # Distributed under the terms of the BSD License. The full license is
9 # in the file COPYING, distributed as part of this software.
12 10 #-----------------------------------------------------------------------------
13
11
14 12 #-----------------------------------------------------------------------------
15 # Imports
13 # Imports
16 14 #-----------------------------------------------------------------------------
17 15
18 16 # Tell nose to skip this module
19 17 __test__ = {}
20 18
21 import unittest
19 from twisted.trial import unittest
22 20 import IPython.kernel.core.notification as notification
23 from nose.tools import timed
24 21
25 #
26 # Supporting test classes
27 #
22 #-----------------------------------------------------------------------------
23 # Support Classes
24 #-----------------------------------------------------------------------------
28 25
29 26 class Observer(object):
30 27 """docstring for Observer"""
@@ -39,7 +36,6 b' class Observer(object):'
39 36 self.expectedType,
40 37 self.expectedSender)
41 38
42
43 39 def callback(self, theType, sender, args={}):
44 40 """callback"""
45 41
@@ -50,7 +46,6 b' class Observer(object):'
50 46 assert(args == self.expectedKwArgs)
51 47 self.recieved = True
52 48
53
54 49 def verify(self):
55 50 """verify"""
56 51
@@ -60,7 +55,6 b' class Observer(object):'
60 55 """reset"""
61 56
62 57 self.recieved = False
63
64 58
65 59
66 60 class Notifier(object):
@@ -75,11 +69,10 b' class Notifier(object):'
75 69
76 70 center.post_notification(self.theType, self,
77 71 **self.kwargs)
78
79 72
80 #
81 # Test Cases
82 #
73 #-----------------------------------------------------------------------------
74 # Tests
75 #-----------------------------------------------------------------------------
83 76
84 77 class NotificationTests(unittest.TestCase):
85 78 """docstring for NotificationTests"""
@@ -97,7 +90,6 b' class NotificationTests(unittest.TestCase):'
97 90
98 91 observer.verify()
99 92
100
101 93 def test_type_specificity(self):
102 94 """Test that observers are registered by type"""
103 95
@@ -112,7 +104,6 b' class NotificationTests(unittest.TestCase):'
112 104
113 105 observer.verify()
114 106
115
116 107 def test_sender_specificity(self):
117 108 """Test that observers are registered by sender"""
118 109
@@ -126,7 +117,6 b' class NotificationTests(unittest.TestCase):'
126 117
127 118 observer.verify()
128 119
129
130 120 def test_remove_all_observers(self):
131 121 """White-box test for remove_all_observers"""
132 122
@@ -139,8 +129,7 b' class NotificationTests(unittest.TestCase):'
139 129 notification.sharedCenter.remove_all_observers()
140 130
141 131 self.assert_(len(notification.sharedCenter.observers) == 0, "observers removed")
142
143
132
144 133 def test_any_sender(self):
145 134 """test_any_sender"""
146 135
@@ -156,9 +145,7 b' class NotificationTests(unittest.TestCase):'
156 145 observer.reset()
157 146 sender2.post()
158 147 observer.verify()
159
160
161 @timed(.01)
148
162 149 def test_post_performance(self):
163 150 """Test that post_notification, even with many registered irrelevant
164 151 observers is fast"""
@@ -171,4 +158,4 b' class NotificationTests(unittest.TestCase):'
171 158 notification.sharedCenter.post_notification('EXPECTED_TYPE', self)
172 159
173 160 o.verify()
174
161
@@ -2,69 +2,77 b''
2 2 """
3 3 Test the output capture at the OS level, using file descriptors.
4 4 """
5
6 __docformat__ = "restructuredtext en"
7
8 #-------------------------------------------------------------------------------
9 # Copyright (C) 2008 The IPython Development Team
5 #-----------------------------------------------------------------------------
6 # Copyright (C) 2008-2009 The IPython Development Team
10 7 #
11 8 # Distributed under the terms of the BSD License. The full license is
12 9 # in the file COPYING, distributed as part of this software.
13 #-------------------------------------------------------------------------------
10 #-----------------------------------------------------------------------------
11
12 #-----------------------------------------------------------------------------
13 # Imports
14 #-----------------------------------------------------------------------------
14 15
15 16 # Tell nose to skip this module
16 17 __test__ = {}
17 18
18 import os
19 19 from cStringIO import StringIO
20 import os
21
22 from twisted.trial import unittest
20 23
21 from IPython.testing import decorators as dec
24 from IPython.testing import decorators_trial as dec
22 25
23 26 #-----------------------------------------------------------------------------
24 # Test functions
27 # Tests
28 #-----------------------------------------------------------------------------
25 29
26 @dec.skip_win32
27 def test_redirector():
28 """ Checks that the redirector can be used to do synchronous capture.
29 """
30 from IPython.kernel.core.fd_redirector import FDRedirector
31 r = FDRedirector()
32 out = StringIO()
33 try:
34 r.start()
35 for i in range(10):
36 os.system('echo %ic' % i)
37 print >>out, r.getvalue(),
38 print >>out, i
39 except:
40 r.stop()
41 raise
42 r.stop()
43 result1 = out.getvalue()
44 result2 = "".join("%ic\n%i\n" %(i, i) for i in range(10))
45 assert result1 == result2
46 30
31 class TestRedirector(unittest.TestCase):
47 32
48 @dec.skip_win32
49 def test_redirector_output_trap():
50 """ This test check not only that the redirector_output_trap does
33 @dec.skip_win32
34 def test_redirector(self):
35 """Checks that the redirector can be used to do synchronous capture.
36 """
37 from IPython.kernel.core.fd_redirector import FDRedirector
38 r = FDRedirector()
39 out = StringIO()
40 try:
41 r.start()
42 for i in range(10):
43 os.system('echo %ic' % i)
44 print >>out, r.getvalue(),
45 print >>out, i
46 except:
47 r.stop()
48 raise
49 r.stop()
50 result1 = out.getvalue()
51 result2 = "".join("%ic\n%i\n" %(i, i) for i in range(10))
52 self.assertEquals(result1, result2)
53
54 @dec.skip_win32
55 def test_redirector_output_trap(self):
56 """Check the greedy trapping behavior of the traps.
57
58 This test check not only that the redirector_output_trap does
51 59 trap the output, but also that it does it in a gready way, that
52 60 is by calling the callback ASAP.
53 """
54 from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap
55 out = StringIO()
56 trap = RedirectorOutputTrap(out.write, out.write)
57 try:
58 trap.set()
59 for i in range(10):
60 os.system('echo %ic' % i)
61 print "%ip" % i
62 print >>out, i
63 except:
61 """
62 from IPython.kernel.core.redirector_output_trap import RedirectorOutputTrap
63 out = StringIO()
64 trap = RedirectorOutputTrap(out.write, out.write)
65 try:
66 trap.set()
67 for i in range(10):
68 os.system('echo %ic' % i)
69 print "%ip" % i
70 print >>out, i
71 except:
72 trap.unset()
73 raise
64 74 trap.unset()
65 raise
66 trap.unset()
67 result1 = out.getvalue()
68 result2 = "".join("%ic\n%ip\n%i\n" %(i, i, i) for i in range(10))
69 assert result1 == result2
70
75 result1 = out.getvalue()
76 result2 = "".join("%ic\n%ip\n%i\n" %(i, i, i) for i in range(10))
77 self.assertEquals(result1, result2)
78
@@ -46,7 +46,7 b' def test_deliberately_broken():'
46 46 """A deliberately broken test - we want to skip this one."""
47 47 1/0
48 48
49 @dec.skip('foo')
49 @dec.skip('Testing the skip decorator')
50 50 def test_deliberately_broken2():
51 51 """Another deliberately broken test - we want to skip this one."""
52 52 1/0
General Comments 0
You need to be logged in to leave comments. Login now