##// END OF EJS Templates
Fix for skipping tests when matplotlib not available.
Thomas Kluyver -
Show More
@@ -1,118 +1,118
1 """Test suite for pylab_import_all magic
1 """Test suite for pylab_import_all magic
2 Modified from the irunner module but using regex.
2 Modified from the irunner module but using regex.
3 """
3 """
4
4
5 # Global to make tests extra verbose and help debugging
5 # Global to make tests extra verbose and help debugging
6 VERBOSE = True
6 VERBOSE = True
7
7
8 # stdlib imports
8 # stdlib imports
9 import StringIO
9 import StringIO
10 import sys
10 import sys
11 import unittest
11 import unittest
12 import re
12 import re
13
13
14 # IPython imports
14 # IPython imports
15 from IPython.lib import irunner
15 from IPython.lib import irunner
16 from IPython.testing import decorators
16 from IPython.testing import decorators
17
17
18 def pylab_not_importable():
18 def pylab_not_importable():
19 """Test if importing pylab fails with RuntimeError (true when having no display)"""
19 """Test if importing pylab fails with RuntimeError (true when having no display)"""
20 try:
20 try:
21 import pylab
21 import pylab
22 return False
22 return False
23 except RuntimeError:
23 except RuntimeError:
24 return True
24 return True
25
25
26 # Testing code begins
26 # Testing code begins
27 class RunnerTestCase(unittest.TestCase):
27 class RunnerTestCase(unittest.TestCase):
28
28
29 def setUp(self):
29 def setUp(self):
30 self.out = StringIO.StringIO()
30 self.out = StringIO.StringIO()
31 #self.out = sys.stdout
31 #self.out = sys.stdout
32
32
33 def _test_runner(self,runner,source,output):
33 def _test_runner(self,runner,source,output):
34 """Test that a given runner's input/output match."""
34 """Test that a given runner's input/output match."""
35
35
36 runner.run_source(source)
36 runner.run_source(source)
37 out = self.out.getvalue()
37 out = self.out.getvalue()
38 #out = ''
38 #out = ''
39 # this output contains nasty \r\n lineends, and the initial ipython
39 # this output contains nasty \r\n lineends, and the initial ipython
40 # banner. clean it up for comparison, removing lines of whitespace
40 # banner. clean it up for comparison, removing lines of whitespace
41 output_l = [l for l in output.splitlines() if l and not l.isspace()]
41 output_l = [l for l in output.splitlines() if l and not l.isspace()]
42 out_l = [l for l in out.splitlines() if l and not l.isspace()]
42 out_l = [l for l in out.splitlines() if l and not l.isspace()]
43 mismatch = 0
43 mismatch = 0
44 if len(output_l) != len(out_l):
44 if len(output_l) != len(out_l):
45 message = ("Mismatch in number of lines\n\n"
45 message = ("Mismatch in number of lines\n\n"
46 "Expected:\n"
46 "Expected:\n"
47 "~~~~~~~~~\n"
47 "~~~~~~~~~\n"
48 "%s\n\n"
48 "%s\n\n"
49 "Got:\n"
49 "Got:\n"
50 "~~~~~~~~~\n"
50 "~~~~~~~~~\n"
51 "%s"
51 "%s"
52 ) % ("\n".join(output_l), "\n".join(out_l))
52 ) % ("\n".join(output_l), "\n".join(out_l))
53 self.fail(message)
53 self.fail(message)
54 for n in range(len(output_l)):
54 for n in range(len(output_l)):
55 # Do a line-by-line comparison
55 # Do a line-by-line comparison
56 ol1 = output_l[n].strip()
56 ol1 = output_l[n].strip()
57 ol2 = out_l[n].strip()
57 ol2 = out_l[n].strip()
58 if not re.match(ol1,ol2):
58 if not re.match(ol1,ol2):
59 mismatch += 1
59 mismatch += 1
60 if VERBOSE:
60 if VERBOSE:
61 print '<<< line %s does not match:' % n
61 print '<<< line %s does not match:' % n
62 print repr(ol1)
62 print repr(ol1)
63 print repr(ol2)
63 print repr(ol2)
64 print '>>>'
64 print '>>>'
65 self.assert_(mismatch==0,'Number of mismatched lines: %s' %
65 self.assert_(mismatch==0,'Number of mismatched lines: %s' %
66 mismatch)
66 mismatch)
67
67
68 @decorators.skipif(pylab_not_importable, "Likely a run without X.")
69 @decorators.skipif_not_matplotlib
68 @decorators.skipif_not_matplotlib
69 @decorators.skipif(pylab_not_importable, "Likely a run without X.")
70 def test_pylab_import_all_enabled(self):
70 def test_pylab_import_all_enabled(self):
71 "Verify that plot is available when pylab_import_all = True"
71 "Verify that plot is available when pylab_import_all = True"
72 source = """
72 source = """
73 from IPython.config.application import Application
73 from IPython.config.application import Application
74 app = Application.instance()
74 app = Application.instance()
75 app.pylab_import_all = True
75 app.pylab_import_all = True
76 pylab
76 pylab
77 ip=get_ipython()
77 ip=get_ipython()
78 'plot' in ip.user_ns
78 'plot' in ip.user_ns
79 """
79 """
80 output = """
80 output = """
81 In \[1\]: from IPython\.config\.application import Application
81 In \[1\]: from IPython\.config\.application import Application
82 In \[2\]: app = Application\.instance\(\)
82 In \[2\]: app = Application\.instance\(\)
83 In \[3\]: app\.pylab_import_all = True
83 In \[3\]: app\.pylab_import_all = True
84 In \[4\]: pylab
84 In \[4\]: pylab
85 ^Welcome to pylab, a matplotlib-based Python environment
85 ^Welcome to pylab, a matplotlib-based Python environment
86 For more information, type 'help\(pylab\)'\.
86 For more information, type 'help\(pylab\)'\.
87 In \[5\]: ip=get_ipython\(\)
87 In \[5\]: ip=get_ipython\(\)
88 In \[6\]: \'plot\' in ip\.user_ns
88 In \[6\]: \'plot\' in ip\.user_ns
89 Out\[6\]: True
89 Out\[6\]: True
90 """
90 """
91 runner = irunner.IPythonRunner(out=self.out)
91 runner = irunner.IPythonRunner(out=self.out)
92 self._test_runner(runner,source,output)
92 self._test_runner(runner,source,output)
93
93
94 @decorators.skipif(pylab_not_importable, "Likely a run without X.")
95 @decorators.skipif_not_matplotlib
94 @decorators.skipif_not_matplotlib
95 @decorators.skipif(pylab_not_importable, "Likely a run without X.")
96 def test_pylab_import_all_disabled(self):
96 def test_pylab_import_all_disabled(self):
97 "Verify that plot is not available when pylab_import_all = False"
97 "Verify that plot is not available when pylab_import_all = False"
98 source = """
98 source = """
99 from IPython.config.application import Application
99 from IPython.config.application import Application
100 app = Application.instance()
100 app = Application.instance()
101 app.pylab_import_all = False
101 app.pylab_import_all = False
102 pylab
102 pylab
103 ip=get_ipython()
103 ip=get_ipython()
104 'plot' in ip.user_ns
104 'plot' in ip.user_ns
105 """
105 """
106 output = """
106 output = """
107 In \[1\]: from IPython\.config\.application import Application
107 In \[1\]: from IPython\.config\.application import Application
108 In \[2\]: app = Application\.instance\(\)
108 In \[2\]: app = Application\.instance\(\)
109 In \[3\]: app\.pylab_import_all = False
109 In \[3\]: app\.pylab_import_all = False
110 In \[4\]: pylab
110 In \[4\]: pylab
111 ^Welcome to pylab, a matplotlib-based Python environment
111 ^Welcome to pylab, a matplotlib-based Python environment
112 For more information, type 'help\(pylab\)'\.
112 For more information, type 'help\(pylab\)'\.
113 In \[5\]: ip=get_ipython\(\)
113 In \[5\]: ip=get_ipython\(\)
114 In \[6\]: \'plot\' in ip\.user_ns
114 In \[6\]: \'plot\' in ip\.user_ns
115 Out\[6\]: False
115 Out\[6\]: False
116 """
116 """
117 runner = irunner.IPythonRunner(out=self.out)
117 runner = irunner.IPythonRunner(out=self.out)
118 self._test_runner(runner,source,output)
118 self._test_runner(runner,source,output)
General Comments 0
You need to be logged in to leave comments. Login now