[py-svn] r33000 - in py/dist/py/test: terminal testing

arigo at codespeak.net arigo at codespeak.net
Sun Oct 8 15:42:12 CEST 2006


Author: arigo
Date: Sun Oct  8 15:42:09 2006
New Revision: 33000

Modified:
   py/dist/py/test/terminal/terminal.py
   py/dist/py/test/testing/test_session.py
Log:
(pedronis, arigo)

Change triggered by us getting very confused when the 'E' was not located
on the expected line.  Usually it doesn't matter much, because the 'E' is
on the last line and the last line is part of the statement that failed.
Sometimes, though, the guessing-of-statement-range gets confused, and
more source is printed.  For this case let's at least put the 'E' on the
correct line...


Modified: py/dist/py/test/terminal/terminal.py
==============================================================================
--- py/dist/py/test/terminal/terminal.py	(original)
+++ py/dist/py/test/terminal/terminal.py	Sun Oct  8 15:42:09 2006
@@ -294,11 +294,13 @@
             else: 
                 self.out.line("")
             source = self.getentrysource(entry)
+            firstsourceline = entry.getfirstlinesource()
+            marker_location = entry.lineno - firstsourceline
             if entry == last: 
-                self.repr_source(source, 'E')
+                self.repr_source(source, 'E', marker_location)
                 self.repr_failure_explanation(excinfo, source) 
             else:
-                self.repr_source(source, '>') 
+                self.repr_source(source, '>', marker_location)
             self.out.line("") 
             self.out.line("[%s:%d]" %(entry.frame.code.path, entry.lineno+1))  
             self.repr_locals(entry) 
@@ -387,11 +389,19 @@
             source = py.code.Source("[failure to get at sourcelines from %r]\n" % entry)
         return source.deindent()
 
-    def repr_source(self, source, marker=">"):
-        for line in source[:-1]: 
-            self.out.line("    " + line) 
-        lastline = source[-1]
-        self.out.line(marker + "   " + lastline) 
+    def repr_source(self, source, marker=">", marker_location=-1):
+        if marker_location < 0:
+            marker_location += len(source)
+            if marker_location < 0:
+                marker_location = 0
+        if marker_location >= len(source):
+            marker_location = len(source) - 1
+        for i in range(len(source)):
+            if i == marker_location:
+                prefix = marker + "   "
+            else:
+                prefix = "    "
+            self.out.line(prefix + source[i])
 
     def repr_failure_explanation(self, excinfo, source): 
         try: 

Modified: py/dist/py/test/testing/test_session.py
==============================================================================
--- py/dist/py/test/testing/test_session.py	(original)
+++ py/dist/py/test/testing/test_session.py	Sun Oct  8 15:42:09 2006
@@ -306,6 +306,31 @@
         assert out.find("""[Exception("Ha Ha fooled you, I'm a broken repr().") raised in repr()]""") != -1 #'
         assert out.find("[unknown exception raised in repr()]") != -1
 
+    def test_E_on_correct_line(self):
+        o = tmpdir.ensure('E_on_correct_line', dir=1)
+        tfile = o.join('test_correct_line.py')
+        source = py.code.Source("""
+            import py
+            def test_hello():
+                assert (None ==
+                        ['a',
+                         'b',
+                         'c'])
+        """)
+        tfile.write(source)
+        session = self.session
+        session.main([o])
+        out = self.file.getvalue()
+        print 'Output of simulated "py.test test_correct_line.py":'
+        print out
+        i = out.find('test_correct_line.py:')
+        assert i >= 0
+        linenum = int(out[i+len('test_correct_line.py:')])  # a single char
+        line_to_report = source[linenum-1]
+        expected_output = '\nE   ' + line_to_report + '\n'
+        print 'Looking for:', expected_output
+        assert expected_output in out
+
 
 from py.__.test.terminal.remote import getrootdir 
 class TestRemote: 



More information about the pytest-commit mailing list