代码覆盖率结果与 Xcode 7 中的实际覆盖率不准确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30976820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Code coverage result is not accurate to real coverage in Xcode 7
提问by Sudhir Kumar
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.
我在应用程序中运行测试用例并启用代码覆盖率数据 Xcode 7 Beta 2。但是当我的所有测试用例都成功运行时,我只能获得很少的文件覆盖率数据。
Some files has covered all codes by unit test cases but still showing 3% code coverage.
有些文件通过单元测试用例覆盖了所有代码,但仍然显示 3% 的代码覆盖率。
For example:
例如:
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.
这是代码覆盖率的结果,正如您在右侧看到的,有一个信息,这些代码行在测试期间被调用了多少次。在这种情况下 - 0。
But...
但...
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?
谁能解释一下?为什么会发生这种情况?
回答by Bart?omiej Semańczyk
IT WORKS.
它有效。
- Since Apple released @testablekeyword to import your project into test target, you don't have to add your files to both target anymore:
- 由于 Apple 发布了@testable关键字以将您的项目导入测试目标,因此您不必再将文件添加到两个目标:
- So just remove every file from your test target:
- 因此,只需从测试目标中删除每个文件:
- Wherever you need access to your file from your test target just import your target using:
@testable import MyApp
- 无论您需要在何处从测试目标访问您的文件,只需使用以下命令导入您的目标:
@testable import MyApp
- Do this for every file in your project.
- 对项目中的每个文件执行此操作。
Then code coverage will be working fine.
然后代码覆盖将正常工作。
Read more from Swift 2 + Xcode 7: Unit Testing Access Made Easy!!!!
阅读更多来自Swift 2 + Xcode 7:单元测试访问变得容易!!!!
If you need to know how to work with code coverage read How to use code coverage in Xcode 7?
如果您需要知道如何使用代码覆盖率,请阅读如何在 Xcode 7 中使用代码覆盖率?
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."
正如@Gerd Castan 之前提到的那样:“所以在我看来,当至少存在一个未测试此方法的目标时,已测试方法显示的覆盖率为 0。”
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.
解决方法很简单。不要让编译器认为该文件包含在多个目标中,而是使用@testable
关键字导入您的模块。
回答by Gerd Castan
I think I found out what XCTest coverage ist doing and it makes some sense:
我想我发现了 XCTest 的覆盖范围正在做什么并且它有一定的意义:
My setup:
我的设置:
class1 compiled into target1
class1 编译成 target1
class2 compiled into target1 and into target2
class2 编译成 target1 和 target2
Test setup:
测试设置:
import XCTest
@testable import target1
class MyTests: XCTestCase {
func testSomething() {
someMethodFromClass1()
someMethodFromClass2()
}
}
What I find is that class1 (compiled into target1) shows test coverage and class2 (compiled into target1 and into target2) shows no test coverage.
我发现 class1(编译到 target1)显示测试覆盖率,而 class2(编译到 target1 和 target2)显示没有测试覆盖率。
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.
因此,在我看来,当至少存在一个未测试此方法的目标时,已测试方法显示的覆盖率为 0。
And this makes a lot of sense, because testing a method in a target doesn't say anything about how it behaves in a different target.
这很有意义,因为在目标中测试方法并没有说明它在不同目标中的行为。
Apple wants us to test all targets.
苹果希望我们测试所有目标。
UpdateOne more hint to back this theory:
更新另一个提示来支持这一理论:
go to the report navigator
转到报告导航器
and click on coverage.
并单击覆盖范围。
If you have more than one target, you see your files grouped by target.
如果您有多个目标,您会看到按目标分组的文件。
And if you have one file in two targets, you see your file twice.
如果你在两个目标中有一个文件,你会看到你的文件两次。
If you have one file in both targets, the code coverage of this one file is shown for both targets. And (at least in my projects) one file has different blue lines in each target:
如果您在两个目标中都有一个文件,则会为两个目标显示该文件的代码覆盖率。并且(至少在我的项目中)一个文件在每个目标中有不同的蓝线:
coverage in target 1:
目标 1 的覆盖范围:
coverage of same file in the same project in the same test run in target 2:
目标 2 中同一测试运行中同一项目中同一文件的覆盖范围:
If you look at your test coverage in the source editor, apple has to decide which coverage it shows to you. I think showing the target with the lowest coverage is the best apple can do in the source editor.
如果您在源代码编辑器中查看测试覆盖率,Apple 必须决定它向您显示的覆盖率。我认为显示覆盖率最低的目标是苹果在源代码编辑器中可以做的最好的事情。
simple fix for a special case:
特殊情况的简单修复:
If your only second target is your test target: don't compile into your test target and use @testable import
.
如果您唯一的第二个目标是您的测试目标:不要编译到您的测试目标中并使用@testable import
.
For all other cases you have to test each target.
对于所有其他情况,您必须测试每个目标。
回答by courteouselk
I checked at Apple developers forums for the subject and after reading through various posts I guess I came across the solution.
我在 Apple 开发者论坛上查看了该主题,在阅读了各种帖子后,我想我遇到了解决方案。
In order for the thing to work it is necessary to:
为了使事情发挥作用,有必要:
- Remove all your application source files from the test target
- In your unit-test sources put
@testable import <NameOfYourModule>
- Re-build and re-run tests
- 从测试目标中删除所有应用程序源文件
- 在您的单元测试源中放置
@testable import <NameOfYourModule>
- 重新构建并重新运行测试
I tested this out with my current project, and the results are much better.
我用我当前的项目对此进行了测试,结果要好得多。
Original solution recipe can be found at: http://natashatherobot.com/swift-2-xcode-7-unit-testing-access/
可以在以下位置找到原始解决方案配方:http: //natashatherobot.com/swift-2-xcode-7-unit-testing-access/
Also it seems that the functionality is a bit raw-ish, hence bugs possible, and Apple suggests submitting bug reports when things do not work as expected:
此外,该功能似乎有点原始,因此可能存在错误,Apple 建议在事情未按预期工作时提交错误报告:
I've personally seen code coverage results for some very large projects. Right now the support works best for applications and frameworks. If that's what you're testing, then it would be best if you could file a bug report at https://bugreport.apple.comso that we can investigate your particular circumstances. For that matter, a bug report would be a good thing no matter what type of project you have. If possible, it's best to actually in the project with the report. If you can't do that, describe its setup in as much detail as possible. Pictures are good.
我个人看过一些非常大的项目的代码覆盖率结果。目前,该支持最适合应用程序和框架。如果这就是您要测试的内容,那么您最好可以在https://bugreport.apple.com 上提交错误报告, 以便我们调查您的特定情况。就此而言,无论您拥有什么类型的项目,错误报告都是一件好事。如果可能,最好在项目中实际使用报告。如果您不能这样做,请尽可能详细地描述其设置。图片很好。
Original thread: https://forums.developer.apple.com/message/9733#9733
回答by Dietr1ch
Bear in mind that there are multiple ways to cover code with tests, you may test all functions, or you may be covering all instructions in the functions, but you may not be covering all the execution paths that are possible.
请记住,有多种方法可以用测试覆盖代码,您可以测试所有函数,或者您可能覆盖函数中的所有指令,但您可能没有覆盖所有可能的执行路径。
Or Xcode coverage stuff may be broken, but it's hard to tell if you don't give details on what kind of coverage are you expecting it to check.
或者 Xcode 覆盖范围的内容可能已损坏,但如果您不提供有关您希望它检查哪种覆盖范围的详细信息,则很难判断。
回答by Rakesh Yembaram
This happens because .swift file of your project selected for both targets by default.
发生这种情况是因为默认情况下为两个目标选择了项目的 .swift 文件。
Manually select and remove test target for files works for me.
手动选择和删除文件的测试目标对我有用。