Analyze the following code. Why might it yield a low mutation score despite having tests for positive numbers, negative numbers, and division by zero?

.NET interview question for Advanced practice.

Answer

The test for division by zero likely only asserts the return is 0, which is a weak test. A better practice is to throw an exception, and a test asserting this would kill more mutants.

Explanation

The core issue is that returning 0 for division by zero is a weak error handling strategy; throwing a DivideByZeroException is standard practice. A test for this case probably just asserts Divide(5, 0) == 0. This test is not very robust and may not kill subtle mutants. The most robust test, which would kill more mutants, is one that asserts a specific exception is thrown, like Assert.Throws<DivideByZeroException(() = calculator.Divide(5, 0));. Such a test would fail if any mutant changed the logic to return a value instead of throwing, making the test suite stronger.

Related Questions