本文整理汇总了Python中coverage.backward.StringIO类的典型用法代码示例。如果您正苦于以下问题:Python StringIO类的具体用法?Python StringIO怎么用?Python StringIO使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StringIO类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_plugin_with_no_sys_info
def test_plugin_with_no_sys_info(self):
self.make_file(
"plugin_no_sys_info.py",
"""\
import coverage
class Plugin(coverage.CoveragePlugin):
pass
def coverage_init(reg, options):
reg.add_noop(Plugin())
""",
)
debug_out = StringIO()
cov = coverage.Coverage(debug=["sys"])
cov._debug_file = debug_out
cov.config["run:plugins"] = ["plugin_no_sys_info"]
cov.load()
out_lines = debug_out.getvalue().splitlines()
expected_end = [
"-- sys: plugin_no_sys_info.Plugin ----------------------------",
"-- end -------------------------------------------------------",
]
self.assertEqual(expected_end, out_lines[-len(expected_end) :])
示例2: pretend_to_be_pytestcov
def pretend_to_be_pytestcov(self, append):
"""Act like pytest-cov."""
self.make_file("prog.py", """\
a = 1
b = 2
if b == 1:
c = 4
""")
self.make_file(".coveragerc", """\
[run]
parallel = True
source = .
""")
cov = coverage.Coverage(source=None, branch=None, config_file='.coveragerc')
if append:
cov.load()
else:
cov.erase()
self.start_import_stop(cov, "prog")
cov.combine()
cov.save()
report = StringIO()
cov.report(show_missing=None, ignore_errors=True, file=report, skip_covered=None)
self.assertEqual(report.getvalue(), textwrap.dedent("""\
Name Stmts Miss Cover
-----------------------------
prog.py 4 1 75%
"""))
self.assert_file_count(".coverage", 0)
self.assert_file_count(".coverage.*", 1)
示例3: test_plugin_with_no_sys_info
def test_plugin_with_no_sys_info(self):
self.make_file("plugin_no_sys_info.py", """\
import coverage
class Plugin(coverage.CoveragePlugin):
pass
def coverage_init(reg, options):
reg.add_configurer(Plugin())
""")
debug_out = StringIO()
cov = coverage.Coverage(debug=["sys"])
cov._debug_file = debug_out
cov.set_option("run:plugins", ["plugin_no_sys_info"])
cov.start()
cov.stop() # pragma: nested
out_lines = [line.strip() for line in debug_out.getvalue().splitlines()]
self.assertIn('plugins.file_tracers: -none-', out_lines)
self.assertIn('plugins.configurers: plugin_no_sys_info.Plugin', out_lines)
expected_end = [
"-- sys: plugin_no_sys_info.Plugin ----------------------------",
"-- end -------------------------------------------------------",
]
self.assertEqual(expected_end, out_lines[-len(expected_end):])
示例4: setUp
def setUp(self):
if self.run_in_temp_dir:
# Create a temporary directory.
self.noise = str(random.random())[2:]
self.temp_root = os.path.join(tempfile.gettempdir(), 'test_cover')
self.temp_dir = os.path.join(self.temp_root, self.noise)
os.makedirs(self.temp_dir)
self.old_dir = os.getcwd()
os.chdir(self.temp_dir)
# Modules should be importable from this temp directory.
self.old_syspath = sys.path[:]
sys.path.insert(0, '')
# Keep a counter to make every call to check_coverage unique.
self.n = 0
# Record environment variables that we changed with set_environ.
self.environ_undos = {}
# Capture stdout and stderr so we can examine them in tests.
# nose keeps stdout from littering the screen, so we can safely Tee it,
# but it doesn't capture stderr, so we don't want to Tee stderr to the
# real stderr, since it will interfere with our nice field of dots.
self.old_stdout = sys.stdout
self.captured_stdout = StringIO()
sys.stdout = Tee(sys.stdout, self.captured_stdout)
self.old_stderr = sys.stderr
self.captured_stderr = StringIO()
sys.stderr = self.captured_stderr
# Record sys.modules here so we can restore it in tearDown.
self.old_modules = dict(sys.modules)
示例5: get_report
def get_report(self, cov):
"""Get the report from `cov`, and canonicalize it."""
repout = StringIO()
cov.report(file=repout, show_missing=False)
report = repout.getvalue().replace('\\', '/')
report = re.sub(r" +", " ", report)
return report
示例6: StdStreamCapturingMixin
class StdStreamCapturingMixin(TestCase):
"""A test case mixin that captures stdout and stderr."""
def setUp(self):
super(StdStreamCapturingMixin, self).setUp()
# Capture stdout and stderr so we can examine them in tests.
# nose keeps stdout from littering the screen, so we can safely Tee it,
# but it doesn't capture stderr, so we don't want to Tee stderr to the
# real stderr, since it will interfere with our nice field of dots.
self.old_stdout = sys.stdout
self.captured_stdout = StringIO()
sys.stdout = Tee(sys.stdout, self.captured_stdout)
self.old_stderr = sys.stderr
self.captured_stderr = StringIO()
sys.stderr = self.captured_stderr
self.addCleanup(self.cleanup_std_streams)
def cleanup_std_streams(self):
"""Restore stdout and stderr."""
sys.stdout = self.old_stdout
sys.stderr = self.old_stderr
def stdout(self):
"""Return the data written to stdout during the test."""
return self.captured_stdout.getvalue()
def stderr(self):
"""Return the data written to stderr during the test."""
return self.captured_stderr.getvalue()
示例7: test_plugin_sys_info
def test_plugin_sys_info(self):
self.make_file(
"plugin_sys_info.py",
"""\
import coverage
class Plugin(coverage.CoveragePlugin):
def sys_info(self):
return [("hello", "world")]
def coverage_init(reg, options):
reg.add_noop(Plugin())
""",
)
debug_out = StringIO()
cov = coverage.Coverage(debug=["sys"])
cov._debug_file = debug_out
cov.set_option("run:plugins", ["plugin_sys_info"])
cov.load()
out_lines = debug_out.getvalue().splitlines()
expected_end = [
"-- sys: plugin_sys_info.Plugin -------------------------------",
" hello: world",
"-- end -------------------------------------------------------",
]
self.assertEqual(expected_end, out_lines[-len(expected_end) :])
示例8: coverage_usepkgs
def coverage_usepkgs(self, **kwargs):
"""Try coverage.report()."""
cov = coverage.coverage()
cov.start()
import usepkgs # pragma: nested # pylint: disable=F0401,W0612
cov.stop() # pragma: nested
report = StringIO()
cov.report(file=report, **kwargs)
return report.getvalue()
示例9: coverage_usepkgs
def coverage_usepkgs(self, **kwargs):
"""Try coverage.report()."""
cov = coverage.Coverage()
cov.start()
import usepkgs # pragma: nested # pylint: disable=import-error,unused-variable
cov.stop() # pragma: nested
report = StringIO()
cov.report(file=report, **kwargs)
return report.getvalue()
示例10: test_defer_to_python
def test_defer_to_python(self):
# A plugin that measures, but then wants built-in python reporting.
self.make_file(
"fairly_odd_plugin.py",
"""\
# A plugin that claims all the odd lines are executed, and none of
# the even lines, and then punts reporting off to the built-in
# Python reporting.
import coverage.plugin
class Plugin(coverage.CoveragePlugin):
def file_tracer(self, filename):
return OddTracer(filename)
def file_reporter(self, filename):
return "python"
class OddTracer(coverage.plugin.FileTracer):
def __init__(self, filename):
self.filename = filename
def source_filename(self):
return self.filename
def line_number_range(self, frame):
lineno = frame.f_lineno
if lineno % 2:
return (lineno, lineno)
else:
return (-1, -1)
def coverage_init(reg, options):
reg.add_file_tracer(Plugin())
""",
)
self.make_file(
"unsuspecting.py",
"""\
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
""",
)
cov = coverage.Coverage(include=["unsuspecting.py"])
cov.set_option("run:plugins", ["fairly_odd_plugin"])
self.start_import_stop(cov, "unsuspecting")
repout = StringIO()
total = cov.report(file=repout)
report = repout.getvalue().splitlines()
expected = [
"Name Stmts Miss Cover Missing",
"-----------------------------------------------",
"unsuspecting.py 6 3 50% 2, 4, 6",
]
self.assertEqual(report, expected)
self.assertEqual(total, 50)
示例11: get_summary_text
def get_summary_text(self, coverage_data, options):
"""Get text output from the SummaryReporter."""
cov = Coverage()
cov.start()
cov.stop() # pragma: nested
cov.data = coverage_data
printer = SummaryReporter(cov, options)
destination = StringIO()
printer.report([], destination)
return destination.getvalue()
示例12: test_read_and_write_are_opposites
def test_read_and_write_are_opposites(self):
covdata1 = CoverageData()
covdata1.add_arcs(ARCS_3)
stringio = StringIO()
covdata1.write_fileobj(stringio)
stringio.seek(0)
covdata2 = CoverageData()
covdata2.read_fileobj(stringio)
self.assert_arcs3_data(covdata2)
示例13: test_report_file
def test_report_file(self):
# The file= argument of coverage.report makes the report go there.
self.do_report_work("mycode3")
fout = StringIO()
coverage.report(["mycode3.py"], file=fout)
self.assertEqual(self.stdout(), "")
self.assertEqual(fout.getvalue(), textwrap.dedent("""\
Name Stmts Miss Cover Missing
------------------------------------------
mycode3.py 7 3 57% 4-6
"""))
示例14: setUp
def setUp(self):
super(CoverageTest, self).setUp()
if _TEST_NAME_FILE:
f = open(_TEST_NAME_FILE, "w")
f.write("%s_%s" % (self.__class__.__name__, self._testMethodName))
f.close()
# Tell newer unittest implementations to print long helpful messages.
self.longMessage = True
# tearDown will restore the original sys.path
self.old_syspath = sys.path[:]
if self.run_in_temp_dir:
# Create a temporary directory.
self.noise = str(random.random())[2:]
self.temp_root = os.path.join(tempfile.gettempdir(), 'test_cover')
self.temp_dir = os.path.join(self.temp_root, self.noise)
os.makedirs(self.temp_dir)
self.old_dir = os.getcwd()
os.chdir(self.temp_dir)
# Modules should be importable from this temp directory. We don't
# use '' because we make lots of different temp directories and
# nose's caching importer can get confused. The full path prevents
# problems.
sys.path.insert(0, os.getcwd())
# Keep a counter to make every call to check_coverage unique.
self.n = 0
# Record environment variables that we changed with set_environ.
self.environ_undos = {}
# Capture stdout and stderr so we can examine them in tests.
# nose keeps stdout from littering the screen, so we can safely Tee it,
# but it doesn't capture stderr, so we don't want to Tee stderr to the
# real stderr, since it will interfere with our nice field of dots.
self.old_stdout = sys.stdout
self.captured_stdout = StringIO()
sys.stdout = Tee(sys.stdout, self.captured_stdout)
self.old_stderr = sys.stderr
self.captured_stderr = StringIO()
sys.stderr = self.captured_stderr
# Record sys.modules here so we can restore it in tearDown.
self.old_modules = dict(sys.modules)
class_behavior = self.class_behavior()
class_behavior.tests += 1
class_behavior.test_method_made_any_files = False
class_behavior.temp_dir = self.run_in_temp_dir
示例15: test_empty_files
def test_empty_files(self):
# Shows that empty files like __init__.py are listed as having zero
# statements, not one statement.
cov = coverage.Coverage()
cov.start()
import usepkgs # pragma: nested # pylint: disable=import-error,unused-variable
cov.stop() # pragma: nested
repout = StringIO()
cov.report(file=repout, show_missing=False)
report = repout.getvalue().replace('\\', '/')
report = re.sub(r"\s+", " ", report)
self.assertIn("tests/modules/pkg1/__init__.py 1 0 100%", report)
self.assertIn("tests/modules/pkg2/__init__.py 0 0 100%", report)