Code coverage result is not accurate to real coverage in Xcode 7

前端 未结 5 718
清歌不尽
清歌不尽 2021-02-02 12:18

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 succ

5条回答
  •  醉话见心
    2021-02-02 12:38

    I think I found out what XCTest coverage ist doing and it makes some sense:

    My setup:

    class1 compiled into target1

    class2 compiled into target1 and into 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.

    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.

    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.

    Update One 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:

    coverage of same file in the same project in the same test run in target 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.

    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.

    For all other cases you have to test each target.

提交回复
热议问题