summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2017-05-04 07:35:24 -0400
committerNed Batchelder <ned@nedbatchelder.com>2017-05-04 07:35:24 -0400
commite062672509aea7cc7fb302c5f086a26ec6840989 (patch)
treedb74b7802bc409a810e2956913b5749f411090cf
parent52d0e79e5cba53c5d248601cd666ff2954bd1d27 (diff)
downloadpython-coveragepy-e062672509aea7cc7fb302c5f086a26ec6840989.tar.gz
Refactor common test infrastructure for fiddling sys.path and finding files
-rw-r--r--tests/coveragetest.py14
-rw-r--r--tests/test_api.py22
-rw-r--r--tests/test_config.py13
-rw-r--r--tests/test_execfile.py12
-rw-r--r--tests/test_filereporter.py11
-rw-r--r--tests/test_phystokens.py11
-rw-r--r--tests/test_summary.py31
7 files changed, 38 insertions, 76 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py
index 0e6131f..4d9540d 100644
--- a/tests/coveragetest.py
+++ b/tests/coveragetest.py
@@ -31,6 +31,9 @@ from tests.helpers import run_command, SuperModuleCleaner
# Status returns for the command line.
OK, ERR = 0, 1
+# The coverage/tests directory, for all sorts of finding test helping things.
+TESTS_DIR = os.path.dirname(__file__)
+
def convert_skip_exceptions(method):
"""A decorator for test methods to convert StopEverything to SkipTest."""
@@ -490,6 +493,17 @@ class CoverageTest(
return self.squeezed_lines(report)[-1]
+class UsingModulesMixin(object):
+ """A mixin for importing modules from tests/modules and tests/moremodules."""
+
+ def setUp(self):
+ super(UsingModulesMixin, self).setUp()
+
+ # Parent class saves and restores sys.path, we can just modify it.
+ sys.path.append(self.nice_file(TESTS_DIR, 'modules'))
+ sys.path.append(self.nice_file(TESTS_DIR, 'moremodules'))
+
+
def command_line(args, **kwargs):
"""Run `args` through the CoverageScript command line.
diff --git a/tests/test_api.py b/tests/test_api.py
index 9a3fc82..90f9654 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -15,7 +15,7 @@ from coverage.backward import StringIO, import_local_file
from coverage.misc import CoverageException
from coverage.report import Reporter
-from tests.coveragetest import CoverageTest, CoverageTestMethodsMixin
+from tests.coveragetest import CoverageTest, CoverageTestMethodsMixin, TESTS_DIR, UsingModulesMixin
class ApiTest(CoverageTest):
@@ -464,24 +464,11 @@ class NamespaceModuleTest(CoverageTest):
cov.analysis(sys.modules['namespace'])
-class UsingModulesMixin(object):
- """A mixin for importing modules from test/modules and test/moremodules."""
-
- run_in_temp_dir = False
-
- def setUp(self):
- super(UsingModulesMixin, self).setUp()
-
- self.chdir(self.nice_file(os.path.dirname(__file__), 'modules'))
-
- # Parent class saves and restores sys.path, we can just modify it.
- sys.path.append(".")
- sys.path.append("../moremodules")
-
-
class OmitIncludeTestsMixin(UsingModulesMixin, CoverageTestMethodsMixin):
"""Test methods for coverage methods taking include and omit."""
+ run_in_temp_dir = False
+
def filenames_in(self, summary, filenames):
"""Assert the `filenames` are in the keys of `summary`."""
for filename in filenames.split():
@@ -579,6 +566,9 @@ class SourceOmitIncludeTest(OmitIncludeTestsMixin, CoverageTest):
# Used to be if you omitted something executed and inside the source,
# then after it was executed but not recorded, it would be found in
# the search for unexecuted files, and given a score of 0%.
+
+ # The omit arg is by path, so need to be in the modules directory.
+ self.chdir(self.nice_file(TESTS_DIR, 'modules'))
lines = self.coverage_usepkgs(source=["pkg1"], omit=["pkg1/p1b.py"])
self.filenames_in(lines, "p1a")
self.filenames_not_in(lines, "p1b")
diff --git a/tests/test_config.py b/tests/test_config.py
index 9224046..9a4e8a5 100644
--- a/tests/test_config.py
+++ b/tests/test_config.py
@@ -4,13 +4,10 @@
"""Test the config file handling for coverage.py"""
-import sys
-import os
-
import coverage
from coverage.misc import CoverageException
-from tests.coveragetest import CoverageTest
+from tests.coveragetest import CoverageTest, UsingModulesMixin
class ConfigTest(CoverageTest):
@@ -220,15 +217,9 @@ class ConfigTest(CoverageTest):
_ = coverage.Coverage()
-class ConfigFileTest(CoverageTest):
+class ConfigFileTest(UsingModulesMixin, CoverageTest):
"""Tests of the config file settings in particular."""
- def setUp(self):
- super(ConfigFileTest, self).setUp()
- # Parent class saves and restores sys.path, we can just modify it.
- # Add modules to the path so we can import plugins.
- sys.path.append(self.nice_file(os.path.dirname(__file__), 'modules'))
-
# This sample file tries to use lots of variation of syntax...
# The {section} placeholder lets us nest these settings in another file.
LOTSA_SETTINGS = """\
diff --git a/tests/test_execfile.py b/tests/test_execfile.py
index bad3da9..032eaa2 100644
--- a/tests/test_execfile.py
+++ b/tests/test_execfile.py
@@ -8,16 +8,15 @@ import json
import os
import os.path
import re
-import sys
from coverage import env
from coverage.backward import binary_bytes
from coverage.execfile import run_python_file, run_python_module
from coverage.misc import NoCode, NoSource
-from tests.coveragetest import CoverageTest
+from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin
-TRY_EXECFILE = os.path.join(os.path.dirname(__file__), "modules/process_test/try_execfile.py")
+TRY_EXECFILE = os.path.join(TESTS_DIR, "modules/process_test/try_execfile.py")
class RunFileTest(CoverageTest):
@@ -170,16 +169,11 @@ class RunPycFileTest(CoverageTest):
run_python_file(bf, [bf])
-class RunModuleTest(CoverageTest):
+class RunModuleTest(UsingModulesMixin, CoverageTest):
"""Test run_python_module."""
run_in_temp_dir = False
- def setUp(self):
- super(RunModuleTest, self).setUp()
- # Parent class saves and restores sys.path, we can just modify it.
- sys.path.append(self.nice_file(os.path.dirname(__file__), 'modules'))
-
def test_runmod1(self):
run_python_module("runmod1", ["runmod1", "hello"])
self.assertEqual(self.stderr(), "")
diff --git a/tests/test_filereporter.py b/tests/test_filereporter.py
index 59eafa1..91e4776 100644
--- a/tests/test_filereporter.py
+++ b/tests/test_filereporter.py
@@ -4,12 +4,11 @@
"""Tests for FileReporters"""
import os
-import sys
from coverage.plugin import FileReporter
from coverage.python import PythonFileReporter
-from tests.coveragetest import CoverageTest
+from tests.coveragetest import CoverageTest, UsingModulesMixin
# pylint: disable=import-error
# Unable to import 'aa' (No module named aa)
@@ -20,17 +19,11 @@ def native(filename):
return filename.replace("/", os.sep)
-class FileReporterTest(CoverageTest):
+class FileReporterTest(UsingModulesMixin, CoverageTest):
"""Tests for FileReporter classes."""
run_in_temp_dir = False
- def setUp(self):
- super(FileReporterTest, self).setUp()
- # Parent class saves and restores sys.path, we can just modify it.
- testmods = self.nice_file(os.path.dirname(__file__), 'modules')
- sys.path.append(testmods)
-
def test_filenames(self):
acu = PythonFileReporter("aa/afile.py")
bcu = PythonFileReporter("aa/bb/bfile.py")
diff --git a/tests/test_phystokens.py b/tests/test_phystokens.py
index ddb652e..15bc6af 100644
--- a/tests/test_phystokens.py
+++ b/tests/test_phystokens.py
@@ -12,7 +12,7 @@ from coverage.phystokens import source_token_lines, source_encoding
from coverage.phystokens import neuter_encoding_declaration, compile_unicode
from coverage.python import get_python_source
-from tests.coveragetest import CoverageTest
+from tests.coveragetest import CoverageTest, TESTS_DIR
# A simple program and its token stream.
@@ -43,9 +43,6 @@ MIXED_WS_TOKENS = [
[('ws', ' '), ('nam', 'b'), ('op', '='), ('str', '"indented"')],
]
-# Where this file is, so we can find other files next to it.
-HERE = os.path.dirname(__file__)
-
class PhysTokensTest(CoverageTest):
"""Tests for coverage.py's improved tokenizer."""
@@ -83,14 +80,14 @@ class PhysTokensTest(CoverageTest):
def test_tokenize_real_file(self):
# Check the tokenization of a real file (large, btw).
- real_file = os.path.join(HERE, "test_coverage.py")
+ real_file = os.path.join(TESTS_DIR, "test_coverage.py")
self.check_file_tokenization(real_file)
def test_stress(self):
# Check the tokenization of a stress-test file.
- stress = os.path.join(HERE, "stress_phystoken.tok")
+ stress = os.path.join(TESTS_DIR, "stress_phystoken.tok")
self.check_file_tokenization(stress)
- stress = os.path.join(HERE, "stress_phystoken_dos.tok")
+ stress = os.path.join(TESTS_DIR, "stress_phystoken_dos.tok")
self.check_file_tokenization(stress)
diff --git a/tests/test_summary.py b/tests/test_summary.py
index 7c9f4c1..0fe112d 100644
--- a/tests/test_summary.py
+++ b/tests/test_summary.py
@@ -9,7 +9,6 @@ import os
import os.path
import py_compile
import re
-import sys
import coverage
from coverage import env
@@ -20,19 +19,12 @@ from coverage.data import CoverageData
from coverage.misc import CoverageException, output_encoding
from coverage.summary import SummaryReporter
-from tests.coveragetest import CoverageTest
+from tests.coveragetest import CoverageTest, TESTS_DIR, UsingModulesMixin
-HERE = os.path.dirname(__file__)
-
-class SummaryTest(CoverageTest):
+class SummaryTest(UsingModulesMixin, CoverageTest):
"""Tests of the text summary reporting for coverage.py."""
- def setUp(self):
- super(SummaryTest, self).setUp()
- # Parent class saves and restores sys.path, we can just modify it.
- sys.path.append(self.nice_file(HERE, 'modules'))
-
def make_mycode(self):
"""Make the mycode.py file when needed."""
self.make_file("mycode.py", """\
@@ -100,7 +92,7 @@ class SummaryTest(CoverageTest):
# Try reporting while omitting some modules
self.make_mycode()
self.run_command("coverage run mycode.py")
- report = self.report_from_command("coverage report --omit '%s/*'" % HERE)
+ report = self.report_from_command("coverage report --omit '%s/*'" % TESTS_DIR)
# Name Stmts Miss Cover
# -------------------------------
@@ -604,7 +596,7 @@ class SummaryTest(CoverageTest):
self.assertIn("mod.py 1 0 100%", report)
-class SummaryTest2(CoverageTest):
+class SummaryTest2(UsingModulesMixin, CoverageTest):
"""Another bunch of summary tests."""
# This class exists because tests naturally clump into classes based on the
# needs of their setUp, rather than the product features they are testing.
@@ -612,12 +604,6 @@ class SummaryTest2(CoverageTest):
run_in_temp_dir = False
- def setUp(self):
- super(SummaryTest2, self).setUp()
- # Parent class saves and restores sys.path, we can just modify it.
- sys.path.append(self.nice_file(HERE, 'modules'))
- sys.path.append(self.nice_file(HERE, 'moremodules'))
-
def test_empty_files(self):
# Shows that empty files like __init__.py are listed as having zero
# statements, not one statement.
@@ -675,13 +661,10 @@ class TestSummaryReporterConfiguration(CoverageTest):
run_in_temp_dir = False
- # We just need some readable files to work with. These will do.
- HERE = os.path.dirname(__file__)
-
LINES_1 = {
- os.path.join(HERE, "test_api.py"): dict.fromkeys(range(400)),
- os.path.join(HERE, "test_backward.py"): dict.fromkeys(range(20)),
- os.path.join(HERE, "test_coverage.py"): dict.fromkeys(range(15)),
+ os.path.join(TESTS_DIR, "test_api.py"): dict.fromkeys(range(400)),
+ os.path.join(TESTS_DIR, "test_backward.py"): dict.fromkeys(range(20)),
+ os.path.join(TESTS_DIR, "test_coverage.py"): dict.fromkeys(range(15)),
}
def get_coverage_data(self, lines):