Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. ## Code coverage criteria
  2.  
  3. **Code coverage** has several **coverage criteria**, sometimes called "metrics" or "types." They include:
  4.  
  5. * Function coverage
  6. * Statement coverage (or line coverage)
  7. * Branch coverage
  8. * Condition coverage
  9. * Path coverage
  10.  
  11. For example, say we have this function:
  12.  
  13. ```python
  14. def foo(x, y):
  15.  
  16. z = 0
  17. if x > 0 and y > 0:
  18. z = x
  19.  
  20. return y if (x > y) else z
  21. ```
  22.  
  23. The minimum tests we need to satisfy **function coverage** are:
  24.  
  25. ```python
  26. foo(0, 0)
  27. ```
  28.  
  29. `foo()` was called.
  30.  
  31. ---
  32.  
  33. The minimum tests we need to satisfy **line coverage** are:
  34.  
  35. ```python
  36. foo(1, 1)
  37. ```
  38.  
  39. Every line ran.
  40.  
  41. ---
  42.  
  43. The minimum tests we need to satisfy **branch coverage** are:
  44.  
  45. ```python
  46. foo(1, 0)
  47. foo(1, 1)
  48. ```
  49.  
  50. `z` was and wasn't set to `x`, and we returned both `y` and `z`.
  51.  
  52. ---
  53.  
  54. The minimum tests we need to satisfy **condition coverage** are:
  55.  
  56. ```python
  57. foo(1, 0)
  58. foo(0, 1)
  59. ```
  60.  
  61. `x` was and wasn't greater than `0`, `y` was and wasn't greater than `0`, and `x` was and wasn't greater than `y`.
  62.  
  63. ---
  64.  
  65. The minimum tests we need to satisfy **path coverage** are:
  66.  
  67. ```python
  68. foo(-1, 0)
  69. foo(0, 0)
  70. foo(1, 2)
  71. foo(1, 1)
  72. ```
  73.  
  74. We have every combination of: (a) setting `z` to `0` or `x`, and (b) returning `y` or `z`.
  75.  
  76. ---
  77.  
  78. *None* of the criteria require testing **every combination of inputs**.
  79.  
  80. We have 3 conditions, each of which can be true or false:
  81.  
  82. ```python
  83. x > 0
  84. y > 0
  85. x > y
  86. ```
  87.  
  88. So that's 8 total combinations (6 mathematically possible combinations):
  89.  
  90. ```python
  91. # x > 0 y > 0 x > y
  92. foo(2, 1) # T T T
  93. foo(1, 1) # T T F
  94. foo(1, 0) # T F T
  95. impossible # T F F
  96. impossible # F T T
  97. foo(0, 1) # F T F
  98. foo(0, -1) # F F T
  99. foo(-1, 0) # F F F
  100. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement