summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-02-02 09:12:47 -0500
committerNed Batchelder <ned@nedbatchelder.com>2021-02-02 09:12:47 -0500
commit80c021d9174e7ae3e5183f1902903fb90a891246 (patch)
treea0edae6ca0996217b9f0fa3213f824eab8b1cbe1
parent9012623110f49635fff61d19a4f5bb779de91b99 (diff)
downloadpython-coveragepy-git-80c021d9174e7ae3e5183f1902903fb90a891246.tar.gz
refactor: remove reliance on unittest_mixins.StdStreamCapturingMixin
This is another step toward removing unittest.TestCase as a base class.
-rw-r--r--tests/coveragetest.py4
-rw-r--r--tests/mixins.py31
-rw-r--r--tests/test_api.py8
-rw-r--r--tests/test_arcs.py5
-rw-r--r--tests/test_cmdline.py10
-rw-r--r--tests/test_execfile.py30
6 files changed, 62 insertions, 26 deletions
diff --git a/tests/coveragetest.py b/tests/coveragetest.py
index eeabfb46..71f4e2c8 100644
--- a/tests/coveragetest.py
+++ b/tests/coveragetest.py
@@ -15,7 +15,7 @@ import shlex
import sys
import pytest
-from unittest_mixins import EnvironmentAwareMixin, StdStreamCapturingMixin, TempDirMixin
+from unittest_mixins import EnvironmentAwareMixin, TempDirMixin
import coverage
from coverage import env
@@ -25,7 +25,7 @@ from coverage.cmdline import CoverageScript
from tests.helpers import arcs_to_arcz_repr, arcz_to_arcs
from tests.helpers import run_command, SuperModuleCleaner
-from tests.mixins import StopEverythingMixin
+from tests.mixins import StdStreamCapturingMixin, StopEverythingMixin
# Status returns for the command line.
diff --git a/tests/mixins.py b/tests/mixins.py
index 97ca093c..ab0623c0 100644
--- a/tests/mixins.py
+++ b/tests/mixins.py
@@ -10,6 +10,8 @@ Some of these are transitional while working toward pure-pytest style.
import functools
import types
+import pytest
+
from coverage.backunittest import unittest
from coverage.misc import StopEverything
@@ -37,3 +39,32 @@ class SkipConvertingMetaclass(type):
StopEverythingMixin = SkipConvertingMetaclass('StopEverythingMixin', (), {})
+
+
+class StdStreamCapturingMixin:
+ """
+ Adapter from the pytest capsys fixture to more convenient methods.
+
+ This doesn't also output to the real stdout, so we probably want to move
+ to "real" capsys when we can use fixtures in test methods.
+
+ Once you've used one of these methods, the capturing is reset, so another
+ invocation will only return the delta.
+
+ """
+ @pytest.fixture(autouse=True)
+ def _capcapsys(self, capsys):
+ """Grab the fixture so our methods can use it."""
+ self.capsys = capsys
+
+ def stdouterr(self):
+ """Returns (out, err), two strings for stdout and stderr."""
+ return self.capsys.readouterr()
+
+ def stdout(self):
+ """Returns a string, the captured stdout."""
+ return self.capsys.readouterr().out
+
+ def stderr(self):
+ """Returns a string, the captured stderr."""
+ return self.capsys.readouterr().err
diff --git a/tests/test_api.py b/tests/test_api.py
index 6c322795..ea625ff1 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -520,10 +520,8 @@ class ApiTest(CoverageTest):
self.start_import_stop(cov, "hello")
cov.get_data()
- out = self.stdout()
+ out, err = self.stdouterr()
assert "Hello\n" in out
-
- err = self.stderr()
assert textwrap.dedent("""\
Coverage.py warning: Module sys has no Python source. (module-not-python)
Coverage.py warning: Module xyzzy was never imported. (module-not-imported)
@@ -544,10 +542,8 @@ class ApiTest(CoverageTest):
self.start_import_stop(cov, "hello")
cov.get_data()
- out = self.stdout()
+ out, err = self.stdouterr()
assert "Hello\n" in out
-
- err = self.stderr()
assert "Coverage.py warning: Module sys has no Python source. (module-not-python)" in err
assert "module-not-imported" not in err
assert "no-data-collected" not in err
diff --git a/tests/test_arcs.py b/tests/test_arcs.py
index 3b63bcc2..66777751 100644
--- a/tests/test_arcs.py
+++ b/tests/test_arcs.py
@@ -1370,7 +1370,8 @@ class MiscArcTest(CoverageTest):
# opcodes.
# Note that we no longer interpret bytecode at all, but it couldn't
# hurt to keep the test...
- for n in [10, 50, 100, 500, 1000, 2000]:
+ sizes = [10, 50, 100, 500, 1000, 2000]
+ for n in sizes:
code = """\
data = [
""" + "".join("""\
@@ -1383,7 +1384,7 @@ class MiscArcTest(CoverageTest):
print(len(data))
"""
self.check_coverage(code, arcs=[(-1, 1), (1, 2*n+4), (2*n+4, -1)])
- assert self.stdout().split()[-1] == str(n)
+ assert self.stdout().split() == [str(n) for n in sizes]
def test_partial_generators(self):
# https://github.com/nedbat/coveragepy/issues/475
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py
index a0744452..0eb26cd0 100644
--- a/tests/test_cmdline.py
+++ b/tests/test_cmdline.py
@@ -545,8 +545,9 @@ class CmdLineTest(BaseCmdLineTest):
# config file.
self.command_line("run --concurrency=multiprocessing --branch foo.py", ret=ERR)
msg = "Options affecting multiprocessing must only be specified in a configuration file."
- assert msg in self.stderr()
- assert "Remove --branch from the command line." in self.stderr()
+ _, err = self.stdouterr()
+ assert msg in err
+ assert "Remove --branch from the command line." in err
def test_run_debug(self):
self.cmd_executes("run --debug=opt1 foo.py", """\
@@ -915,8 +916,9 @@ class CmdMainTest(CoverageTest):
def test_raise(self):
ret = coverage.cmdline.main(['raise'])
assert ret == 1
- assert self.stdout() == ""
- err = self.stderr().split('\n')
+ out, err = self.stdouterr()
+ assert out == ""
+ err = err.split('\n')
assert err[0] == 'Traceback (most recent call last):'
assert err[-3] == ' raise Exception("oh noes!")'
assert err[-2] == 'Exception: oh noes!'
diff --git a/tests/test_execfile.py b/tests/test_execfile.py
index db78d0f6..d221f2d3 100644
--- a/tests/test_execfile.py
+++ b/tests/test_execfile.py
@@ -193,33 +193,39 @@ class RunModuleTest(UsingModulesMixin, CoverageTest):
def test_runmod1(self):
run_python_module(["runmod1", "hello"])
- assert self.stderr() == ""
- assert self.stdout() == "runmod1: passed hello\n"
+ out, err = self.stdouterr()
+ assert out == "runmod1: passed hello\n"
+ assert err == ""
def test_runmod2(self):
run_python_module(["pkg1.runmod2", "hello"])
- assert self.stderr() == ""
- assert self.stdout() == "pkg1.__init__: pkg1\nrunmod2: passed hello\n"
+ out, err = self.stdouterr()
+ assert out == "pkg1.__init__: pkg1\nrunmod2: passed hello\n"
+ assert err == ""
def test_runmod3(self):
run_python_module(["pkg1.sub.runmod3", "hello"])
- assert self.stderr() == ""
- assert self.stdout() == "pkg1.__init__: pkg1\nrunmod3: passed hello\n"
+ out, err = self.stdouterr()
+ assert out == "pkg1.__init__: pkg1\nrunmod3: passed hello\n"
+ assert err == ""
def test_pkg1_main(self):
run_python_module(["pkg1", "hello"])
- assert self.stderr() == ""
- assert self.stdout() == "pkg1.__init__: pkg1\npkg1.__main__: passed hello\n"
+ out, err = self.stdouterr()
+ assert out == "pkg1.__init__: pkg1\npkg1.__main__: passed hello\n"
+ assert err == ""
def test_pkg1_sub_main(self):
run_python_module(["pkg1.sub", "hello"])
- assert self.stderr() == ""
- assert self.stdout() == "pkg1.__init__: pkg1\npkg1.sub.__main__: passed hello\n"
+ out, err = self.stdouterr()
+ assert out == "pkg1.__init__: pkg1\npkg1.sub.__main__: passed hello\n"
+ assert err == ""
def test_pkg1_init(self):
run_python_module(["pkg1.__init__", "wut?"])
- assert self.stderr() == ""
- assert self.stdout() == "pkg1.__init__: pkg1\npkg1.__init__: __main__\n"
+ out, err = self.stdouterr()
+ assert out == "pkg1.__init__: pkg1\npkg1.__init__: __main__\n"
+ assert err == ""
def test_no_such_module(self):
with pytest.raises(NoSource, match="No module named '?i_dont_exist'?"):