I'm having trouble getting my code coverage up to min. 90% because XCode takes the PreviewProvider into account.
What should I do? Remove all the SwiftUI previews? Or is there a way I can exclude some lines with 'PreviewProvider' keywords etc.
Xcode ver 12.0 Jenkins for CI slather & cobertura for code coverage
Side question, there is no official test suite available for unit testing SwiftUI components. Do you guys not test them at all, or use third party libraries? I've been using ViewInspector but i dislike that to track the updated state of the component, I need to include testing code in the actual codebase itself.
The easiest way to exclude code from code coverage analysis is to use ExcludeFromCodeCoverage attribute. This attribute tells tooling that class or some of its members are not planned to be covered with tests. EditFormModel class shown above can be left out from code coverage by simply adding the attribute.
The PreviewProvider protocol is defined in the SwiftUI framework, which means you can take advantage of the the framework's declarative syntax to configure the previews you create. The current run destination defines the preview on the right, iPhone 12 in this example.
To view the coverage reports: Select the Report Navigator in the navigator pane on the left (⌘8) Select the latest Test run in the navigator pane. Select the Coverage tab in the editor.
Enabling Code Coverage in Xcode Code coverage is enabled in the scheme editor. Click the Covered scheme and choose Edit Scheme.... Select Test on the left and check the checkbox Gather coverage data. That is it.
Here is a description of working approach (demo with Xcode 13 / iOS 15):
struct ContentView: View {
var body: some View {
Text("Hello, world!")
.padding()
}
}
#if !TESTING
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
#endif
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With