简体   繁体   中英

Exclude SwiftUI previews from code coverage?

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.

Here is a description of working approach (demo with Xcode 13 / iOS 15):

  1. Add explicit Testing (name as you want) configuration for UT

演示1

  1. Add conditional macro TESTING (name as you want) for Testing configuration

演示2

  1. Put preview provider into condition like
struct ContentView: View {
    var body: some View {
        Text("Hello, world!")
            .padding()
    }
}

#if !TESTING
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif
  1. Set Testing configuration for UT schema

演示3

  1. Run UT and observe coverage

演示4

backup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM