Python "ZeroDivisionError: division by zero" in CI
Dividing by zero raises ZeroDivisionError. In CI this typically appears when a denominator derived from a count, length, or total is zero because the input set was empty.
What this error means
A test or metric calculation fails with "ZeroDivisionError: division by zero" on an average, rate, or percentage computation.
python
avg = total / len(values)
ZeroDivisionError: division by zeroCommon causes
The denominator is an empty count
A len(...), sum, or count evaluated to zero because no rows matched, then code divided by it.
A configured rate or step was left at zero
A divisor read from config defaulted to 0 in the CI environment.
How to fix it
Guard the denominator
- Return a sensible default (0, None) when the denominator is zero instead of dividing.
- Validate config-supplied divisors are non-zero at load time.
- Add a test for the empty-input case so the guard is covered.
Python
avg = total / len(values) if values else 0.0How to prevent it
- Always handle the empty-collection case in aggregate math.
- Validate any config divisor is non-zero before use.
- Add explicit tests for zero-count edge cases.
Related guides
Python "OverflowError" in CIFix "OverflowError" in CI - a float computation exceeded the representable range, often math.exp or a power o…
Python "IndexError: list index out of range" in CIFix "IndexError: list index out of range" in CI - test code indexed past the end of a list, often because sha…
Python "RecursionError" during serialization in CIFix "RecursionError: maximum recursion depth exceeded" while serializing objects in CI - a cyclic reference o…