Jakub
Jakub

Reputation: 13860

UI Test Case not show code coverage

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://[email protected]/Kettu/so_34718699.git

Upvotes: 27

Views: 5563

Answers (2)

alexem
alexem

Reputation: 31

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

Check Gather coverage data

Upvotes: 2

Arun Balakrishnan
Arun Balakrishnan

Reputation: 1502

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.

Upvotes: 1

Related Questions