diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2015-02-07 10:53:13 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2015-02-07 10:53:13 -0500 |
commit | c5144455d0f571c002678d369ec32707c6304e1f (patch) | |
tree | dca4cabafa20065a08d071d93d904a932bb7bbbb | |
parent | d6e2d5c1034a7cc0e2bfac251bfe5b4ec8c73036 (diff) | |
download | python-coveragepy-c5144455d0f571c002678d369ec32707c6304e1f.tar.gz |
Fully embrace SkipTest with our own method.
Also, no test is conditionally defined. They call self.skip if they
should be skippped.
-rw-r--r-- | coverage/test_helpers.py | 5 | ||||
-rw-r--r-- | tests/coveragetest.py | 6 | ||||
-rw-r--r-- | tests/test_arcs.py | 45 | ||||
-rw-r--r-- | tests/test_concurrency.py | 4 | ||||
-rw-r--r-- | tests/test_coverage.py | 28 | ||||
-rw-r--r-- | tests/test_phystokens.py | 4 | ||||
-rw-r--r-- | tests/test_plugins.py | 3 | ||||
-rw-r--r-- | tests/test_process.py | 160 | ||||
-rw-r--r-- | tests/test_summary.py | 46 |
9 files changed, 156 insertions, 145 deletions
diff --git a/coverage/test_helpers.py b/coverage/test_helpers.py index b49e75d..a4c73d3 100644 --- a/coverage/test_helpers.py +++ b/coverage/test_helpers.py @@ -231,6 +231,7 @@ class TempDirMixin(SysPathAwareMixin, ModuleAwareMixin, TestCase): """A value object to store per-class.""" def __init__(self): self.tests = 0 + self.skipped = 0 self.temp_dir = True self.tests_making_files = 0 self.test_method_made_any_files = False @@ -242,7 +243,9 @@ class TempDirMixin(SysPathAwareMixin, ModuleAwareMixin, TestCase): def report_on_class_behavior(cls): """Called at process exit to report on class behavior.""" for test_class, behavior in cls.class_behaviors.items(): - if behavior.temp_dir and behavior.tests_making_files == 0: + if behavior.tests == behavior.skipped: + bad = "" + elif behavior.temp_dir and behavior.tests_making_files == 0: bad = "Inefficient" elif not behavior.temp_dir and behavior.tests_making_files > 0: bad = "Unsafe" diff --git a/tests/coveragetest.py b/tests/coveragetest.py index 23de2ec..e1f42bb 100644 --- a/tests/coveragetest.py +++ b/tests/coveragetest.py @@ -10,6 +10,8 @@ from coverage.test_helpers import ( EnvironmentAwareMixin, StdStreamCapturingMixin, TempDirMixin, ) +from nose.plugins.skip import SkipTest + from tests.backtest import run_command @@ -39,6 +41,10 @@ class CoverageTest( self.__class__.__name__, self._testMethodName, )) + def skip(self, reason): + self.class_behavior().skipped += 1 + raise SkipTest(reason) + def clean_local_file_imports(self): """Clean up the results of calls to `import_local_file`. diff --git a/tests/test_arcs.py b/tests/test_arcs.py index 92dac5d..d3717a8 100644 --- a/tests/test_arcs.py +++ b/tests/test_arcs.py @@ -503,29 +503,30 @@ class ExceptionArcTest(CoverageTest): arcz=".1 12 23 35 56 61 17 7.", arcz_missing="", arcz_unpredicted="") - # Run this test only on Py2 for now. I hope to fix it on Py3 - # eventually... - if env.PY2: - # "except Exception as e" is crucial here. - def test_bug_212(self): - self.check_coverage("""\ - def b(exc): - try: - while 1: - raise Exception(exc) # 4 - except Exception as e: - if exc != 'expected': - raise - q = 8 - - b('expected') + # "except Exception as e" is crucial here. + def test_bug_212(self): + # Run this test only on Py2 for now. I hope to fix it on Py3 + # eventually... + if env.PY3: + self.skip("This doesn't work on Python 3") + self.check_coverage("""\ + def b(exc): try: - b('unexpected') # C - except: - pass - """, - arcz=".1 .2 1A 23 34 56 67 68 8. AB BC C. DE E.", - arcz_missing="C.", arcz_unpredicted="45 7. CD") + while 1: + raise Exception(exc) # 4 + except Exception as e: + if exc != 'expected': + raise + q = 8 + + b('expected') + try: + b('unexpected') # C + except: + pass + """, + arcz=".1 .2 1A 23 34 56 67 68 8. AB BC C. DE E.", + arcz_missing="C.", arcz_unpredicted="45 7. CD") def test_except_finally(self): self.check_coverage("""\ diff --git a/tests/test_concurrency.py b/tests/test_concurrency.py index b7c1364..9a82a0c 100644 --- a/tests/test_concurrency.py +++ b/tests/test_concurrency.py @@ -4,8 +4,6 @@ import os import os.path import threading -from nose.plugins.skip import SkipTest - import coverage from coverage import env @@ -230,7 +228,7 @@ class MultiprocessingTest(CoverageTest): # Currently, this doesn't work on Windows, something about pickling # the monkey-patched Process class? if env.WINDOWS: - raise SkipTest + self.skip("Multiprocessing support doesn't work on Windows") def test_multiprocessing(self): self.make_file("multi.py", """\ diff --git a/tests/test_coverage.py b/tests/test_coverage.py index 21f9154..35c7c25 100644 --- a/tests/test_coverage.py +++ b/tests/test_coverage.py @@ -306,19 +306,21 @@ class SimpleStatementTest(CoverageTest): """, [1,2,3,6,9], "") - if env.PY2: # Print statement is gone in Py3k. - def test_print(self): - self.check_coverage("""\ - print "hello, world!" - print ("hey: %d" % - 17) - print "goodbye" - print "hello, world!", - print ("hey: %d" % - 17), - print "goodbye", - """, - [1,2,4,5,6,8], "") + def test_print(self): + if env.PY3: # Print statement is gone in Py3k. + self.skip("No more print statement in Python 3.") + + self.check_coverage("""\ + print "hello, world!" + print ("hey: %d" % + 17) + print "goodbye" + print "hello, world!", + print ("hey: %d" % + 17), + print "goodbye", + """, + [1,2,4,5,6,8], "") def test_raise(self): self.check_coverage("""\ diff --git a/tests/test_phystokens.py b/tests/test_phystokens.py index 74a5220..7edd6aa 100644 --- a/tests/test_phystokens.py +++ b/tests/test_phystokens.py @@ -3,8 +3,6 @@ import os.path import re -from nose.plugins.skip import SkipTest - from coverage import env from coverage.phystokens import source_token_lines, source_encoding @@ -114,7 +112,7 @@ class SourceEncodingTest(CoverageTest): if env.PYPY and env.PY3: # PyPy3 gets this case wrong. Not sure what I can do about it, # so skip the test. - raise SkipTest + self.skip("PyPy3 is wrong about non-comment encoding. Skip it.") # Should not detect anything here source = b'def parse(src, encoding=None):\n pass' self.assertEqual(source_encoding(source), DEF_ENCODING) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 9bb8632..48b0498 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -9,7 +9,6 @@ from coverage.control import Plugins import coverage.plugin -from nose.plugins.skip import SkipTest from tests.coveragetest import CoverageTest @@ -202,7 +201,7 @@ class FileTracerTest(CoverageTest): def setUp(self): super(FileTracerTest, self).setUp() if env.C_TRACER: - raise SkipTest("Need Python tracer for plugin tests") + self.skip("Need Python tracer for plugin tests") def test_plugin1(self): self.make_file("simple.py", """\ diff --git a/tests/test_process.py b/tests/test_process.py index 3dec597..5ab6ba6 100644 --- a/tests/test_process.py +++ b/tests/test_process.py @@ -6,8 +6,6 @@ import os.path import sys import textwrap -from nose.plugins.skip import SkipTest - import coverage from coverage import env @@ -397,56 +395,59 @@ class ProcessTest(CoverageTest): out_py = self.run_command("python -m sub.run_me") self.assertMultiLineEqual(out_cov, out_py) - if sys.version_info >= (2, 7): - # Coverage isn't bug-for-bug compatible in the behavior of -m for - # Pythons < 2.7 - def test_coverage_run_dashm_is_like_python_dashm_with__main__207(self): - # https://bitbucket.org/ned/coveragepy/issue/207 - self.make_file("package/__init__.py", "print('init')") - self.make_file("package/__main__.py", "print('main')") - out_cov = self.run_command("coverage run -m package") - out_py = self.run_command("python -m package") - self.assertMultiLineEqual(out_cov, out_py) - - if hasattr(os, 'fork'): - def test_fork(self): - self.make_file("fork.py", """\ - import os - - def child(): - print('Child!') - - def main(): - ret = os.fork() - - if ret == 0: - child() - else: - os.waitpid(ret, 0) - - main() - """) - - out = self.run_command("coverage run -p fork.py") - self.assertEqual(out, 'Child!\n') - self.assert_doesnt_exist(".coverage") - - # After running the forking program, there should be two - # .coverage.machine.123 files. - self.assertEqual(self.number_of_data_files(), 2) - - # Combine the parallel coverage data files into .coverage . - self.run_command("coverage combine") - self.assert_exists(".coverage") - - # After combining, there should be only the .coverage file. - self.assertEqual(self.number_of_data_files(), 1) - - # Read the coverage file and see that b_or_c.py has all 7 lines - # executed. - data = coverage.CoverageData() - data.read_file(".coverage") - self.assertEqual(data.summary()['fork.py'], 9) + def test_coverage_run_dashm_is_like_python_dashm_with__main__207(self): + if sys.version_info < (2, 7): + # Coverage isn't bug-for-bug compatible in the behavior of -m for + # Pythons < 2.7 + self.skip("-m doesn't work the same < Python 2.7") + # https://bitbucket.org/ned/coveragepy/issue/207 + self.make_file("package/__init__.py", "print('init')") + self.make_file("package/__main__.py", "print('main')") + out_cov = self.run_command("coverage run -m package") + out_py = self.run_command("python -m package") + self.assertMultiLineEqual(out_cov, out_py) + + def test_fork(self): + if not hasattr(os, 'fork'): + self.skip("Can't test os.fork since it doesn't exist.") + + self.make_file("fork.py", """\ + import os + + def child(): + print('Child!') + + def main(): + ret = os.fork() + + if ret == 0: + child() + else: + os.waitpid(ret, 0) + + main() + """) + + out = self.run_command("coverage run -p fork.py") + self.assertEqual(out, 'Child!\n') + self.assert_doesnt_exist(".coverage") + + # After running the forking program, there should be two + # .coverage.machine.123 files. + self.assertEqual(self.number_of_data_files(), 2) + + # Combine the parallel coverage data files into .coverage . + self.run_command("coverage combine") + self.assert_exists(".coverage") + + # After combining, there should be only the .coverage file. + self.assertEqual(self.number_of_data_files(), 1) + + # Read the coverage file and see that b_or_c.py has all 7 lines + # executed. + data = coverage.CoverageData() + data.read_file(".coverage") + self.assertEqual(data.summary()['fork.py'], 9) def test_warnings(self): self.make_file("hello.py", """\ @@ -530,31 +531,34 @@ class ProcessTest(CoverageTest): self.assertIn("Trace function changed", out) - if env.PY3: # This only works on 3.x for now. + def test_fullcoverage(self): # pragma: not covered + if env.PY2: # This doesn't work on Python 2. + self.skip("fullcoverage doesn't work on Python 2.") # It only works with the C tracer, and if we aren't measuring ourselves. - if env.C_TRACER and not env.METACOV: # pragma: not covered - def test_fullcoverage(self): - # fullcoverage is a trick to get stdlib modules measured from - # the very beginning of the process. Here we import os and - # then check how many lines are measured. - self.make_file("getenv.py", """\ - import os - print("FOOEY == %s" % os.getenv("FOOEY")) - """) - - fullcov = os.path.join( - os.path.dirname(coverage.__file__), "fullcoverage" - ) - self.set_environ("FOOEY", "BOO") - self.set_environ("PYTHONPATH", fullcov) - out = self.run_command("python -m coverage run -L getenv.py") - self.assertEqual(out, "FOOEY == BOO\n") - data = coverage.CoverageData() - data.read_file(".coverage") - # The actual number of executed lines in os.py when it's - # imported is 120 or so. Just running os.getenv executes - # about 5. - self.assertGreater(data.summary()['os.py'], 50) + if not env.C_TRACER or env.METACOV: + self.skip("fullcoverage only works with the C tracer.") + + # fullcoverage is a trick to get stdlib modules measured from + # the very beginning of the process. Here we import os and + # then check how many lines are measured. + self.make_file("getenv.py", """\ + import os + print("FOOEY == %s" % os.getenv("FOOEY")) + """) + + fullcov = os.path.join( + os.path.dirname(coverage.__file__), "fullcoverage" + ) + self.set_environ("FOOEY", "BOO") + self.set_environ("PYTHONPATH", fullcov) + out = self.run_command("python -m coverage run -L getenv.py") + self.assertEqual(out, "FOOEY == BOO\n") + data = coverage.CoverageData() + data.read_file(".coverage") + # The actual number of executed lines in os.py when it's + # imported is 120 or so. Just running os.getenv executes + # about 5. + self.assertGreater(data.summary()['os.py'], 50) def test_deprecation_warnings(self): # Test that coverage doesn't trigger deprecation warnings. @@ -745,7 +749,7 @@ class ProcessStartupTest(ProcessCoverageMixin, CoverageTest): def test_subprocess_with_pth_files(self): # pragma: not covered if env.METACOV: - raise SkipTest( + self.skip( "Can't test sub-process pth file suppport during metacoverage" ) @@ -795,7 +799,7 @@ class ProcessStartupWithSourceTest(ProcessCoverageMixin, CoverageTest): self, dashm, package, source ): # pragma: not covered if env.METACOV: - raise SkipTest( + self.skip( "Can't test sub-process pth file suppport during metacoverage" ) diff --git a/tests/test_summary.py b/tests/test_summary.py index 6448216..878ae89 100644 --- a/tests/test_summary.py +++ b/tests/test_summary.py @@ -7,8 +7,6 @@ import py_compile import re import sys -from nose.plugins.skip import SkipTest - import coverage from coverage import env from coverage.backward import StringIO @@ -427,26 +425,28 @@ class SummaryTest(CoverageTest): self.assertIn("TheCode", report) self.assertNotIn("thecode", report) - if env.WINDOWS: - def test_pyw_files(self): - # https://bitbucket.org/ned/coveragepy/issue/261 - self.make_file("start.pyw", """\ - import mod - print("In start.pyw") - """) - self.make_file("mod.pyw", """\ - print("In mod.pyw") - """) - cov = coverage.coverage() - cov.start() - import start # pragma: nested # pylint: disable=import-error,unused-variable - cov.stop() # pragma: nested - - report = self.get_report(cov) - self.assertNotIn("NoSource", report) - report = report.splitlines() - self.assertIn("start.pyw 2 0 100%", report) - self.assertIn("mod.pyw 1 0 100%", report) + def test_pyw_files(self): + if not env.WINDOWS: + self.skip(".pyw files are only on Windows.") + + # https://bitbucket.org/ned/coveragepy/issue/261 + self.make_file("start.pyw", """\ + import mod + print("In start.pyw") + """) + self.make_file("mod.pyw", """\ + print("In mod.pyw") + """) + cov = coverage.coverage() + cov.start() + import start # pragma: nested # pylint: disable=import-error,unused-variable + cov.stop() # pragma: nested + + report = self.get_report(cov) + self.assertNotIn("NoSource", report) + report = report.splitlines() + self.assertIn("start.pyw 2 0 100%", report) + self.assertIn("mod.pyw 1 0 100%", report) def test_tracing_pyc_file(self): # Create two Python files. @@ -468,7 +468,7 @@ class SummaryTest(CoverageTest): def test_missing_py_file_during_run(self): # PyPy2 doesn't run bare .pyc files. if env.PYPY and env.PY2: - raise SkipTest("PyPy2 doesn't run bare .pyc files") + self.skip("PyPy2 doesn't run bare .pyc files") # Create two Python files. self.make_file("mod.py", "a = 1\n") |