简体   繁体   English

从代码覆盖范围中排除 SwiftUI 预览?

[英]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. 90% 因为 XCode 考虑了 PreviewProvider。

What should I do?我应该怎么办? Remove all the SwiftUI previews?删除所有 SwiftUI 预览? Or is there a way I can exclude some lines with 'PreviewProvider' keywords etc.或者有没有办法可以排除一些带有“PreviewProvider”关键字等的行。

Xcode ver 12.0 Jenkins for CI slather & cobertura for code coverage Xcode ver 12.0 Jenkins 用于 CI slather & cobertura 用于代码覆盖

Side question, there is no official test suite available for unit testing SwiftUI components.附带问题,没有可用于单元测试 SwiftUI 组件的官方测试套件。 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.我一直在使用 ViewInspector,但我不喜欢用它来跟踪组件的更新状态,我需要在实际代码库本身中包含测试代码。

Here is a description of working approach (demo with Xcode 13 / iOS 15):以下是工作方法的描述(使用 Xcode 13 / iOS 15 进行演示):

  1. Add explicit Testing (name as you want) configuration for UT为 UT 添加显式测试(根据需要命名)配置

演示1

  1. Add conditional macro TESTING (name as you want) for Testing configuration为测试配置添加条件宏 TESTING(您想要的名称)

演示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为 UT 模式设置测试配置

演示3

  1. Run UT and observe coverage运行 UT 并观察覆盖率

演示4

backup 备份

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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