-->

UI Test Case not show code coverage

2019-02-03 04:03发布

问题:

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:

And result:

Still 0%.

Xcode 7.2 (7C68)

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

回答1:

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



回答2:

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.

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

And with your UITestcase it is as follows

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