summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2011-03-24 22:49:37 -0400
committerNed Batchelder <ned@nedbatchelder.com>2011-03-24 22:49:37 -0400
commit01ea68a1088fc1cdbdd586624ac695c5b697edeb (patch)
tree7b64f69deaeda1fa07c38bfdb64c7c4d8e879eb3 /test
parent1f950ca6c4c1da6d5f2f37faeb0217e9ff273b75 (diff)
downloadpython-coveragepy-git-01ea68a1088fc1cdbdd586624ac695c5b697edeb.tar.gz
Incremental HTML generation. Some cleanup would be good.
Diffstat (limited to 'test')
-rw-r--r--test/coveragetest.py22
-rw-r--r--test/test_html.py121
-rw-r--r--test/test_misc.py28
3 files changed, 170 insertions, 1 deletions
diff --git a/test/coveragetest.py b/test/coveragetest.py
index 621d7ae2..93cffa86 100644
--- a/test/coveragetest.py
+++ b/test/coveragetest.py
@@ -1,6 +1,6 @@
"""Base test case class for coverage testing."""
-import imp, os, random, shlex, shutil, sys, tempfile, textwrap
+import glob, imp, os, random, shlex, shutil, sys, tempfile, textwrap
import coverage
from coverage.backward import sorted, StringIO # pylint: disable=W0622
@@ -83,6 +83,9 @@ class CoverageTest(TestCase):
sys.stdout = self.old_stdout
sys.stderr = self.old_stderr
+ self.clean_modules()
+
+ def clean_modules(self):
# Remove any new modules imported during the test run. This lets us
# import the same source files for more than one test.
for m in [m for m in sys.modules if m not in self.old_modules]:
@@ -155,6 +158,23 @@ class CoverageTest(TestCase):
return filename
+ def clean_local_file_imports(self):
+ """Clean up the results of calls to `import_local_file`.
+
+ Use this if you need to `import_local_file` the same file twice in
+ one test.
+
+ """
+ # So that we can re-import files, clean them out first.
+ self.clean_modules()
+ # Also have to clean out the .pyc file, since the timestamp
+ # resolution is only one second, a changed file might not be
+ # picked up.
+ for pyc in glob.glob('*.pyc'):
+ os.remove(pyc)
+ if os.path.exists("__pycache__"):
+ shutil.rmtree("__pycache__")
+
def import_local_file(self, modname):
"""Import a local file as a module.
diff --git a/test/test_html.py b/test/test_html.py
new file mode 100644
index 00000000..ad03f054
--- /dev/null
+++ b/test/test_html.py
@@ -0,0 +1,121 @@
+"""Tests that HTML generation is awesome."""
+
+import os.path, sys
+import coverage
+sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
+from coveragetest import CoverageTest
+
+class HtmlTest(CoverageTest):
+ """HTML!"""
+
+ def create_initial_files(self):
+ """Create the source files we need to run these tests."""
+ self.make_file("main_file.py", """\
+ import helper1, helper2
+ helper1.func1(12)
+ helper2.func2(12)
+ """)
+ self.make_file("helper1.py", """\
+ def func1(x):
+ if x % 2:
+ print("odd")
+ """)
+ self.make_file("helper2.py", """\
+ def func2(x):
+ print("x is %d" % x)
+ """)
+
+ def run_coverage(self, **kwargs):
+ """Run coverage on main_file.py, and create an HTML report."""
+ self.clean_local_file_imports()
+ cov = coverage.coverage(**kwargs)
+ cov.start()
+ self.import_local_file("main_file")
+ cov.stop()
+ cov.html_report()
+
+ def remove_html_files(self):
+ """Remove the HTML files created as part of the HTML report."""
+ os.remove("htmlcov/index.html")
+ os.remove("htmlcov/main_file.html")
+ os.remove("htmlcov/helper1.html")
+ os.remove("htmlcov/helper2.html")
+
+ def test_html_created(self):
+ # Test basic HTML generation: files should be created.
+ self.create_initial_files()
+ self.run_coverage()
+
+ self.assert_exists("htmlcov/index.html")
+ self.assert_exists("htmlcov/main_file.html")
+ self.assert_exists("htmlcov/helper1.html")
+ self.assert_exists("htmlcov/helper2.html")
+ self.assert_exists("htmlcov/style.css")
+ self.assert_exists("htmlcov/coverage_html.js")
+
+ def test_html_delta_from_source_change(self):
+ # HTML generation can create only the files that have changed.
+ # In this case, helper1 changes because its source is different.
+ self.create_initial_files()
+ self.run_coverage()
+ index1 = open("htmlcov/index.html").read()
+ self.remove_html_files()
+
+ # Now change a file and do it again
+ self.make_file("helper1.py", """\
+ def func1(x): # A nice function
+ if x % 2:
+ print("odd")
+ """)
+
+ self.run_coverage()
+
+ # Only the changed files should have been created.
+ self.assert_exists("htmlcov/index.html")
+ self.assert_exists("htmlcov/helper1.html")
+ self.assert_doesnt_exist("htmlcov/main_file.html")
+ self.assert_doesnt_exist("htmlcov/helper2.html")
+ index2 = open("htmlcov/index.html").read()
+ self.assertMultiLineEqual(index1, index2)
+
+ def test_html_delta_from_coverage_change(self):
+ # HTML generation can create only the files that have changed.
+ # In this case, helper1 changes because its coverage is different.
+ self.create_initial_files()
+ self.run_coverage()
+ self.remove_html_files()
+
+ # Now change a file and do it again
+ self.make_file("main_file.py", """\
+ import helper1, helper2
+ helper1.func1(23)
+ helper2.func2(23)
+ """)
+
+ self.run_coverage()
+
+ # Only the changed files should have been created.
+ self.assert_exists("htmlcov/index.html")
+ self.assert_exists("htmlcov/helper1.html")
+ self.assert_exists("htmlcov/main_file.html")
+ self.assert_doesnt_exist("htmlcov/helper2.html")
+
+ def test_html_delta_from_settings_change(self):
+ # HTML generation can create only the files that have changed.
+ # In this case, everything changes because the coverage settings have
+ # changed.
+ self.create_initial_files()
+ self.run_coverage()
+ index1 = open("htmlcov/index.html").read()
+ self.remove_html_files()
+
+ self.run_coverage(timid=True)
+
+ # Only the changed files should have been created.
+ self.assert_exists("htmlcov/index.html")
+ self.assert_exists("htmlcov/helper1.html")
+ self.assert_exists("htmlcov/main_file.html")
+ self.assert_exists("htmlcov/helper2.html")
+ index2 = open("htmlcov/index.html").read()
+ self.assertMultiLineEqual(index1, index2)
+
diff --git a/test/test_misc.py b/test/test_misc.py
new file mode 100644
index 00000000..72f5caac
--- /dev/null
+++ b/test/test_misc.py
@@ -0,0 +1,28 @@
+"""Tests of miscellaneous stuff."""
+
+import os, sys
+
+from coverage.misc import Hasher
+sys.path.insert(0, os.path.split(__file__)[0]) # Force relative import for Py3k
+from coveragetest import CoverageTest
+
+class HasherTest(CoverageTest):
+ """Test our wrapper of md5 hashing."""
+
+ def test_string_hashing(self):
+ h1 = Hasher()
+ h1.update("Hello, world!")
+ h2 = Hasher()
+ h2.update("Goodbye!")
+ h3 = Hasher()
+ h3.update("Hello, world!")
+ self.assertNotEqual(h1.digest(), h2.digest())
+ self.assertEqual(h1.digest(), h3.digest())
+
+ def test_dict_hashing(self):
+ h1 = Hasher()
+ h1.update({'a': 17, 'b': 23})
+ h2 = Hasher()
+ h2.update({'b': 23, 'a': 17})
+ self.assertEqual(h1.digest(), h2.digest())
+