test_results.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. """Tests for coverage.py's results analysis."""
  4. from coverage.results import Numbers
  5. from tests.coveragetest import CoverageTest
  6. class NumbersTest(CoverageTest):
  7. """Tests for coverage.py's numeric measurement summaries."""
  8. run_in_temp_dir = False
  9. def test_basic(self):
  10. n1 = Numbers(n_files=1, n_statements=200, n_missing=20)
  11. self.assertEqual(n1.n_statements, 200)
  12. self.assertEqual(n1.n_executed, 180)
  13. self.assertEqual(n1.n_missing, 20)
  14. self.assertEqual(n1.pc_covered, 90)
  15. def test_addition(self):
  16. n1 = Numbers(n_files=1, n_statements=200, n_missing=20)
  17. n2 = Numbers(n_files=1, n_statements=10, n_missing=8)
  18. n3 = n1 + n2
  19. self.assertEqual(n3.n_files, 2)
  20. self.assertEqual(n3.n_statements, 210)
  21. self.assertEqual(n3.n_executed, 182)
  22. self.assertEqual(n3.n_missing, 28)
  23. self.assertAlmostEqual(n3.pc_covered, 86.666666666)
  24. def test_sum(self):
  25. n1 = Numbers(n_files=1, n_statements=200, n_missing=20)
  26. n2 = Numbers(n_files=1, n_statements=10, n_missing=8)
  27. n3 = sum([n1, n2])
  28. self.assertEqual(n3.n_files, 2)
  29. self.assertEqual(n3.n_statements, 210)
  30. self.assertEqual(n3.n_executed, 182)
  31. self.assertEqual(n3.n_missing, 28)
  32. self.assertAlmostEqual(n3.pc_covered, 86.666666666)
  33. def test_pc_covered_str(self):
  34. n0 = Numbers(n_files=1, n_statements=1000, n_missing=0)
  35. n1 = Numbers(n_files=1, n_statements=1000, n_missing=1)
  36. n999 = Numbers(n_files=1, n_statements=1000, n_missing=999)
  37. n1000 = Numbers(n_files=1, n_statements=1000, n_missing=1000)
  38. self.assertEqual(n0.pc_covered_str, "100")
  39. self.assertEqual(n1.pc_covered_str, "99")
  40. self.assertEqual(n999.pc_covered_str, "1")
  41. self.assertEqual(n1000.pc_covered_str, "0")
  42. def test_pc_covered_str_precision(self):
  43. assert Numbers._precision == 0
  44. Numbers.set_precision(1)
  45. n0 = Numbers(n_files=1, n_statements=10000, n_missing=0)
  46. n1 = Numbers(n_files=1, n_statements=10000, n_missing=1)
  47. n9999 = Numbers(n_files=1, n_statements=10000, n_missing=9999)
  48. n10000 = Numbers(n_files=1, n_statements=10000, n_missing=10000)
  49. self.assertEqual(n0.pc_covered_str, "100.0")
  50. self.assertEqual(n1.pc_covered_str, "99.9")
  51. self.assertEqual(n9999.pc_covered_str, "0.1")
  52. self.assertEqual(n10000.pc_covered_str, "0.0")
  53. Numbers.set_precision(0)
  54. def test_covered_ratio(self):
  55. n = Numbers(n_files=1, n_statements=200, n_missing=47)
  56. self.assertEqual(n.ratio_covered, (153, 200))
  57. n = Numbers(
  58. n_files=1, n_statements=200, n_missing=47,
  59. n_branches=10, n_missing_branches=3, n_partial_branches=1000,
  60. )
  61. self.assertEqual(n.ratio_covered, (160, 210))