Drill into a Partially Covered Line

Let’s start writing a test for the max function, keeping an eye on the code coverage as we progress. Delete test_zero, then add the beginnings of the new test:

CodeCoverage/CodeCoverageTests/CoveredClassTests.swift
 func​ ​test_max_with1And2_shouldReturnSomething​() {
 let​ result = ​CoveredClass​.​max​(1, 2)
 }

Note that there’s no assertion yet. Run tests and go to CoveredClass.swift to see the line-by-line coverage. It should look something like this:

images/code-coverage/coverage-if-top.png

The code coverage gutter now shows a smaller red area and two numbers. The 0 marks the section that tests haven’t touched. Above that is a 1, marking a section the test touched once. Between the two, the gutter shows a red-striped area. Hover the mouse cursor in that area, and you’ll see things change, as shown here:

images/code-coverage/coverage-if-top-hover.png

The green section shows the code we’ve touched. The line with the else clause is part green, part red. This gives us a way to see code coverage inside lines. Whenever you see a red-striped area, place your mouse cursor there to examine which parts of the line remain untouched.

Of course, the problem with our test is that it has no assertion. This demonstrates the dangerous part of code coverage. Coverage says nothing about the quality of the tests.

images/aside-icons/tip.png

Lack of code coverage proves lack of testing. But positive code coverage doesn’t prove anything.