test_coverage.py 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  1. # coding: utf-8
  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. """Tests for coverage.py."""
  5. import coverage
  6. from coverage import env
  7. from coverage.misc import CoverageException
  8. from tests.coveragetest import CoverageTest
  9. class TestCoverageTest(CoverageTest):
  10. """Make sure our complex self.check_coverage method works."""
  11. def test_successful_coverage(self):
  12. # The simplest run possible.
  13. self.check_coverage("""\
  14. a = 1
  15. b = 2
  16. """,
  17. [1,2]
  18. )
  19. # You can provide a list of possible statement matches.
  20. self.check_coverage("""\
  21. a = 1
  22. b = 2
  23. """,
  24. ([100], [1,2], [1723,47]),
  25. )
  26. # You can specify missing lines.
  27. self.check_coverage("""\
  28. a = 1
  29. if a == 2:
  30. a = 3
  31. """,
  32. [1,2,3],
  33. missing="3",
  34. )
  35. # You can specify a list of possible missing lines.
  36. self.check_coverage("""\
  37. a = 1
  38. if a == 2:
  39. a = 3
  40. """,
  41. [1,2,3],
  42. missing=("47-49", "3", "100,102")
  43. )
  44. def test_failed_coverage(self):
  45. # If the lines are wrong, the message shows right and wrong.
  46. with self.assertRaisesRegex(AssertionError, r"\[1, 2] != \[1]"):
  47. self.check_coverage("""\
  48. a = 1
  49. b = 2
  50. """,
  51. [1]
  52. )
  53. # If the list of lines possibilities is wrong, the msg shows right.
  54. msg = r"None of the lines choices matched \[1, 2]"
  55. with self.assertRaisesRegex(AssertionError, msg):
  56. self.check_coverage("""\
  57. a = 1
  58. b = 2
  59. """,
  60. ([1], [2])
  61. )
  62. # If the missing lines are wrong, the message shows right and wrong.
  63. with self.assertRaisesRegex(AssertionError, r"'3' != '37'"):
  64. self.check_coverage("""\
  65. a = 1
  66. if a == 2:
  67. a = 3
  68. """,
  69. [1,2,3],
  70. missing="37",
  71. )
  72. # If the missing lines possibilities are wrong, the msg shows right.
  73. msg = r"None of the missing choices matched '3'"
  74. with self.assertRaisesRegex(AssertionError, msg):
  75. self.check_coverage("""\
  76. a = 1
  77. if a == 2:
  78. a = 3
  79. """,
  80. [1,2,3],
  81. missing=("37", "4-10"),
  82. )
  83. class BasicCoverageTest(CoverageTest):
  84. """The simplest tests, for quick smoke testing of fundamental changes."""
  85. def test_simple(self):
  86. self.check_coverage("""\
  87. a = 1
  88. b = 2
  89. c = 4
  90. # Nothing here
  91. d = 6
  92. """,
  93. [1,2,4,6], report="4 0 0 0 100%")
  94. def test_indentation_wackiness(self):
  95. # Partial final lines are OK.
  96. self.check_coverage("""\
  97. import sys
  98. if not sys.path:
  99. a = 1
  100. """, # indented last line
  101. [1,2,3], "3")
  102. def test_multiline_initializer(self):
  103. self.check_coverage("""\
  104. d = {
  105. 'foo': 1+2,
  106. 'bar': (lambda x: x+1)(1),
  107. 'baz': str(1),
  108. }
  109. e = { 'foo': 1, 'bar': 2 }
  110. """,
  111. [1,7], "")
  112. def test_list_comprehension(self):
  113. self.check_coverage("""\
  114. l = [
  115. 2*i for i in range(10)
  116. if i > 5
  117. ]
  118. assert l == [12, 14, 16, 18]
  119. """,
  120. [1,5], "")
  121. class SimpleStatementTest(CoverageTest):
  122. """Testing simple single-line statements."""
  123. def test_expression(self):
  124. # Bare expressions as statements are tricky: some implementations
  125. # optimize some of them away. All implementations seem to count
  126. # the implicit return at the end as executable.
  127. self.check_coverage("""\
  128. 12
  129. 23
  130. """,
  131. ([1,2],[2]), "")
  132. self.check_coverage("""\
  133. 12
  134. 23
  135. a = 3
  136. """,
  137. ([1,2,3],[3]), "")
  138. self.check_coverage("""\
  139. 1 + 2
  140. 1 + \\
  141. 2
  142. """,
  143. ([1,2], [2]), "")
  144. self.check_coverage("""\
  145. 1 + 2
  146. 1 + \\
  147. 2
  148. a = 4
  149. """,
  150. ([1,2,4], [4]), "")
  151. def test_assert(self):
  152. self.check_coverage("""\
  153. assert (1 + 2)
  154. assert (1 +
  155. 2)
  156. assert (1 + 2), 'the universe is broken'
  157. assert (1 +
  158. 2), \\
  159. 'something is amiss'
  160. """,
  161. [1,2,4,5], "")
  162. def test_assignment(self):
  163. # Simple variable assignment
  164. self.check_coverage("""\
  165. a = (1 + 2)
  166. b = (1 +
  167. 2)
  168. c = \\
  169. 1
  170. """,
  171. [1,2,4], "")
  172. def test_assign_tuple(self):
  173. self.check_coverage("""\
  174. a = 1
  175. a,b,c = 7,8,9
  176. assert a == 7 and b == 8 and c == 9
  177. """,
  178. [1,2,3], "")
  179. def test_more_assignments(self):
  180. self.check_coverage("""\
  181. x = []
  182. d = {}
  183. d[
  184. 4 + len(x)
  185. + 5
  186. ] = \\
  187. d[
  188. 8 ** 2
  189. ] = \\
  190. 9
  191. """,
  192. [1, 2, 3], "")
  193. def test_attribute_assignment(self):
  194. # Attribute assignment
  195. self.check_coverage("""\
  196. class obj: pass
  197. o = obj()
  198. o.foo = (1 + 2)
  199. o.foo = (1 +
  200. 2)
  201. o.foo = \\
  202. 1
  203. """,
  204. [1,2,3,4,6], "")
  205. def test_list_of_attribute_assignment(self):
  206. self.check_coverage("""\
  207. class obj: pass
  208. o = obj()
  209. o.a, o.b = (1 + 2), 3
  210. o.a, o.b = (1 +
  211. 2), (3 +
  212. 4)
  213. o.a, o.b = \\
  214. 1, \\
  215. 2
  216. """,
  217. [1,2,3,4,7], "")
  218. def test_augmented_assignment(self):
  219. self.check_coverage("""\
  220. a = 1
  221. a += 1
  222. a += (1 +
  223. 2)
  224. a += \\
  225. 1
  226. """,
  227. [1,2,3,5], "")
  228. def test_triple_string_stuff(self):
  229. self.check_coverage("""\
  230. a = '''
  231. a multiline
  232. string.
  233. '''
  234. b = '''
  235. long expression
  236. ''' + '''
  237. on many
  238. lines.
  239. '''
  240. c = len('''
  241. long expression
  242. ''' +
  243. '''
  244. on many
  245. lines.
  246. ''')
  247. """,
  248. [1,5,11], "")
  249. def test_pass(self):
  250. # pass is tricky: if it's the only statement in a block, then it is
  251. # "executed". But if it is not the only statement, then it is not.
  252. self.check_coverage("""\
  253. if 1==1:
  254. pass
  255. """,
  256. [1,2], "")
  257. self.check_coverage("""\
  258. def foo():
  259. pass
  260. foo()
  261. """,
  262. [1,2,3], "")
  263. self.check_coverage("""\
  264. def foo():
  265. "doc"
  266. pass
  267. foo()
  268. """,
  269. ([1,3,4], [1,4]), "")
  270. self.check_coverage("""\
  271. class Foo:
  272. def foo(self):
  273. pass
  274. Foo().foo()
  275. """,
  276. [1,2,3,4], "")
  277. self.check_coverage("""\
  278. class Foo:
  279. def foo(self):
  280. "Huh?"
  281. pass
  282. Foo().foo()
  283. """,
  284. ([1,2,4,5], [1,2,5]), "")
  285. def test_del(self):
  286. self.check_coverage("""\
  287. d = { 'a': 1, 'b': 1, 'c': 1, 'd': 1, 'e': 1 }
  288. del d['a']
  289. del d[
  290. 'b'
  291. ]
  292. del d['c'], \\
  293. d['d'], \\
  294. d['e']
  295. assert(len(d.keys()) == 0)
  296. """,
  297. [1,2,3,6,9], "")
  298. def test_print(self):
  299. if env.PY3: # Print statement is gone in Py3k.
  300. self.skipTest("No more print statement in Python 3.")
  301. self.check_coverage("""\
  302. print "hello, world!"
  303. print ("hey: %d" %
  304. 17)
  305. print "goodbye"
  306. print "hello, world!",
  307. print ("hey: %d" %
  308. 17),
  309. print "goodbye",
  310. """,
  311. [1,2,4,5,6,8], "")
  312. def test_raise(self):
  313. self.check_coverage("""\
  314. try:
  315. raise Exception(
  316. "hello %d" %
  317. 17)
  318. except:
  319. pass
  320. """,
  321. [1,2,5,6], "")
  322. def test_return(self):
  323. self.check_coverage("""\
  324. def fn():
  325. a = 1
  326. return a
  327. x = fn()
  328. assert(x == 1)
  329. """,
  330. [1,2,3,5,6], "")
  331. self.check_coverage("""\
  332. def fn():
  333. a = 1
  334. return (
  335. a +
  336. 1)
  337. x = fn()
  338. assert(x == 2)
  339. """,
  340. [1,2,3,7,8], "")
  341. self.check_coverage("""\
  342. def fn():
  343. a = 1
  344. return (a,
  345. a + 1,
  346. a + 2)
  347. x,y,z = fn()
  348. assert x == 1 and y == 2 and z == 3
  349. """,
  350. [1,2,3,7,8], "")
  351. def test_yield(self):
  352. self.check_coverage("""\
  353. def gen():
  354. yield 1
  355. yield (2+
  356. 3+
  357. 4)
  358. yield 1, \\
  359. 2
  360. a,b,c = gen()
  361. assert a == 1 and b == 9 and c == (1,2)
  362. """,
  363. [1,2,3,6,8,9], "")
  364. def test_break(self):
  365. self.check_coverage("""\
  366. for x in range(10):
  367. a = 2 + x
  368. break
  369. a = 4
  370. assert a == 2
  371. """,
  372. [1,2,3,4,5], "4")
  373. def test_continue(self):
  374. self.check_coverage("""\
  375. for x in range(10):
  376. a = 2 + x
  377. continue
  378. a = 4
  379. assert a == 11
  380. """,
  381. [1,2,3,4,5], "4")
  382. def test_strange_unexecuted_continue(self):
  383. # Peephole optimization of jumps to jumps can mean that some statements
  384. # never hit the line tracer. The behavior is different in different
  385. # versions of Python, so don't run this test:
  386. self.skipTest("Expected failure: peephole optimization of jumps to jumps")
  387. self.check_coverage("""\
  388. a = b = c = 0
  389. for n in range(100):
  390. if n % 2:
  391. if n % 4:
  392. a += 1
  393. continue # <-- This line may not be hit.
  394. else:
  395. b += 1
  396. c += 1
  397. assert a == 50 and b == 50 and c == 50
  398. a = b = c = 0
  399. for n in range(100):
  400. if n % 2:
  401. if n % 3:
  402. a += 1
  403. continue # <-- This line is always hit.
  404. else:
  405. b += 1
  406. c += 1
  407. assert a == 33 and b == 50 and c == 50
  408. """,
  409. [1,2,3,4,5,6,8,9,10, 12,13,14,15,16,17,19,20,21], "")
  410. def test_import(self):
  411. self.check_coverage("""\
  412. import string
  413. from sys import path
  414. a = 1
  415. """,
  416. [1,2,3], "")
  417. self.check_coverage("""\
  418. import string
  419. if 1 == 2:
  420. from sys import path
  421. a = 1
  422. """,
  423. [1,2,3,4], "3")
  424. self.check_coverage("""\
  425. import string, \\
  426. os, \\
  427. re
  428. from sys import path, \\
  429. stdout
  430. a = 1
  431. """,
  432. [1,4,6], "")
  433. self.check_coverage("""\
  434. import sys, sys as s
  435. assert s.path == sys.path
  436. """,
  437. [1,2], "")
  438. self.check_coverage("""\
  439. import sys, \\
  440. sys as s
  441. assert s.path == sys.path
  442. """,
  443. [1,3], "")
  444. self.check_coverage("""\
  445. from sys import path, \\
  446. path as p
  447. assert p == path
  448. """,
  449. [1,3], "")
  450. self.check_coverage("""\
  451. from sys import \\
  452. *
  453. assert len(path) > 0
  454. """,
  455. [1,3], "")
  456. def test_global(self):
  457. self.check_coverage("""\
  458. g = h = i = 1
  459. def fn():
  460. global g
  461. global h, \\
  462. i
  463. g = h = i = 2
  464. fn()
  465. assert g == 2 and h == 2 and i == 2
  466. """,
  467. [1,2,6,7,8], "")
  468. self.check_coverage("""\
  469. g = h = i = 1
  470. def fn():
  471. global g; g = 2
  472. fn()
  473. assert g == 2 and h == 1 and i == 1
  474. """,
  475. [1,2,3,4,5], "")
  476. def test_exec(self):
  477. self.check_coverage("""\
  478. a = b = c = 1
  479. exec("a = 2")
  480. exec("b = " +
  481. "c = " +
  482. "2")
  483. assert a == 2 and b == 2 and c == 2
  484. """,
  485. [1,2,3,6], "")
  486. self.check_coverage("""\
  487. vars = {'a': 1, 'b': 1, 'c': 1}
  488. exec("a = 2", vars)
  489. exec("b = " +
  490. "c = " +
  491. "2", vars)
  492. assert vars['a'] == 2 and vars['b'] == 2 and vars['c'] == 2
  493. """,
  494. [1,2,3,6], "")
  495. self.check_coverage("""\
  496. globs = {}
  497. locs = {'a': 1, 'b': 1, 'c': 1}
  498. exec("a = 2", globs, locs)
  499. exec("b = " +
  500. "c = " +
  501. "2", globs, locs)
  502. assert locs['a'] == 2 and locs['b'] == 2 and locs['c'] == 2
  503. """,
  504. [1,2,3,4,7], "")
  505. def test_extra_doc_string(self):
  506. self.check_coverage("""\
  507. a = 1
  508. "An extra docstring, should be a comment."
  509. b = 3
  510. assert (a,b) == (1,3)
  511. """,
  512. [1,3,4], "")
  513. self.check_coverage("""\
  514. a = 1
  515. "An extra docstring, should be a comment."
  516. b = 3
  517. 123 # A number for some reason: ignored
  518. 1+1 # An expression: executed.
  519. c = 6
  520. assert (a,b,c) == (1,3,6)
  521. """,
  522. ([1,3,6,7], [1,3,5,6,7], [1,3,4,5,6,7]), "")
  523. def test_nonascii(self):
  524. self.check_coverage("""\
  525. # coding: utf8
  526. a = 2
  527. b = 3
  528. """,
  529. [2, 3]
  530. )
  531. class CompoundStatementTest(CoverageTest):
  532. """Testing coverage of multi-line compound statements."""
  533. def test_statement_list(self):
  534. self.check_coverage("""\
  535. a = 1;
  536. b = 2; c = 3
  537. d = 4; e = 5;
  538. assert (a,b,c,d,e) == (1,2,3,4,5)
  539. """,
  540. [1,2,3,5], "")
  541. def test_if(self):
  542. self.check_coverage("""\
  543. a = 1
  544. if a == 1:
  545. x = 3
  546. assert x == 3
  547. if (a ==
  548. 1):
  549. x = 7
  550. assert x == 7
  551. """,
  552. [1,2,3,4,5,7,8], "")
  553. self.check_coverage("""\
  554. a = 1
  555. if a == 1:
  556. x = 3
  557. else:
  558. y = 5
  559. assert x == 3
  560. """,
  561. [1,2,3,5,6], "5")
  562. self.check_coverage("""\
  563. a = 1
  564. if a != 1:
  565. x = 3
  566. else:
  567. y = 5
  568. assert y == 5
  569. """,
  570. [1,2,3,5,6], "3")
  571. self.check_coverage("""\
  572. a = 1; b = 2
  573. if a == 1:
  574. if b == 2:
  575. x = 4
  576. else:
  577. y = 6
  578. else:
  579. z = 8
  580. assert x == 4
  581. """,
  582. [1,2,3,4,6,8,9], "6-8")
  583. def test_elif(self):
  584. self.check_coverage("""\
  585. a = 1; b = 2; c = 3;
  586. if a == 1:
  587. x = 3
  588. elif b == 2:
  589. y = 5
  590. else:
  591. z = 7
  592. assert x == 3
  593. """,
  594. [1,2,3,4,5,7,8], "4-7", report="7 3 4 1 45% 4-7, 2->4",
  595. )
  596. self.check_coverage("""\
  597. a = 1; b = 2; c = 3;
  598. if a != 1:
  599. x = 3
  600. elif b == 2:
  601. y = 5
  602. else:
  603. z = 7
  604. assert y == 5
  605. """,
  606. [1,2,3,4,5,7,8], "3, 7", report="7 2 4 2 64% 3, 7, 2->3, 4->7",
  607. )
  608. self.check_coverage("""\
  609. a = 1; b = 2; c = 3;
  610. if a != 1:
  611. x = 3
  612. elif b != 2:
  613. y = 5
  614. else:
  615. z = 7
  616. assert z == 7
  617. """,
  618. [1,2,3,4,5,7,8], "3, 5", report="7 2 4 2 64% 3, 5, 2->3, 4->5",
  619. )
  620. def test_elif_no_else(self):
  621. self.check_coverage("""\
  622. a = 1; b = 2; c = 3;
  623. if a == 1:
  624. x = 3
  625. elif b == 2:
  626. y = 5
  627. assert x == 3
  628. """,
  629. [1,2,3,4,5,6], "4-5", report="6 2 4 1 50% 4-5, 2->4",
  630. )
  631. self.check_coverage("""\
  632. a = 1; b = 2; c = 3;
  633. if a != 1:
  634. x = 3
  635. elif b == 2:
  636. y = 5
  637. assert y == 5
  638. """,
  639. [1,2,3,4,5,6], "3", report="6 1 4 2 70% 3, 2->3, 4->6",
  640. )
  641. def test_elif_bizarre(self):
  642. self.check_coverage("""\
  643. def f(self):
  644. if self==1:
  645. x = 3
  646. elif self.m('fred'):
  647. x = 5
  648. elif (g==1) and (b==2):
  649. x = 7
  650. elif self.m('fred')==True:
  651. x = 9
  652. elif ((g==1) and (b==2))==True:
  653. x = 11
  654. else:
  655. x = 13
  656. """,
  657. [1,2,3,4,5,6,7,8,9,10,11,13], "2-13")
  658. def test_split_if(self):
  659. self.check_coverage("""\
  660. a = 1; b = 2; c = 3;
  661. if \\
  662. a == 1:
  663. x = 3
  664. elif \\
  665. b == 2:
  666. y = 5
  667. else:
  668. z = 7
  669. assert x == 3
  670. """,
  671. [1,2,4,5,7,9,10], "5-9")
  672. self.check_coverage("""\
  673. a = 1; b = 2; c = 3;
  674. if \\
  675. a != 1:
  676. x = 3
  677. elif \\
  678. b == 2:
  679. y = 5
  680. else:
  681. z = 7
  682. assert y == 5
  683. """,
  684. [1,2,4,5,7,9,10], "4, 9")
  685. self.check_coverage("""\
  686. a = 1; b = 2; c = 3;
  687. if \\
  688. a != 1:
  689. x = 3
  690. elif \\
  691. b != 2:
  692. y = 5
  693. else:
  694. z = 7
  695. assert z == 7
  696. """,
  697. [1,2,4,5,7,9,10], "4, 7")
  698. def test_pathological_split_if(self):
  699. self.check_coverage("""\
  700. a = 1; b = 2; c = 3;
  701. if (
  702. a == 1
  703. ):
  704. x = 3
  705. elif (
  706. b == 2
  707. ):
  708. y = 5
  709. else:
  710. z = 7
  711. assert x == 3
  712. """,
  713. [1,2,5,6,9,11,12], "6-11")
  714. self.check_coverage("""\
  715. a = 1; b = 2; c = 3;
  716. if (
  717. a != 1
  718. ):
  719. x = 3
  720. elif (
  721. b == 2
  722. ):
  723. y = 5
  724. else:
  725. z = 7
  726. assert y == 5
  727. """,
  728. [1,2,5,6,9,11,12], "5, 11")
  729. self.check_coverage("""\
  730. a = 1; b = 2; c = 3;
  731. if (
  732. a != 1
  733. ):
  734. x = 3
  735. elif (
  736. b != 2
  737. ):
  738. y = 5
  739. else:
  740. z = 7
  741. assert z == 7
  742. """,
  743. [1,2,5,6,9,11,12], "5, 9")
  744. def test_absurd_split_if(self):
  745. self.check_coverage("""\
  746. a = 1; b = 2; c = 3;
  747. if a == 1 \\
  748. :
  749. x = 3
  750. elif b == 2 \\
  751. :
  752. y = 5
  753. else:
  754. z = 7
  755. assert x == 3
  756. """,
  757. [1,2,4,5,7,9,10], "5-9")
  758. self.check_coverage("""\
  759. a = 1; b = 2; c = 3;
  760. if a != 1 \\
  761. :
  762. x = 3
  763. elif b == 2 \\
  764. :
  765. y = 5
  766. else:
  767. z = 7
  768. assert y == 5
  769. """,
  770. [1,2,4,5,7,9,10], "4, 9")
  771. self.check_coverage("""\
  772. a = 1; b = 2; c = 3;
  773. if a != 1 \\
  774. :
  775. x = 3
  776. elif b != 2 \\
  777. :
  778. y = 5
  779. else:
  780. z = 7
  781. assert z == 7
  782. """,
  783. [1,2,4,5,7,9,10], "4, 7")
  784. def test_constant_if(self):
  785. self.check_coverage("""\
  786. if 1:
  787. a = 2
  788. assert a == 2
  789. """,
  790. [2,3], "")
  791. def test_while(self):
  792. self.check_coverage("""\
  793. a = 3; b = 0
  794. while a:
  795. b += 1
  796. a -= 1
  797. assert a == 0 and b == 3
  798. """,
  799. [1,2,3,4,5], "")
  800. self.check_coverage("""\
  801. a = 3; b = 0
  802. while a:
  803. b += 1
  804. break
  805. b = 99
  806. assert a == 3 and b == 1
  807. """,
  808. [1,2,3,4,5,6], "5")
  809. def test_while_else(self):
  810. # Take the else branch.
  811. self.check_coverage("""\
  812. a = 3; b = 0
  813. while a:
  814. b += 1
  815. a -= 1
  816. else:
  817. b = 99
  818. assert a == 0 and b == 99
  819. """,
  820. [1,2,3,4,6,7], "")
  821. # Don't take the else branch.
  822. self.check_coverage("""\
  823. a = 3; b = 0
  824. while a:
  825. b += 1
  826. a -= 1
  827. break
  828. b = 123
  829. else:
  830. b = 99
  831. assert a == 2 and b == 1
  832. """,
  833. [1,2,3,4,5,6,8,9], "6-8")
  834. def test_split_while(self):
  835. self.check_coverage("""\
  836. a = 3; b = 0
  837. while \\
  838. a:
  839. b += 1
  840. a -= 1
  841. assert a == 0 and b == 3
  842. """,
  843. [1,2,4,5,6], "")
  844. self.check_coverage("""\
  845. a = 3; b = 0
  846. while (
  847. a
  848. ):
  849. b += 1
  850. a -= 1
  851. assert a == 0 and b == 3
  852. """,
  853. [1,2,5,6,7], "")
  854. def test_for(self):
  855. self.check_coverage("""\
  856. a = 0
  857. for i in [1,2,3,4,5]:
  858. a += i
  859. assert a == 15
  860. """,
  861. [1,2,3,4], "")
  862. self.check_coverage("""\
  863. a = 0
  864. for i in [1,
  865. 2,3,4,
  866. 5]:
  867. a += i
  868. assert a == 15
  869. """,
  870. [1,2,5,6], "")
  871. self.check_coverage("""\
  872. a = 0
  873. for i in [1,2,3,4,5]:
  874. a += i
  875. break
  876. a = 99
  877. assert a == 1
  878. """,
  879. [1,2,3,4,5,6], "5")
  880. def test_for_else(self):
  881. self.check_coverage("""\
  882. a = 0
  883. for i in range(5):
  884. a += i+1
  885. else:
  886. a = 99
  887. assert a == 99
  888. """,
  889. [1,2,3,5,6], "")
  890. self.check_coverage("""\
  891. a = 0
  892. for i in range(5):
  893. a += i+1
  894. break
  895. a = 99
  896. else:
  897. a = 123
  898. assert a == 1
  899. """,
  900. [1,2,3,4,5,7,8], "5-7")
  901. def test_split_for(self):
  902. self.check_coverage("""\
  903. a = 0
  904. for \\
  905. i in [1,2,3,4,5]:
  906. a += i
  907. assert a == 15
  908. """,
  909. [1,2,4,5], "")
  910. self.check_coverage("""\
  911. a = 0
  912. for \\
  913. i in [1,
  914. 2,3,4,
  915. 5]:
  916. a += i
  917. assert a == 15
  918. """,
  919. [1,2,6,7], "")
  920. def test_try_except(self):
  921. self.check_coverage("""\
  922. a = 0
  923. try:
  924. a = 1
  925. except:
  926. a = 99
  927. assert a == 1
  928. """,
  929. [1,2,3,4,5,6], "4-5")
  930. self.check_coverage("""\
  931. a = 0
  932. try:
  933. a = 1
  934. raise Exception("foo")
  935. except:
  936. a = 99
  937. assert a == 99
  938. """,
  939. [1,2,3,4,5,6,7], "")
  940. self.check_coverage("""\
  941. a = 0
  942. try:
  943. a = 1
  944. raise Exception("foo")
  945. except ImportError:
  946. a = 99
  947. except:
  948. a = 123
  949. assert a == 123
  950. """,
  951. [1,2,3,4,5,6,7,8,9], "6")
  952. self.check_coverage("""\
  953. a = 0
  954. try:
  955. a = 1
  956. raise IOError("foo")
  957. except ImportError:
  958. a = 99
  959. except IOError:
  960. a = 17
  961. except:
  962. a = 123
  963. assert a == 17
  964. """,
  965. [1,2,3,4,5,6,7,8,9,10,11], "6, 9-10")
  966. self.check_coverage("""\
  967. a = 0
  968. try:
  969. a = 1
  970. except:
  971. a = 99
  972. else:
  973. a = 123
  974. assert a == 123
  975. """,
  976. [1,2,3,4,5,7,8], "4-5",
  977. arcz=".1 12 23 45 58 37 78 8.",
  978. arcz_missing="45 58",
  979. )
  980. self.check_coverage("""\
  981. a = 0
  982. try:
  983. a = 1
  984. raise Exception("foo")
  985. except:
  986. a = 99
  987. else:
  988. a = 123
  989. assert a == 99
  990. """,
  991. [1,2,3,4,5,6,8,9], "8",
  992. arcz=".1 12 23 34 45 56 69 89 9.",
  993. arcz_missing="89",
  994. )
  995. def test_try_finally(self):
  996. self.check_coverage("""\
  997. a = 0
  998. try:
  999. a = 1
  1000. finally:
  1001. a = 99
  1002. assert a == 99
  1003. """,
  1004. [1,2,3,5,6], "")
  1005. self.check_coverage("""\
  1006. a = 0; b = 0
  1007. try:
  1008. a = 1
  1009. try:
  1010. raise Exception("foo")
  1011. finally:
  1012. b = 123
  1013. except:
  1014. a = 99
  1015. assert a == 99 and b == 123
  1016. """,
  1017. [1,2,3,4,5,7,8,9,10], "")
  1018. def test_function_def(self):
  1019. self.check_coverage("""\
  1020. a = 99
  1021. def foo():
  1022. ''' docstring
  1023. '''
  1024. return 1
  1025. a = foo()
  1026. assert a == 1
  1027. """,
  1028. [1,2,5,7,8], "")
  1029. self.check_coverage("""\
  1030. def foo(
  1031. a,
  1032. b
  1033. ):
  1034. ''' docstring
  1035. '''
  1036. return a+b
  1037. x = foo(17, 23)
  1038. assert x == 40
  1039. """,
  1040. [1,7,9,10], "")
  1041. self.check_coverage("""\
  1042. def foo(
  1043. a = (lambda x: x*2)(10),
  1044. b = (
  1045. lambda x:
  1046. x+1
  1047. )(1)
  1048. ):
  1049. ''' docstring
  1050. '''
  1051. return a+b
  1052. x = foo()
  1053. assert x == 22
  1054. """,
  1055. [1,10,12,13], "")
  1056. def test_class_def(self):
  1057. self.check_coverage("""\
  1058. # A comment.
  1059. class theClass:
  1060. ''' the docstring.
  1061. Don't be fooled.
  1062. '''
  1063. def __init__(self):
  1064. ''' Another docstring. '''
  1065. self.a = 1
  1066. def foo(self):
  1067. return self.a
  1068. x = theClass().foo()
  1069. assert x == 1
  1070. """,
  1071. [2, 6, 8, 10, 11, 13, 14], "",
  1072. arcz="-22 2D DE E-2 23 36 6A A-2 -68 8-6 -AB B-A",
  1073. )
  1074. class ExcludeTest(CoverageTest):
  1075. """Tests of the exclusion feature to mark lines as not covered."""
  1076. def test_default(self):
  1077. # A number of forms of pragma comment are accepted.
  1078. self.check_coverage("""\
  1079. a = 1
  1080. b = 2 # pragma: no cover
  1081. c = 3
  1082. d = 4 #pragma NOCOVER
  1083. e = 5
  1084. f = 6#\tpragma:\tno cover
  1085. g = 7
  1086. """,
  1087. [1,3,5,7]
  1088. )
  1089. def test_simple(self):
  1090. self.check_coverage("""\
  1091. a = 1; b = 2
  1092. if len([]):
  1093. a = 4 # -cc
  1094. """,
  1095. [1,3], "", excludes=['-cc'])
  1096. def test_two_excludes(self):
  1097. self.check_coverage("""\
  1098. a = 1; b = 2
  1099. if a == 99:
  1100. a = 4 # -cc
  1101. b = 5
  1102. c = 6 # -xx
  1103. assert a == 1 and b == 2
  1104. """,
  1105. [1,3,5,7], "5", excludes=['-cc', '-xx'])
  1106. def test_excluding_if_suite(self):
  1107. self.check_coverage("""\
  1108. a = 1; b = 2
  1109. if len([]): # not-here
  1110. a = 4
  1111. b = 5
  1112. c = 6
  1113. assert a == 1 and b == 2
  1114. """,
  1115. [1,7], "", excludes=['not-here'])
  1116. def test_excluding_if_but_not_else_suite(self):
  1117. self.check_coverage("""\
  1118. a = 1; b = 2
  1119. if len([]): # not-here
  1120. a = 4
  1121. b = 5
  1122. c = 6
  1123. else:
  1124. a = 8
  1125. b = 9
  1126. assert a == 8 and b == 9
  1127. """,
  1128. [1,8,9,10], "", excludes=['not-here'])
  1129. def test_excluding_else_suite(self):
  1130. self.check_coverage("""\
  1131. a = 1; b = 2
  1132. if 1==1:
  1133. a = 4
  1134. b = 5
  1135. c = 6
  1136. else: #pragma: NO COVER
  1137. a = 8
  1138. b = 9
  1139. assert a == 4 and b == 5 and c == 6
  1140. """,
  1141. [1,3,4,5,6,10], "", excludes=['#pragma: NO COVER'])
  1142. self.check_coverage("""\
  1143. a = 1; b = 2
  1144. if 1==1:
  1145. a = 4
  1146. b = 5
  1147. c = 6
  1148. # Lots of comments to confuse the else handler.
  1149. # more.
  1150. else: #pragma: NO COVER
  1151. # Comments here too.
  1152. a = 8
  1153. b = 9
  1154. assert a == 4 and b == 5 and c == 6
  1155. """,
  1156. [1,3,4,5,6,17], "", excludes=['#pragma: NO COVER'])
  1157. def test_excluding_elif_suites(self):
  1158. self.check_coverage("""\
  1159. a = 1; b = 2
  1160. if 1==1:
  1161. a = 4
  1162. b = 5
  1163. c = 6
  1164. elif 1==0: #pragma: NO COVER
  1165. a = 8
  1166. b = 9
  1167. else:
  1168. a = 11
  1169. b = 12
  1170. assert a == 4 and b == 5 and c == 6
  1171. """,
  1172. [1,3,4,5,6,11,12,13], "11-12", excludes=['#pragma: NO COVER'])
  1173. def test_excluding_oneline_if(self):
  1174. self.check_coverage("""\
  1175. def foo():
  1176. a = 2
  1177. if len([]): x = 3 # no cover
  1178. b = 4
  1179. foo()
  1180. """,
  1181. [1,2,4,6], "", excludes=["no cover"])
  1182. def test_excluding_a_colon_not_a_suite(self):
  1183. self.check_coverage("""\
  1184. def foo():
  1185. l = list(range(10))
  1186. a = l[:3] # no cover
  1187. b = 4
  1188. foo()
  1189. """,
  1190. [1,2,4,6], "", excludes=["no cover"])
  1191. def test_excluding_for_suite(self):
  1192. self.check_coverage("""\
  1193. a = 0
  1194. for i in [1,2,3,4,5]: #pragma: NO COVER
  1195. a += i
  1196. assert a == 15
  1197. """,
  1198. [1,4], "", excludes=['#pragma: NO COVER'])
  1199. self.check_coverage("""\
  1200. a = 0
  1201. for i in [1,
  1202. 2,3,4,
  1203. 5]: #pragma: NO COVER
  1204. a += i
  1205. assert a == 15
  1206. """,
  1207. [1,6], "", excludes=['#pragma: NO COVER'])
  1208. self.check_coverage("""\
  1209. a = 0
  1210. for i in [1,2,3,4,5
  1211. ]: #pragma: NO COVER
  1212. a += i
  1213. break
  1214. a = 99
  1215. assert a == 1
  1216. """,
  1217. [1,7], "", excludes=['#pragma: NO COVER'])
  1218. def test_excluding_for_else(self):
  1219. self.check_coverage("""\
  1220. a = 0
  1221. for i in range(5):
  1222. a += i+1
  1223. break
  1224. a = 99
  1225. else: #pragma: NO COVER
  1226. a = 123
  1227. assert a == 1
  1228. """,
  1229. [1,2,3,4,5,8], "5", excludes=['#pragma: NO COVER'])
  1230. def test_excluding_while(self):
  1231. self.check_coverage("""\
  1232. a = 3; b = 0
  1233. while a*b: #pragma: NO COVER
  1234. b += 1
  1235. break
  1236. b = 99
  1237. assert a == 3 and b == 0
  1238. """,
  1239. [1,6], "", excludes=['#pragma: NO COVER'])
  1240. self.check_coverage("""\
  1241. a = 3; b = 0
  1242. while (
  1243. a*b
  1244. ): #pragma: NO COVER
  1245. b += 1
  1246. break
  1247. b = 99
  1248. assert a == 3 and b == 0
  1249. """,
  1250. [1,8], "", excludes=['#pragma: NO COVER'])
  1251. def test_excluding_while_else(self):
  1252. self.check_coverage("""\
  1253. a = 3; b = 0
  1254. while a:
  1255. b += 1
  1256. break
  1257. b = 99
  1258. else: #pragma: NO COVER
  1259. b = 123
  1260. assert a == 3 and b == 1
  1261. """,
  1262. [1,2,3,4,5,8], "5", excludes=['#pragma: NO COVER'])
  1263. def test_excluding_try_except(self):
  1264. self.check_coverage("""\
  1265. a = 0
  1266. try:
  1267. a = 1
  1268. except: #pragma: NO COVER
  1269. a = 99
  1270. assert a == 1
  1271. """,
  1272. [1,2,3,6], "", excludes=['#pragma: NO COVER'])
  1273. self.check_coverage("""\
  1274. a = 0
  1275. try:
  1276. a = 1
  1277. raise Exception("foo")
  1278. except:
  1279. a = 99
  1280. assert a == 99
  1281. """,
  1282. [1,2,3,4,5,6,7], "", excludes=['#pragma: NO COVER'])
  1283. self.check_coverage("""\
  1284. a = 0
  1285. try:
  1286. a = 1
  1287. raise Exception("foo")
  1288. except ImportError: #pragma: NO COVER
  1289. a = 99
  1290. except:
  1291. a = 123
  1292. assert a == 123
  1293. """,
  1294. [1,2,3,4,7,8,9], "", excludes=['#pragma: NO COVER'])
  1295. self.check_coverage("""\
  1296. a = 0
  1297. try:
  1298. a = 1
  1299. except: #pragma: NO COVER
  1300. a = 99
  1301. else:
  1302. a = 123
  1303. assert a == 123
  1304. """,
  1305. [1,2,3,7,8], "", excludes=['#pragma: NO COVER'],
  1306. arcz=".1 12 23 37 45 58 78 8.",
  1307. arcz_missing="45 58",
  1308. )
  1309. self.check_coverage("""\
  1310. a = 0
  1311. try:
  1312. a = 1
  1313. raise Exception("foo")
  1314. except:
  1315. a = 99
  1316. else: #pragma: NO COVER
  1317. a = 123
  1318. assert a == 99
  1319. """,
  1320. [1,2,3,4,5,6,9], "", excludes=['#pragma: NO COVER'],
  1321. arcz=".1 12 23 34 45 56 69 89 9.",
  1322. arcz_missing="89",
  1323. )
  1324. def test_excluding_try_except_pass(self):
  1325. self.check_coverage("""\
  1326. a = 0
  1327. try:
  1328. a = 1
  1329. except: #pragma: NO COVER
  1330. x = 2
  1331. assert a == 1
  1332. """,
  1333. [1,2,3,6], "", excludes=['#pragma: NO COVER'])
  1334. self.check_coverage("""\
  1335. a = 0
  1336. try:
  1337. a = 1
  1338. raise Exception("foo")
  1339. except ImportError: #pragma: NO COVER
  1340. x = 2
  1341. except:
  1342. a = 123
  1343. assert a == 123
  1344. """,
  1345. [1,2,3,4,7,8,9], "", excludes=['#pragma: NO COVER'])
  1346. self.check_coverage("""\
  1347. a = 0
  1348. try:
  1349. a = 1
  1350. except: #pragma: NO COVER
  1351. x = 2
  1352. else:
  1353. a = 123
  1354. assert a == 123
  1355. """,
  1356. [1,2,3,7,8], "", excludes=['#pragma: NO COVER'],
  1357. arcz=".1 12 23 37 45 58 78 8.",
  1358. arcz_missing="45 58",
  1359. )
  1360. self.check_coverage("""\
  1361. a = 0
  1362. try:
  1363. a = 1
  1364. raise Exception("foo")
  1365. except:
  1366. a = 99
  1367. else: #pragma: NO COVER
  1368. x = 2
  1369. assert a == 99
  1370. """,
  1371. [1,2,3,4,5,6,9], "", excludes=['#pragma: NO COVER'],
  1372. arcz=".1 12 23 34 45 56 69 89 9.",
  1373. arcz_missing="89",
  1374. )
  1375. def test_excluding_if_pass(self):
  1376. # From a comment on the coverage.py page by Michael McNeil Forbes:
  1377. self.check_coverage("""\
  1378. def f():
  1379. if False: # pragma: no cover
  1380. pass # This line still reported as missing
  1381. if False: # pragma: no cover
  1382. x = 1 # Now it is skipped.
  1383. f()
  1384. """,
  1385. [1,7], "", excludes=["no cover"])
  1386. def test_excluding_function(self):
  1387. self.check_coverage("""\
  1388. def fn(foo): #pragma: NO COVER
  1389. a = 1
  1390. b = 2
  1391. c = 3
  1392. x = 1
  1393. assert x == 1
  1394. """,
  1395. [6,7], "", excludes=['#pragma: NO COVER'])
  1396. def test_excluding_method(self):
  1397. self.check_coverage("""\
  1398. class Fooey:
  1399. def __init__(self):
  1400. self.a = 1
  1401. def foo(self): #pragma: NO COVER
  1402. return self.a
  1403. x = Fooey()
  1404. assert x.a == 1
  1405. """,
  1406. [1,2,3,8,9], "", excludes=['#pragma: NO COVER'])
  1407. def test_excluding_class(self):
  1408. self.check_coverage("""\
  1409. class Fooey: #pragma: NO COVER
  1410. def __init__(self):
  1411. self.a = 1
  1412. def foo(self):
  1413. return self.a
  1414. x = 1
  1415. assert x == 1
  1416. """,
  1417. [8,9], "", excludes=['#pragma: NO COVER'])
  1418. def test_excludes_non_ascii(self):
  1419. self.check_coverage("""\
  1420. # coding: utf-8
  1421. a = 1; b = 2
  1422. if len([]):
  1423. a = 5 # ✘cover
  1424. """,
  1425. [2, 4], "", excludes=['✘cover']
  1426. )
  1427. def test_formfeed(self):
  1428. # https://bitbucket.org/ned/coveragepy/issues/461/multiline-asserts-need-too-many-pragma
  1429. self.check_coverage("""\
  1430. x = 1
  1431. assert len([]) == 0, (
  1432. "This won't happen %s" % ("hello",)
  1433. )
  1434. \f
  1435. x = 6
  1436. assert len([]) == 0, (
  1437. "This won't happen %s" % ("hello",)
  1438. )
  1439. """,
  1440. [1, 6], "", excludes=['assert'],
  1441. )
  1442. class Py24Test(CoverageTest):
  1443. """Tests of new syntax in Python 2.4."""
  1444. def test_function_decorators(self):
  1445. self.check_coverage("""\
  1446. def require_int(func):
  1447. def wrapper(arg):
  1448. assert isinstance(arg, int)
  1449. return func(arg)
  1450. return wrapper
  1451. @require_int
  1452. def p1(arg):
  1453. return arg*2
  1454. assert p1(10) == 20
  1455. """,
  1456. [1,2,3,4,6,8,10,12], "")
  1457. def test_function_decorators_with_args(self):
  1458. self.check_coverage("""\
  1459. def boost_by(extra):
  1460. def decorator(func):
  1461. def wrapper(arg):
  1462. return extra*func(arg)
  1463. return wrapper
  1464. return decorator
  1465. @boost_by(10)
  1466. def boosted(arg):
  1467. return arg*2
  1468. assert boosted(10) == 200
  1469. """,
  1470. [1,2,3,4,5,6,8,10,12], "")
  1471. def test_double_function_decorators(self):
  1472. self.check_coverage("""\
  1473. def require_int(func):
  1474. def wrapper(arg):
  1475. assert isinstance(arg, int)
  1476. return func(arg)
  1477. return wrapper
  1478. def boost_by(extra):
  1479. def decorator(func):
  1480. def wrapper(arg):
  1481. return extra*func(arg)
  1482. return wrapper
  1483. return decorator
  1484. @require_int
  1485. @boost_by(10)
  1486. def boosted1(arg):
  1487. return arg*2
  1488. assert boosted1(10) == 200
  1489. @boost_by(10)
  1490. @require_int
  1491. def boosted2(arg):
  1492. return arg*2
  1493. assert boosted2(10) == 200
  1494. """,
  1495. ([1,2,3,4,5,7,8,9,10,11,12,14,15,17,19,21,22,24,26],
  1496. [1,2,3,4,5,7,8,9,10,11,12,14, 17,19,21, 24,26]), "")
  1497. class Py25Test(CoverageTest):
  1498. """Tests of new syntax in Python 2.5."""
  1499. def test_with_statement(self):
  1500. self.check_coverage("""\
  1501. class Managed:
  1502. def __enter__(self):
  1503. desc = "enter"
  1504. def __exit__(self, type, value, tb):
  1505. desc = "exit"
  1506. m = Managed()
  1507. with m:
  1508. desc = "block1a"
  1509. desc = "block1b"
  1510. try:
  1511. with m:
  1512. desc = "block2"
  1513. raise Exception("Boo!")
  1514. except:
  1515. desc = "caught"
  1516. """,
  1517. [1,2,3,5,6,8,9,10,11,13,14,15,16,17,18], "")
  1518. def test_try_except_finally(self):
  1519. self.check_coverage("""\
  1520. a = 0; b = 0
  1521. try:
  1522. a = 1
  1523. except:
  1524. a = 99
  1525. finally:
  1526. b = 2
  1527. assert a == 1 and b == 2
  1528. """,
  1529. [1,2,3,4,5,7,8], "4-5",
  1530. arcz=".1 12 23 37 45 57 78 8.", arcz_missing="45 57",
  1531. )
  1532. self.check_coverage("""\
  1533. a = 0; b = 0
  1534. try:
  1535. a = 1
  1536. raise Exception("foo")
  1537. except:
  1538. a = 99
  1539. finally:
  1540. b = 2
  1541. assert a == 99 and b == 2
  1542. """,
  1543. [1,2,3,4,5,6,8,9], "",
  1544. arcz=".1 12 23 34 45 56 68 89 9.",
  1545. )
  1546. self.check_coverage("""\
  1547. a = 0; b = 0
  1548. try:
  1549. a = 1
  1550. raise Exception("foo")
  1551. except ImportError:
  1552. a = 99
  1553. except:
  1554. a = 123
  1555. finally:
  1556. b = 2
  1557. assert a == 123 and b == 2
  1558. """,
  1559. [1,2,3,4,5,6,7,8,10,11], "6",
  1560. arcz=".1 12 23 34 45 56 57 78 6A 8A AB B.", arcz_missing="56 6A",
  1561. )
  1562. self.check_coverage("""\
  1563. a = 0; b = 0
  1564. try:
  1565. a = 1
  1566. raise IOError("foo")
  1567. except ImportError:
  1568. a = 99
  1569. except IOError:
  1570. a = 17
  1571. except:
  1572. a = 123
  1573. finally:
  1574. b = 2
  1575. assert a == 17 and b == 2
  1576. """,
  1577. [1,2,3,4,5,6,7,8,9,10,12,13], "6, 9-10",
  1578. arcz=".1 12 23 34 45 56 6C 57 78 8C 79 9A AC CD D.",
  1579. arcz_missing="56 6C 79 9A AC",
  1580. )
  1581. self.check_coverage("""\
  1582. a = 0; b = 0
  1583. try:
  1584. a = 1
  1585. except:
  1586. a = 99
  1587. else:
  1588. a = 123
  1589. finally:
  1590. b = 2
  1591. assert a == 123 and b == 2
  1592. """,
  1593. [1,2,3,4,5,7,9,10], "4-5",
  1594. arcz=".1 12 23 37 45 59 79 9A A.",
  1595. arcz_missing="45 59",
  1596. )
  1597. self.check_coverage("""\
  1598. a = 0; b = 0
  1599. try:
  1600. a = 1
  1601. raise Exception("foo")
  1602. except:
  1603. a = 99
  1604. else:
  1605. a = 123
  1606. finally:
  1607. b = 2
  1608. assert a == 99 and b == 2
  1609. """,
  1610. [1,2,3,4,5,6,8,10,11], "8",
  1611. arcz=".1 12 23 34 45 56 6A 8A AB B.",
  1612. arcz_missing="8A",
  1613. )
  1614. class ModuleTest(CoverageTest):
  1615. """Tests for the module-level behavior of the `coverage` module."""
  1616. run_in_temp_dir = False
  1617. def test_not_singleton(self):
  1618. # You *can* create another coverage object.
  1619. coverage.Coverage()
  1620. coverage.Coverage()
  1621. def test_old_name_and_new_name(self):
  1622. self.assertIs(coverage.coverage, coverage.Coverage)
  1623. class ReportingTest(CoverageTest):
  1624. """Tests of some reporting behavior."""
  1625. # We don't make any temporary files, but we need an empty directory to run
  1626. # the tests in.
  1627. no_files_in_temp_dir = True
  1628. def test_no_data_to_report_on_annotate(self):
  1629. # Reporting with no data produces a nice message and no output
  1630. # directory.
  1631. with self.assertRaisesRegex(CoverageException, "No data to report."):
  1632. self.command_line("annotate -d ann")
  1633. self.assert_doesnt_exist("ann")
  1634. def test_no_data_to_report_on_html(self):
  1635. # Reporting with no data produces a nice message and no output
  1636. # directory.
  1637. with self.assertRaisesRegex(CoverageException, "No data to report."):
  1638. self.command_line("html -d htmlcov")
  1639. self.assert_doesnt_exist("htmlcov")
  1640. def test_no_data_to_report_on_xml(self):
  1641. # Reporting with no data produces a nice message.
  1642. with self.assertRaisesRegex(CoverageException, "No data to report."):
  1643. self.command_line("xml")
  1644. self.assert_doesnt_exist("coverage.xml")