Commits

holger krekel committed a8b625d Merge

Merged in adamgoucher/pytest (pull request #29)

stdout/stderr now captured by junitxml

Comments (0)

Files changed (3)

_pytest/capture.py

         if funcarg_outerr is not None:
             outerr = (outerr[0] + funcarg_outerr[0],
                       outerr[1] + funcarg_outerr[1])
-        if not rep.passed:
-            addouterr(rep, outerr)
+        addouterr(rep, outerr)
         if not rep.passed or rep.when == "teardown":
             outerr = ('', '')
         item.outerr = outerr

_pytest/junitxml.py

             time=getattr(report, 'duration', 0)
         ))
 
+    def _write_captured_output(self, report):
+        sec = dict(report.sections)
+        for name in ('out', 'err'):
+            content = sec.get("Captured std%s" % name)
+            if content:
+                tag = getattr(Junit, 'system-'+name)
+                self.append(tag(bin_xml_escape(content)))
+
     def append(self, obj):
         self.tests[-1].append(obj)
 
     def append_pass(self, report):
         self.passed += 1
+        self._write_captured_output(report)
 
     def append_failure(self, report):
         #msg = str(report.longrepr.reprtraceback.extraline)
                 Junit.skipped(message="xfail-marked test passes unexpectedly"))
             self.skipped += 1
         else:
-            sec = dict(report.sections)
             fail = Junit.failure(message="test failure")
             fail.append(str(report.longrepr))
             self.append(fail)
-            for name in ('out', 'err'):
-                content = sec.get("Captured std%s" % name)
-                if content:
-                    tag = getattr(Junit, 'system-'+name)
-                    self.append(tag(bin_xml_escape(content)))
             self.failed += 1
+        self._write_captured_output(report)
 
     def append_collect_failure(self, report):
         #msg = str(report.longrepr.reprtraceback.extraline)
                               message=skipreason
                 ))
         self.skipped += 1
+        self._write_captured_output(report)
 
     def pytest_runtest_logreport(self, report):
         if report.passed:

testing/test_junitxml.py

         if not sys.platform.startswith("java"):
             assert "hx" in fnode.toxml()
 
+    def test_pass_captures_stdout(self, testdir):
+        testdir.makepyfile("""
+            def test_pass():
+                print('hello-stdout')
+        """)
+        result, dom = runandparse(testdir)
+        node = dom.getElementsByTagName("testsuite")[0]
+        pnode = node.getElementsByTagName("testcase")[0]
+        systemout = pnode.getElementsByTagName("system-out")[0]
+        assert "hello-stdout" in systemout.toxml()
+
+    def test_pass_captures_stderr(self, testdir):
+        testdir.makepyfile("""
+            import sys
+            def test_pass():
+                sys.stderr.write('hello-stderr')
+        """)
+        result, dom = runandparse(testdir)
+        node = dom.getElementsByTagName("testsuite")[0]
+        pnode = node.getElementsByTagName("testcase")[0]
+        systemout = pnode.getElementsByTagName("system-err")[0]
+        assert "hello-stderr" in systemout.toxml()
+
 def test_mangle_testnames():
     from _pytest.junitxml import mangle_testnames
     names = ["a/pything.py", "Class", "()", "method"]
     newnames = mangle_testnames(names)
     assert newnames == ["a.pything", "Class", "method"]
 
-
 def test_dont_configure_on_slaves(tmpdir):
     gotten = []
     class FakeConfig:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.