Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Code coverage criteria
- **Code coverage** has several **coverage criteria**, sometimes called "metrics" or "types." They include:
- * Function coverage
- * Statement coverage (or line coverage)
- * Branch coverage
- * Condition coverage
- * Path coverage
- For example, say we have this function:
- ```python
- def foo(x, y):
- z = 0
- if x > 0 and y > 0:
- z = x
- return y if (x > y) else z
- ```
- The minimum tests we need to satisfy **function coverage** are:
- ```python
- foo(0, 0)
- ```
- `foo()` was called.
- ---
- The minimum tests we need to satisfy **line coverage** are:
- ```python
- foo(1, 1)
- ```
- Every line ran.
- ---
- The minimum tests we need to satisfy **branch coverage** are:
- ```python
- foo(1, 0)
- foo(1, 1)
- ```
- `z` was and wasn't set to `x`, and we returned both `y` and `z`.
- ---
- The minimum tests we need to satisfy **condition coverage** are:
- ```python
- foo(1, 0)
- foo(0, 1)
- ```
- `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`.
- ---
- The minimum tests we need to satisfy **path coverage** are:
- ```python
- foo(-1, 0)
- foo(0, 0)
- foo(1, 2)
- foo(1, 1)
- ```
- We have every combination of: (a) setting `z` to `0` or `x`, and (b) returning `y` or `z`.
- ---
- *None* of the criteria require testing **every combination of inputs**.
- We have 3 conditions, each of which can be true or false:
- ```python
- x > 0
- y > 0
- x > y
- ```
- So that's 8 total combinations (6 mathematically possible combinations):
- ```python
- # x > 0 y > 0 x > y
- foo(2, 1) # T T T
- foo(1, 1) # T T F
- foo(1, 0) # T F T
- impossible # T F F
- impossible # F T T
- foo(0, 1) # F T F
- foo(0, -1) # F F T
- foo(-1, 0) # F F F
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement