Commits

Ned Batchelder  committed b62f839

Split out the report tests, to give them their due.

  • Participants
  • Parent commits 7480801

Comments (0)

Files changed (2)

File test/test_coverage.py

 # Copyright 2004-2009, Ned Batchelder
 # http://nedbatchelder.com/code/coverage
 
-import os, re, sys, unittest
+import os, sys, unittest
 
 import coverage
 coverage.use_cache(0)
         self.assert_(os.path.exists(".coverage"))
         self.assertEqual(out, 'done\n')
     
-    def testReport(self):
-        self.make_file("mycode.py", """\
-            import covmod1
-            import covmodzip1
-            a = 1
-            print ('done')
-            """)
-
-        out = self.run_command("coverage -x mycode.py")
-        self.assertEqual(out, 'done\n')
-        report1 = self.run_command("coverage -r").replace('\\', '/')
-
-        # Name                                              Stmts   Exec  Cover
-        # ---------------------------------------------------------------------
-        # c:/ned/coverage/trunk/test/modules/covmod1            2      2   100%
-        # c:/ned/coverage/trunk/test/zipmods.zip/covmodzip1     2      2   100%
-        # mycode                                                4      4   100%
-        # ---------------------------------------------------------------------
-        # TOTAL                                                 8      8   100%
-
-        self.assert_("error" not in report1.lower())
-        self.assert_("/coverage/__init__/" not in report1)
-        self.assert_("/test/modules/covmod1 " in report1)
-        self.assert_("/test/zipmods.zip/covmodzip1 " in report1)
-        self.assert_("mycode " in report1)
-        last_line = report1.split('\n')[-2]
-        self.assertEqual(re.sub(r"\s+", " ", last_line), "TOTAL 8 8 100%")
-
-        for l in report1.split('\n'):
-            if '/test/modules/covmod1' in l:
-                # Save a module prefix for the omit test later.
-                prefix = l.split('/test/')[0] + '/test/'
-                break
-
-        # Try reporting just one module
-        report2 = self.run_command("coverage -r mycode.py").replace('\\', '/')
-
-        # Name     Stmts   Exec  Cover
-        # ----------------------------
-        # mycode       4      4   100%
-
-        self.assert_("error" not in report2.lower())
-        self.assert_("/coverage/" not in report2)
-        self.assert_("/test/modules/covmod1 " not in report2)
-        self.assert_("/test/zipmods.zip/covmodzip1 " not in report2)
-        self.assert_("mycode " in report2)
-
-        # Try reporting while omitting some modules
-        report3 = self.run_command("coverage -r -o %s" % prefix)
-        report3 = report3.replace('\\', '/')
-
-        # Name     Stmts   Exec  Cover
-        # ----------------------------
-        # mycode       4      4   100%
-
-        self.assert_("error" not in report3.lower())
-        self.assert_("/coverage/" not in report3)
-        self.assert_("/test/modules/covmod1 " not in report3)
-        self.assert_("/test/zipmods.zip/covmodzip1 " not in report3)
-        self.assert_("mycode " in report3)
-
     def testCombineParallelData(self):
         self.make_file("b_or_c.py", """\
             import sys

File test/test_summary.py

+"""Test text-based summary reporting for coverage.py"""
+
+import os, re, sys
+
+sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
+from coveragetest import CoverageTest
+
+class SummaryTest(CoverageTest):
+    """Tests of the text summary reporting for coverage.py."""
+
+    def setUp(self):
+        super(SummaryTest, self).setUp()
+        self.make_file("mycode.py", """\
+            import covmod1
+            import covmodzip1
+            a = 1
+            print ('done')
+            """)
+
+    def report_from_command(self, cmd):
+        """Return the report from the `cmd`, with some convenience added."""
+        report = self.run_command(cmd).replace('\\', '/')
+        self.assert_("error" not in report.lower())
+        return report
+    
+    def line_count(self, report):
+        """How many lines are in `report`?"""
+        self.assertEqual(report.split('\n')[-1], "")
+        return len(report.split('\n')) - 1
+
+    def last_line_squeezed(self, report):
+        """Return the last line of `report` with the spaces squeezed down."""
+        last_line = report.split('\n')[-2]
+        return re.sub(r"\s+", " ", last_line)
+
+    def test_report(self):
+        out = self.run_command("coverage -x mycode.py")
+        self.assertEqual(out, 'done\n')
+        report1 = self.report_from_command("coverage -r")
+
+        # Name                                              Stmts   Exec  Cover
+        # ---------------------------------------------------------------------
+        # c:/ned/coverage/trunk/test/modules/covmod1            2      2   100%
+        # c:/ned/coverage/trunk/test/zipmods.zip/covmodzip1     2      2   100%
+        # mycode                                                4      4   100%
+        # ---------------------------------------------------------------------
+        # TOTAL                                                 8      8   100%
+
+        self.assert_("/coverage/__init__/" not in report1)
+        self.assert_("/test/modules/covmod1 " in report1)
+        self.assert_("/test/zipmods.zip/covmodzip1 " in report1)
+        self.assert_("mycode " in report1)
+        self.assertEqual(self.last_line_squeezed(report1), "TOTAL 8 8 100%")
+
+    def test_report_just_one(self):
+        # Try reporting just one module
+        self.run_command("coverage -x mycode.py")
+        report2 = self.report_from_command("coverage -r mycode.py")
+
+        # Name     Stmts   Exec  Cover
+        # ----------------------------
+        # mycode       4      4   100%
+
+        self.assertEqual(self.line_count(report2), 3)
+        self.assert_("/coverage/" not in report2)
+        self.assert_("/test/modules/covmod1 " not in report2)
+        self.assert_("/test/zipmods.zip/covmodzip1 " not in report2)
+        self.assert_("mycode " in report2)
+        self.assertEqual(self.last_line_squeezed(report2), "mycode 4 4 100%")
+
+    def test_report_omitting(self):
+        # Try reporting while omitting some modules
+        prefix = os.path.split(__file__)[0]
+        self.run_command("coverage -x mycode.py")
+        report3 = self.report_from_command("coverage -r -o %s" % prefix)
+        
+        # Name     Stmts   Exec  Cover
+        # ----------------------------
+        # mycode       4      4   100%
+
+        self.assertEqual(self.line_count(report3), 3)
+        self.assert_("/coverage/" not in report3)
+        self.assert_("/test/modules/covmod1 " not in report3)
+        self.assert_("/test/zipmods.zip/covmodzip1 " not in report3)
+        self.assert_("mycode " in report3)
+        self.assertEqual(self.last_line_squeezed(report3), "mycode 4 4 100%")