UI测试用例不显示代码覆盖率

时间:2016-01-11 10:04:22

标签: ios xcode swift xctest osx-server

我有一些使用XCTestCase类编写的测试,我想计算代码覆盖率。对于常规测试,它在我的机器人中很好地显示,但对于UI测试总是0%。

最简单的测试:

import XCTest

class FAQUITests: XCTestCase {

    let app = XCUIApplication()        
    override func setUp() {
        super.setUp()
        app.launch()
    }

    func openFaqView() {
        app.navigationBars["NavigationBar"].buttons["FAQ"].tap()
    }

    func testFaq() {
        openFaqView()
        app.tables.cells.elementBoundByIndex(0).tap()
    }        

}

这肯定会显示一些测试报道,但事实并非如此。我启用了我的机器人代码覆盖率:

enter image description here

结果:

enter image description here

仍为0%。

Xcode 7.2(7C68)

编辑: 示例项目:https://Kettu@bitbucket.org/Kettu/so_34718699.git

2 个答案:

答案 0 :(得分:2)

检查测试方案中的“收集覆盖数据”是否有效。

Check Gather coverage data

答案 1 :(得分:0)

如果您只有UI测试目标,则会发生这种情况。添加单元测试目标。然后再次运行测试。这次您将看到代码覆盖率。Sample Targets

enter image description here

如果没有单元测试代码,UITestcase不提供覆盖数据。你可以试试这个。在Viewcontroller中有一个按钮,将IBAction附加到它。并为它创建UITestcase。使用和不使用UITestcases检查代码覆盖率。你会看到差异。差异将为您提供UITestcase的代码覆盖率。截至目前,Xcode并未单独提供代码覆盖率数据。它取决于单元测试用例。

没有UI TestCase的测试覆盖率 with UI Testcase

---用你的代码 我已经注释掉了你的

  

func testStepper(){

    let steppersQuery = app.steppers
    steppersQuery.buttons["Increment"].tap()
    steppersQuery.buttons["Decrement"].tap()
}

func testSegmented() {
    app.buttons["Second"].tap()
    app.buttons["Fourth"].tap()
    app.buttons["Third"].tap()
    app.buttons["First"].tap()

}
来自TestDemoUITests的

结果是这样的 without UITesting code

使用您的UITestcase,如下所示 with UITestcase

现在非常明显的是UITestcase为单元测试用例添加了代码覆盖率。