Hello fellow overflowers,
I'm working on a Swift class whose initializer contains a for loop that runs a certain amount of times according to a parameter of the init. Unfortunately, I cannot show you the exact code, but it's similar to this:
init(numberOfTimes: Int) {
...
for index in 0..<numberOfTimes {
// do some stuff here
// shows 0 coverage
}
...
}
I have several unit tests for this initializer, running the for loop between 0 and 5 times. The tests pass, yet code coverage always marks the inside of the loop as not covered, even though it clearly runs — I can breakpoint inside the loop and every function called in the loop shows as covered.
Furthermore, if I extract the contents of the for loop, the code does appear covered and the overall code coverage of the class increased by nearly 20%:
init(numberOfTimes: Int) {
...
for index in 0..<numberOfTimes {
doOne(index)
// this part still shows 0 coverage
}
...
}
private func doOne(_ index: Int) {
// do same things here
// shows correct coverage
}
Why does this happen? Am I not meeting the correct criteria for code coverage inside the for loop?