To be clear, if the code you’re testing is tightly bound to all that kernel infrastructure, I don’t consider it a unit test. By definition, a unit test is meant to test things in isolation.
Regardless, coming back to your main question, code coverage, I don’t think there’s any Apple infrastructure for doing that in kernel code. Normally I’d recommend that you file an enhancement request for such support, but given this year’s announcements at WWDC I doubt there’s much point doing that.
You may be able to hack something together by repurposing Clang’s user space support. Conceptually, code coverage is pretty simple: The compiler instruments the code to update counters as it runs, and there’s runtime support to dump those counters when the program exits. It’s that last bit you’d need to hack on. I don’t have a lot of experience with this but my initial impression is that this would be feasible but not trivial.
A good place to start is the Clang doc on the subject. After that I recommend that you play around a bit on your own and then, if you get stuck, seek help via the Clang support channels.
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"