UI Test Case not show code coverage
Asked Answered
D

2

27

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

Deflected answered 11/1, 2016 at 10:4 Comment(4)
Ah, sorry. From your original it sounded like you were expecting it to show in the screenshot :DPlumcot
As far as I understand it, it's not possible to record code coverage for UI tests because they are not executed on source code level.Livorno
Hmmm @dasdom, this sounds legit. Thanks, for pointing that out.Deflected
I believe that the actual problem is the fact that during UI tests the applications is not started only once but multiple times. It's very hard to record correct coverage (including branches) that way. Some smart merging would have to be implemented and the app would have to exit gracefully after every run. We had similar problems with previous Xcode versions and also with UIAutomation before. I belive it's low priority task for Xcode developers. We don't even have a way to get the coverage info for Swift apps from Xcode yet. It only shows in UI.Apocrine
G
2

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

Check Gather coverage data

Gourmont answered 30/6, 2016 at 16:35 Comment(0)
H
1

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.

Humphries answered 25/1, 2016 at 6:50 Comment(6)
That is NOT a coverage from UI TestsDeflected
When I commented out the UI Tests. This is the result.Humphries
This is what i've done in example project you can download from my answer. Code coverage is not shown on method from IBAction.Deflected
Yes it fire for examlple viewDidLoad because It launch the screen, but this is not for example buttonTapped method that is fired. So that its not real test coverage. Check the example project that I posted, and try to test other method than viewDidLoad using UITest.Deflected
Look at the results closely. The two methods segmentedControlValueChanged and stepperValueChanged is invoked for UITest and not unit test. Hence the overall Test Coverage is less than 50% for the project without UITests and it is about 80% with UITests. So 80%-50% = 30% is the code coverage by the UITests.Humphries
You're totally correct. UI Testing does add to the code coverage. Just finished a sample test that pumped the code coverage from 10.3% to 28%.Blat

© 2022 - 2024 — McMap. All rights reserved.