Skip to content

Commit 50fec11

Browse files
committed
Add testcase for verifying CLI output
The single test function checks that the coverfile is correctly written and contains appropriate line count and unreached code highlighting. The check for ``if []`` avoids interpreter optimization which might allow the interpreter to recognize the conditional statement as a no-op and eliminate it. For example, ``if False`` would not be counted, nor would any code in the following block be highlighted by the trace.
1 parent cdfed48 commit 50fec11

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Lib/test/test_trace.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import sys
33
from test.support import TESTFN, rmtree, unlink, captured_stdout
44
from test.support.script_helper import assert_python_ok, assert_python_failure
5+
import textwrap
56
import unittest
67

78
import trace
@@ -365,6 +366,35 @@ def test_ignored(self):
365366
# Matched before.
366367
self.assertTrue(ignore.names(jn('bar', 'baz.py'), 'baz'))
367368

369+
# Created for Issue 31908 -- CLI utility not writing cover files
370+
class TestCoverageCommandLineOutput(unittest.TestCase):
371+
372+
codefile = 'tmp.py'
373+
coverfile = 'tmp.cover'
374+
375+
def setUp(self):
376+
with open(self.codefile, 'w') as f:
377+
f.write(textwrap.dedent('''\
378+
x = 42
379+
if []:
380+
print('unreachable')
381+
'''))
382+
383+
def tearDown(self):
384+
unlink(self.codefile)
385+
unlink(self.coverfile)
386+
387+
def test_cover_files_written(self):
388+
argv = '-m trace --count --missing'.split() + [self.codefile]
389+
status, stdout, stderr = assert_python_ok(*argv)
390+
self.assertTrue(os.path.exists(self.coverfile))
391+
with open(self.coverfile) as f:
392+
self.assertEqual(f.read(), textwrap.dedent('''\
393+
1: x = 42
394+
1: if []:
395+
>>>>>> print('unreachable')
396+
'''))
397+
368398
class TestCommandLine(unittest.TestCase):
369399

370400
def test_failures(self):

0 commit comments

Comments
 (0)