代码覆盖率结果不准确到Xcode 7中的实际覆盖率 [英] Code coverage result is not accurate to real coverage in Xcode 7

查看:157
本文介绍了代码覆盖率结果不准确到Xcode 7中的实际覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中运行测试用例,启用了代码覆盖率数据Xcode 7 Beta 2.但是,在我的所有测试用例成功运行的情况下,我只能获得少量文件覆盖率数据。

I am running test cases in application with enabled code coverage data Xcode 7 Beta 2. But I am able to get only few files coverage data while my all test cases are running successfully.

有些文件已经涵盖了单元测试用例的所有代码,但仍显示3%的代码覆盖率。

Some files has covered all codes by unit test cases but still showing 3% code coverage.

例如:


这是代码覆盖的结果,正如您在右侧所看到的,有一个信息在测试期间调用这些代码行的次数。在这种情况下 - 0。

This is the result of code coverage, as you can see on the right side, there is an info how many times these lines of code was called during tests. In this case - 0.

但是......


这里是测试中的一个地方,我们可以看到这个函数确实被调用了。多少次?哦......至少一次。这个数字是通过右侧的信息提供的。

here is a place in tests where we can see that this function was called indeed. How many times? oh... at least once. This number is delivered by info on the right side.

所以上面的代码应该被标记为被调用,而不是变灰: - )

So the code above should be marked as called, and not be grayed out:-)

任何人都能解释一下吗?为什么会这样?

Can anyone explain this? Why does this happen?

推荐答案

IT工作


  1. 由于Apple发布了 @testable 关键字以将项目导入测试目标,因此您不必再将文件添加到两个目标:

  1. Since Apple released @testable keyword to import your project into test target, you don't have to add your files to both target anymore:


  1. 所以只需从测试目标中删除每个文件:


  1. 无论您需要从测试目标访问文件,只需使用以下命令导入目标: @testable import MyApp

  1. Wherever you need access to your file from your test target just import your target using: @testable import MyApp


  1. 这样做对于项目中的每个文件。

然后代码覆盖率将正常工作。

Then code coverage will be working fine.

Swift 2 + Xcode 7:单元测试访问制作中了解更多信息简单!!!!

如果您需要知道如何使用代码覆盖率,请阅读如何在Xcode 7中使用代码覆盖率?

If you need to know how to work with code coverage read How to use code coverage in Xcode 7?

As @前面提到的Gerd Castan是:因此,在我看来,当存在至少一个未测试此方法的目标时,测试方法显示覆盖率为0.

As @Gerd Castan mentioned earlier is: "So it appears to me that a tested method shows a coverage of 0 when there exists at least one target where this method is not tested."

解决方案很简单。不要让编译器认为这个文件包含在多个目标中,而是使用 @testable 关键字导入模块。

Solution is simple. Do not let compiler think that this file is included in more that one target, instead import your module using @testable keyword.

这篇关于代码覆盖率结果不准确到Xcode 7中的实际覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆