UI Test Case not show code coverage

2019-02-03 03:43发布

I have some tests written using XCTestCase classes and I want to calculate code coverage. For the regular test it is shown nicely in my bot, but for UI Tests is always 0%.

The simplest test:

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()
    }        

}

And this surely should show some test coverage but it's not. I set in my bot code coverage enabled:

enter image description here

And result:

enter image description here

Still 0%.

Xcode 7.2 (7C68)

EDIT: Example Project : https://Kettu@bitbucket.org/Kettu/so_34718699.git

2条回答
时光不老,我们不散
2楼-- · 2019-02-03 04:01

This happens if you have only UI test target. Add a Unit Test Target. And then run tests again. This time you will see the code coverage.Sample Targets

enter image description here

UITestcase does not provide coverage data without Unit testing code. You could try this. Have a button in your Viewcontroller, attach an IBAction to it. And create UITestcase to it. Check the code coverage with and without UITestcases. You would see the difference. The difference would give you the code coverage by the UITestcase. As of now, Xcode doesnt provide, code coverage data seperately. It is dependant on the Unit Testcases.

Test coverage without UI TestCase with UI Testcase

---With your code I have commented out your

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()

}

from TestDemoUITests and the result was this without UITesting code

And with your UITestcase it is as follows with UITestcase

It is pretty much evident now that UITestcase adds to the Unit Testcase for code coverage.

查看更多
在下西门庆
3楼-- · 2019-02-03 04:14

Check if the "Gather coverage data" is active in the Test Scheme.

Check Gather coverage data

查看更多
登录 后发表回答