test_summary.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. # coding: utf8
  2. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  3. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  4. """Test text-based summary reporting for coverage.py"""
  5. import glob
  6. import os
  7. import os.path
  8. import py_compile
  9. import re
  10. import sys
  11. import coverage
  12. from coverage import env
  13. from coverage.backward import StringIO
  14. from coverage.config import CoverageConfig
  15. from coverage.control import Coverage
  16. from coverage.data import CoverageData
  17. from coverage.misc import CoverageException, output_encoding
  18. from coverage.summary import SummaryReporter
  19. from tests.coveragetest import CoverageTest
  20. HERE = os.path.dirname(__file__)
  21. class SummaryTest(CoverageTest):
  22. """Tests of the text summary reporting for coverage.py."""
  23. def setUp(self):
  24. super(SummaryTest, self).setUp()
  25. # Parent class saves and restores sys.path, we can just modify it.
  26. sys.path.append(self.nice_file(HERE, 'modules'))
  27. def make_mycode(self):
  28. """Make the mycode.py file when needed."""
  29. self.make_file("mycode.py", """\
  30. import covmod1
  31. import covmodzip1
  32. a = 1
  33. print('done')
  34. """)
  35. def test_report(self):
  36. self.make_mycode()
  37. out = self.run_command("coverage run mycode.py")
  38. self.assertEqual(out, 'done\n')
  39. report = self.report_from_command("coverage report")
  40. # Name Stmts Miss Cover
  41. # ------------------------------------------------------------------
  42. # c:/ned/coverage/tests/modules/covmod1.py 2 0 100%
  43. # c:/ned/coverage/tests/zipmods.zip/covmodzip1.py 3 0 100%
  44. # mycode.py 4 0 100%
  45. # ------------------------------------------------------------------
  46. # TOTAL 9 0 100%
  47. self.assertNotIn("/coverage/__init__/", report)
  48. self.assertIn("/tests/modules/covmod1.py ", report)
  49. self.assertIn("/tests/zipmods.zip/covmodzip1.py ", report)
  50. self.assertIn("mycode.py ", report)
  51. self.assertEqual(self.last_line_squeezed(report), "TOTAL 9 0 100%")
  52. def test_report_just_one(self):
  53. # Try reporting just one module
  54. self.make_mycode()
  55. self.run_command("coverage run mycode.py")
  56. report = self.report_from_command("coverage report mycode.py")
  57. # Name Stmts Miss Cover
  58. # -------------------------------
  59. # mycode.py 4 0 100%
  60. self.assertEqual(self.line_count(report), 3)
  61. self.assertNotIn("/coverage/", report)
  62. self.assertNotIn("/tests/modules/covmod1.py ", report)
  63. self.assertNotIn("/tests/zipmods.zip/covmodzip1.py ", report)
  64. self.assertIn("mycode.py ", report)
  65. self.assertEqual(self.last_line_squeezed(report), "mycode.py 4 0 100%")
  66. def test_report_wildcard(self):
  67. # Try reporting using wildcards to get the modules.
  68. self.make_mycode()
  69. self.run_command("coverage run mycode.py")
  70. report = self.report_from_command("coverage report my*.py")
  71. # Name Stmts Miss Cover
  72. # -------------------------------
  73. # mycode.py 4 0 100%
  74. self.assertEqual(self.line_count(report), 3)
  75. self.assertNotIn("/coverage/", report)
  76. self.assertNotIn("/tests/modules/covmod1.py ", report)
  77. self.assertNotIn("/tests/zipmods.zip/covmodzip1.py ", report)
  78. self.assertIn("mycode.py ", report)
  79. self.assertEqual(self.last_line_squeezed(report), "mycode.py 4 0 100%")
  80. def test_report_omitting(self):
  81. # Try reporting while omitting some modules
  82. self.make_mycode()
  83. self.run_command("coverage run mycode.py")
  84. report = self.report_from_command("coverage report --omit '%s/*'" % HERE)
  85. # Name Stmts Miss Cover
  86. # -------------------------------
  87. # mycode.py 4 0 100%
  88. self.assertEqual(self.line_count(report), 3)
  89. self.assertNotIn("/coverage/", report)
  90. self.assertNotIn("/tests/modules/covmod1.py ", report)
  91. self.assertNotIn("/tests/zipmods.zip/covmodzip1.py ", report)
  92. self.assertIn("mycode.py ", report)
  93. self.assertEqual(self.last_line_squeezed(report), "mycode.py 4 0 100%")
  94. def test_report_including(self):
  95. # Try reporting while including some modules
  96. self.make_mycode()
  97. self.run_command("coverage run mycode.py")
  98. report = self.report_from_command("coverage report --include=mycode*")
  99. # Name Stmts Miss Cover
  100. # -------------------------------
  101. # mycode.py 4 0 100%
  102. self.assertEqual(self.line_count(report), 3)
  103. self.assertNotIn("/coverage/", report)
  104. self.assertNotIn("/tests/modules/covmod1.py ", report)
  105. self.assertNotIn("/tests/zipmods.zip/covmodzip1.py ", report)
  106. self.assertIn("mycode.py ", report)
  107. self.assertEqual(self.last_line_squeezed(report), "mycode.py 4 0 100%")
  108. def test_report_branches(self):
  109. self.make_file("mybranch.py", """\
  110. def branch(x):
  111. if x:
  112. print("x")
  113. return x
  114. branch(1)
  115. """)
  116. out = self.run_command("coverage run --branch mybranch.py")
  117. self.assertEqual(out, 'x\n')
  118. report = self.report_from_command("coverage report")
  119. # Name Stmts Miss Branch BrPart Cover
  120. # -----------------------------------------------
  121. # mybranch.py 5 0 2 1 85%
  122. self.assertEqual(self.line_count(report), 3)
  123. self.assertIn("mybranch.py ", report)
  124. self.assertEqual(self.last_line_squeezed(report), "mybranch.py 5 0 2 1 86%")
  125. def test_report_show_missing(self):
  126. self.make_file("mymissing.py", """\
  127. def missing(x, y):
  128. if x:
  129. print("x")
  130. return x
  131. if y:
  132. print("y")
  133. try:
  134. print("z")
  135. 1/0
  136. print("Never!")
  137. except ZeroDivisionError:
  138. pass
  139. return x
  140. missing(0, 1)
  141. """)
  142. out = self.run_command("coverage run mymissing.py")
  143. self.assertEqual(out, 'y\nz\n')
  144. report = self.report_from_command("coverage report --show-missing")
  145. # Name Stmts Miss Cover Missing
  146. # --------------------------------------------
  147. # mymissing.py 14 3 79% 3-4, 10
  148. self.assertEqual(self.line_count(report), 3)
  149. self.assertIn("mymissing.py ", report)
  150. self.assertEqual(self.last_line_squeezed(report), "mymissing.py 14 3 79% 3-4, 10")
  151. def test_report_show_missing_branches(self):
  152. self.make_file("mybranch.py", """\
  153. def branch(x, y):
  154. if x:
  155. print("x")
  156. if y:
  157. print("y")
  158. branch(1, 1)
  159. """)
  160. out = self.run_command("coverage run --branch mybranch.py")
  161. self.assertEqual(out, 'x\ny\n')
  162. report = self.report_from_command("coverage report --show-missing")
  163. # Name Stmts Miss Branch BrPart Cover Missing
  164. # ----------------------------------------------------------
  165. # mybranch.py 6 0 4 2 80% 2->4, 4->exit
  166. self.assertEqual(self.line_count(report), 3)
  167. self.assertIn("mybranch.py ", report)
  168. self.assertEqual(self.last_line_squeezed(report), "mybranch.py 6 0 4 2 80% 2->4, 4->exit")
  169. def test_report_show_missing_branches_and_lines(self):
  170. self.make_file("main.py", """\
  171. import mybranch
  172. """)
  173. self.make_file("mybranch.py", """\
  174. def branch(x, y, z):
  175. if x:
  176. print("x")
  177. if y:
  178. print("y")
  179. if z:
  180. if x and y:
  181. print("z")
  182. return x
  183. branch(1, 1, 0)
  184. """)
  185. out = self.run_command("coverage run --branch main.py")
  186. self.assertEqual(out, 'x\ny\n')
  187. report = self.report_from_command("coverage report --show-missing")
  188. report_lines = report.splitlines()
  189. expected = [
  190. 'Name Stmts Miss Branch BrPart Cover Missing',
  191. '---------------------------------------------------------',
  192. 'main.py 1 0 0 0 100%',
  193. 'mybranch.py 10 2 8 3 61% 7-8, 2->4, 4->6, 6->7',
  194. '---------------------------------------------------------',
  195. 'TOTAL 11 2 8 3 63%',
  196. ]
  197. self.assertEqual(report_lines, expected)
  198. def test_report_skip_covered_no_branches(self):
  199. self.make_file("main.py", """
  200. import not_covered
  201. def normal():
  202. print("z")
  203. normal()
  204. """)
  205. self.make_file("not_covered.py", """
  206. def not_covered():
  207. print("n")
  208. """)
  209. out = self.run_command("coverage run main.py")
  210. self.assertEqual(out, "z\n")
  211. report = self.report_from_command("coverage report --skip-covered --fail-under=70")
  212. # Name Stmts Miss Cover
  213. # ------------------------------------
  214. # not_covered.py 2 1 50%
  215. # ------------------------------------
  216. # TOTAL 6 1 83%
  217. #
  218. # 1 file skipped due to complete coverage.
  219. self.assertEqual(self.line_count(report), 7, report)
  220. squeezed = self.squeezed_lines(report)
  221. self.assertEqual(squeezed[2], "not_covered.py 2 1 50%")
  222. self.assertEqual(squeezed[4], "TOTAL 6 1 83%")
  223. self.assertEqual(squeezed[6], "1 file skipped due to complete coverage.")
  224. self.assertEqual(self.last_command_status, 0)
  225. def test_report_skip_covered_branches(self):
  226. self.make_file("main.py", """
  227. import not_covered, covered
  228. def normal(z):
  229. if z:
  230. print("z")
  231. normal(True)
  232. normal(False)
  233. """)
  234. self.make_file("not_covered.py", """
  235. def not_covered(n):
  236. if n:
  237. print("n")
  238. not_covered(True)
  239. """)
  240. self.make_file("covered.py", """
  241. def foo():
  242. pass
  243. foo()
  244. """)
  245. out = self.run_command("coverage run --branch main.py")
  246. self.assertEqual(out, "n\nz\n")
  247. report = self.report_from_command("coverage report --skip-covered")
  248. # Name Stmts Miss Branch BrPart Cover
  249. # --------------------------------------------------
  250. # not_covered.py 4 0 2 1 83%
  251. # --------------------------------------------------
  252. # TOTAL 13 0 4 1 94%
  253. #
  254. # 2 files skipped due to complete coverage.
  255. self.assertEqual(self.line_count(report), 7, report)
  256. squeezed = self.squeezed_lines(report)
  257. self.assertEqual(squeezed[2], "not_covered.py 4 0 2 1 83%")
  258. self.assertEqual(squeezed[4], "TOTAL 13 0 4 1 94%")
  259. self.assertEqual(squeezed[6], "2 files skipped due to complete coverage.")
  260. def test_report_skip_covered_branches_with_totals(self):
  261. self.make_file("main.py", """
  262. import not_covered
  263. import also_not_run
  264. def normal(z):
  265. if z:
  266. print("z")
  267. normal(True)
  268. normal(False)
  269. """)
  270. self.make_file("not_covered.py", """
  271. def not_covered(n):
  272. if n:
  273. print("n")
  274. not_covered(True)
  275. """)
  276. self.make_file("also_not_run.py", """
  277. def does_not_appear_in_this_film(ni):
  278. print("Ni!")
  279. """)
  280. out = self.run_command("coverage run --branch main.py")
  281. self.assertEqual(out, "n\nz\n")
  282. report = self.report_from_command("coverage report --skip-covered")
  283. # Name Stmts Miss Branch BrPart Cover
  284. # --------------------------------------------------
  285. # also_not_run.py 2 1 0 0 50%
  286. # not_covered.py 4 0 2 1 83%
  287. # --------------------------------------------------
  288. # TOTAL 13 1 4 1 88%
  289. #
  290. # 1 file skipped due to complete coverage.
  291. self.assertEqual(self.line_count(report), 8, report)
  292. squeezed = self.squeezed_lines(report)
  293. self.assertEqual(squeezed[2], "also_not_run.py 2 1 0 0 50%")
  294. self.assertEqual(squeezed[3], "not_covered.py 4 0 2 1 83%")
  295. self.assertEqual(squeezed[5], "TOTAL 13 1 4 1 88%")
  296. self.assertEqual(squeezed[7], "1 file skipped due to complete coverage.")
  297. def test_report_skip_covered_all_files_covered(self):
  298. self.make_file("main.py", """
  299. def foo():
  300. pass
  301. foo()
  302. """)
  303. out = self.run_command("coverage run --branch main.py")
  304. self.assertEqual(out, "")
  305. report = self.report_from_command("coverage report --skip-covered")
  306. # Name Stmts Miss Branch BrPart Cover
  307. # -------------------------------------------
  308. #
  309. # 1 file skipped due to complete coverage.
  310. self.assertEqual(self.line_count(report), 4, report)
  311. squeezed = self.squeezed_lines(report)
  312. self.assertEqual(squeezed[3], "1 file skipped due to complete coverage.")
  313. def test_report_skip_covered_no_data(self):
  314. report = self.report_from_command("coverage report --skip-covered")
  315. # Name Stmts Miss Branch BrPart Cover
  316. # -------------------------------------------
  317. # No data to report.
  318. self.assertEqual(self.line_count(report), 3, report)
  319. squeezed = self.squeezed_lines(report)
  320. self.assertEqual(squeezed[2], "No data to report.")
  321. def test_dotpy_not_python(self):
  322. # We run a .py file, and when reporting, we can't parse it as Python.
  323. # We should get an error message in the report.
  324. self.make_mycode()
  325. self.run_command("coverage run mycode.py")
  326. self.make_file("mycode.py", "This isn't python at all!")
  327. report = self.report_from_command("coverage report mycode.py")
  328. # Name Stmts Miss Cover
  329. # ----------------------------
  330. # mycode NotPython: Couldn't parse '...' as Python source: 'invalid syntax' at line 1
  331. # No data to report.
  332. last = self.squeezed_lines(report)[-2]
  333. # The actual file name varies run to run.
  334. last = re.sub(r"parse '.*mycode.py", "parse 'mycode.py", last)
  335. # The actual error message varies version to version
  336. last = re.sub(r": '.*' at", ": 'error' at", last)
  337. self.assertEqual(
  338. last,
  339. "mycode.py NotPython: Couldn't parse 'mycode.py' as Python source: 'error' at line 1"
  340. )
  341. def test_accenteddotpy_not_python(self):
  342. # We run a .py file with a non-ascii name, and when reporting, we can't
  343. # parse it as Python. We should get an error message in the report.
  344. self.make_file(u"accented\xe2.py", "print('accented')")
  345. self.run_command(u"coverage run accented\xe2.py")
  346. self.make_file(u"accented\xe2.py", "This isn't python at all!")
  347. report = self.report_from_command(u"coverage report accented\xe2.py")
  348. # Name Stmts Miss Cover
  349. # ----------------------------
  350. # xxxx NotPython: Couldn't parse '...' as Python source: 'invalid syntax' at line 1
  351. # No data to report.
  352. last = self.squeezed_lines(report)[-2]
  353. # The actual file name varies run to run.
  354. last = re.sub(r"parse '.*(accented.*?\.py)", r"parse '\1", last)
  355. # The actual error message varies version to version
  356. last = re.sub(r": '.*' at", ": 'error' at", last)
  357. expected = (
  358. u"accented\xe2.py NotPython: "
  359. u"Couldn't parse 'accented\xe2.py' as Python source: 'error' at line 1"
  360. )
  361. if env.PY2:
  362. # pylint: disable=redefined-variable-type
  363. expected = expected.encode(output_encoding())
  364. self.assertEqual(last, expected)
  365. def test_dotpy_not_python_ignored(self):
  366. # We run a .py file, and when reporting, we can't parse it as Python,
  367. # but we've said to ignore errors, so there's no error reported.
  368. self.make_mycode()
  369. self.run_command("coverage run mycode.py")
  370. self.make_file("mycode.py", "This isn't python at all!")
  371. report = self.report_from_command("coverage report -i mycode.py")
  372. # Name Stmts Miss Cover
  373. # ----------------------------
  374. self.assertEqual(self.line_count(report), 3)
  375. self.assertIn('No data to report.', report)
  376. def test_dothtml_not_python(self):
  377. # We run a .html file, and when reporting, we can't parse it as
  378. # Python. Since it wasn't .py, no error is reported.
  379. # Run an "html" file
  380. self.make_file("mycode.html", "a = 1")
  381. self.run_command("coverage run mycode.html")
  382. # Before reporting, change it to be an HTML file.
  383. self.make_file("mycode.html", "<h1>This isn't python at all!</h1>")
  384. report = self.report_from_command("coverage report mycode.html")
  385. # Name Stmts Miss Cover
  386. # ----------------------------
  387. # No data to report.
  388. self.assertEqual(self.line_count(report), 3)
  389. self.assertIn('No data to report.', report)
  390. def get_report(self, cov):
  391. """Get the report from `cov`, and canonicalize it."""
  392. repout = StringIO()
  393. cov.report(file=repout, show_missing=False)
  394. report = repout.getvalue().replace('\\', '/')
  395. report = re.sub(r" +", " ", report)
  396. return report
  397. def test_bug_156_file_not_run_should_be_zero(self):
  398. # https://bitbucket.org/ned/coveragepy/issue/156
  399. self.make_file("mybranch.py", """\
  400. def branch(x):
  401. if x:
  402. print("x")
  403. return x
  404. branch(1)
  405. """)
  406. self.make_file("main.py", """\
  407. print("y")
  408. """)
  409. cov = coverage.Coverage(branch=True, source=["."])
  410. cov.start()
  411. import main # pragma: nested # pylint: disable=import-error
  412. cov.stop() # pragma: nested
  413. report = self.get_report(cov).splitlines()
  414. self.assertIn("mybranch.py 5 5 2 0 0%", report)
  415. def run_TheCode_and_report_it(self):
  416. """A helper for the next few tests."""
  417. cov = coverage.Coverage()
  418. cov.start()
  419. import TheCode # pragma: nested # pylint: disable=import-error
  420. cov.stop() # pragma: nested
  421. return self.get_report(cov)
  422. def test_bug_203_mixed_case_listed_twice_with_rc(self):
  423. self.make_file("TheCode.py", "a = 1\n")
  424. self.make_file(".coveragerc", "[run]\nsource = .\n")
  425. report = self.run_TheCode_and_report_it()
  426. self.assertIn("TheCode", report)
  427. self.assertNotIn("thecode", report)
  428. def test_bug_203_mixed_case_listed_twice(self):
  429. self.make_file("TheCode.py", "a = 1\n")
  430. report = self.run_TheCode_and_report_it()
  431. self.assertIn("TheCode", report)
  432. self.assertNotIn("thecode", report)
  433. def test_pyw_files(self):
  434. if not env.WINDOWS:
  435. self.skipTest(".pyw files are only on Windows.")
  436. # https://bitbucket.org/ned/coveragepy/issue/261
  437. self.make_file("start.pyw", """\
  438. import mod
  439. print("In start.pyw")
  440. """)
  441. self.make_file("mod.pyw", """\
  442. print("In mod.pyw")
  443. """)
  444. cov = coverage.Coverage()
  445. cov.start()
  446. import start # pragma: nested # pylint: disable=import-error
  447. cov.stop() # pragma: nested
  448. report = self.get_report(cov)
  449. self.assertNotIn("NoSource", report)
  450. report = report.splitlines()
  451. self.assertIn("start.pyw 2 0 100%", report)
  452. self.assertIn("mod.pyw 1 0 100%", report)
  453. def test_tracing_pyc_file(self):
  454. # Create two Python files.
  455. self.make_file("mod.py", "a = 1\n")
  456. self.make_file("main.py", "import mod\n")
  457. # Make one into a .pyc.
  458. py_compile.compile("mod.py")
  459. # Run the program.
  460. cov = coverage.Coverage()
  461. cov.start()
  462. import main # pragma: nested # pylint: disable=import-error
  463. cov.stop() # pragma: nested
  464. report = self.get_report(cov).splitlines()
  465. self.assertIn("mod.py 1 0 100%", report)
  466. def test_missing_py_file_during_run(self):
  467. # PyPy2 doesn't run bare .pyc files.
  468. if env.PYPY and env.PY2:
  469. self.skipTest("PyPy2 doesn't run bare .pyc files")
  470. # Create two Python files.
  471. self.make_file("mod.py", "a = 1\n")
  472. self.make_file("main.py", "import mod\n")
  473. # Make one into a .pyc, and remove the .py.
  474. py_compile.compile("mod.py")
  475. os.remove("mod.py")
  476. # Python 3 puts the .pyc files in a __pycache__ directory, and will
  477. # not import from there without source. It will import a .pyc from
  478. # the source location though.
  479. if not os.path.exists("mod.pyc"):
  480. pycs = glob.glob("__pycache__/mod.*.pyc")
  481. self.assertEqual(len(pycs), 1)
  482. os.rename(pycs[0], "mod.pyc")
  483. # Run the program.
  484. cov = coverage.Coverage()
  485. cov.start()
  486. import main # pragma: nested # pylint: disable=import-error
  487. cov.stop() # pragma: nested
  488. # Put back the missing Python file.
  489. self.make_file("mod.py", "a = 1\n")
  490. report = self.get_report(cov).splitlines()
  491. self.assertIn("mod.py 1 0 100%", report)
  492. class SummaryTest2(CoverageTest):
  493. """Another bunch of summary tests."""
  494. # This class exists because tests naturally clump into classes based on the
  495. # needs of their setUp, rather than the product features they are testing.
  496. # There's probably a better way to organize these.
  497. run_in_temp_dir = False
  498. def setUp(self):
  499. super(SummaryTest2, self).setUp()
  500. # Parent class saves and restores sys.path, we can just modify it.
  501. sys.path.append(self.nice_file(HERE, 'modules'))
  502. sys.path.append(self.nice_file(HERE, 'moremodules'))
  503. def test_empty_files(self):
  504. # Shows that empty files like __init__.py are listed as having zero
  505. # statements, not one statement.
  506. cov = coverage.Coverage(branch=True)
  507. cov.start()
  508. import usepkgs # pragma: nested # pylint: disable=import-error
  509. cov.stop() # pragma: nested
  510. repout = StringIO()
  511. cov.report(file=repout, show_missing=False)
  512. report = repout.getvalue().replace('\\', '/')
  513. report = re.sub(r"\s+", " ", report)
  514. self.assertIn("tests/modules/pkg1/__init__.py 2 0 0 0 100%", report)
  515. self.assertIn("tests/modules/pkg2/__init__.py 0 0 0 0 100%", report)
  516. class ReportingReturnValueTest(CoverageTest):
  517. """Tests of reporting functions returning values."""
  518. def run_coverage(self):
  519. """Run coverage on doit.py and return the coverage object."""
  520. self.make_file("doit.py", """\
  521. a = 1
  522. b = 2
  523. c = 3
  524. d = 4
  525. if a > 10:
  526. f = 6
  527. g = 7
  528. """)
  529. cov = coverage.Coverage()
  530. self.start_import_stop(cov, "doit")
  531. return cov
  532. def test_report(self):
  533. cov = self.run_coverage()
  534. val = cov.report(include="*/doit.py")
  535. self.assertAlmostEqual(val, 85.7, 1)
  536. def test_html(self):
  537. cov = self.run_coverage()
  538. val = cov.html_report(include="*/doit.py")
  539. self.assertAlmostEqual(val, 85.7, 1)
  540. def test_xml(self):
  541. cov = self.run_coverage()
  542. val = cov.xml_report(include="*/doit.py")
  543. self.assertAlmostEqual(val, 85.7, 1)
  544. class TestSummaryReporterConfiguration(CoverageTest):
  545. """Tests of SummaryReporter."""
  546. run_in_temp_dir = False
  547. # We just need some readable files to work with. These will do.
  548. HERE = os.path.dirname(__file__)
  549. LINES_1 = {
  550. os.path.join(HERE, "test_api.py"): dict.fromkeys(range(300)),
  551. os.path.join(HERE, "test_backward.py"): dict.fromkeys(range(20)),
  552. os.path.join(HERE, "test_coverage.py"): dict.fromkeys(range(15)),
  553. }
  554. def get_coverage_data(self, lines):
  555. """Get a CoverageData object that includes the requested lines."""
  556. data = CoverageData()
  557. data.add_lines(lines)
  558. return data
  559. def get_summary_text(self, coverage_data, options):
  560. """Get text output from the SummaryReporter."""
  561. cov = Coverage()
  562. cov.start()
  563. cov.stop() # pragma: nested
  564. cov.data = coverage_data
  565. printer = SummaryReporter(cov, options)
  566. destination = StringIO()
  567. printer.report([], destination)
  568. return destination.getvalue()
  569. def test_test_data(self):
  570. # We use our own test files as test data. Check that our assumptions
  571. # about them are still valid. We want the three columns of numbers to
  572. # sort in three different orders.
  573. data = self.get_coverage_data(self.LINES_1)
  574. report = self.get_summary_text(data, CoverageConfig())
  575. print(report)
  576. # Name Stmts Miss Cover
  577. # --------------------------------------------
  578. # tests/test_api.py 339 155 54%
  579. # tests/test_backward.py 13 3 77%
  580. # tests/test_coverage.py 234 228 3%
  581. # --------------------------------------------
  582. # TOTAL 586 386 34%
  583. lines = report.splitlines()[2:-2]
  584. self.assertEqual(len(lines), 3)
  585. nums = [list(map(int, l.replace('%', '').split()[1:])) for l in lines]
  586. # [
  587. # [339, 155, 54],
  588. # [ 13, 3, 77],
  589. # [234, 228, 3]
  590. # ]
  591. self.assertTrue(nums[1][0] < nums[2][0] < nums[0][0])
  592. self.assertTrue(nums[1][1] < nums[0][1] < nums[2][1])
  593. self.assertTrue(nums[2][2] < nums[0][2] < nums[1][2])
  594. def test_defaults(self):
  595. """Run the report with no configuration options."""
  596. data = self.get_coverage_data(self.LINES_1)
  597. opts = CoverageConfig()
  598. report = self.get_summary_text(data, opts)
  599. self.assertNotIn('Missing', report)
  600. self.assertNotIn('Branch', report)
  601. def test_print_missing(self):
  602. """Run the report printing the missing lines."""
  603. data = self.get_coverage_data(self.LINES_1)
  604. opts = CoverageConfig()
  605. opts.from_args(show_missing=True)
  606. report = self.get_summary_text(data, opts)
  607. self.assertIn('Missing', report)
  608. self.assertNotIn('Branch', report)
  609. def assert_ordering(self, text, *words):
  610. """Assert that the `words` appear in order in `text`."""
  611. indexes = list(map(text.find, words))
  612. self.assertEqual(
  613. indexes, sorted(indexes),
  614. "The words %r don't appear in order in %r" % (words, text)
  615. )
  616. def test_sort_report_by_stmts(self):
  617. # Sort the text report by the Stmts column.
  618. data = self.get_coverage_data(self.LINES_1)
  619. opts = CoverageConfig()
  620. opts.from_args(sort='Stmts')
  621. report = self.get_summary_text(data, opts)
  622. self.assert_ordering(report, "test_backward.py", "test_coverage.py", "test_api.py")
  623. def test_sort_report_by_cover(self):
  624. # Sort the text report by the Cover column.
  625. data = self.get_coverage_data(self.LINES_1)
  626. opts = CoverageConfig()
  627. opts.from_args(sort='Cover')
  628. report = self.get_summary_text(data, opts)
  629. self.assert_ordering(report, "test_coverage.py", "test_api.py", "test_backward.py")
  630. def test_sort_report_by_invalid_option(self):
  631. # Sort the text report by a nonsense column.
  632. data = self.get_coverage_data(self.LINES_1)
  633. opts = CoverageConfig()
  634. opts.from_args(sort='Xyzzy')
  635. msg = "Invalid sorting option: 'Xyzzy'"
  636. with self.assertRaisesRegex(CoverageException, msg):
  637. self.get_summary_text(data, opts)