Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code coverage in Xcode without tests (for manual run)

Code Coverage is commonly used with tests in Xcode. I would like to use it for manually executed app. Can I do it, possibly with third-party tools?

For example: I build and launch the app on device, perform some actions with it and then look at code coverage results.

like image 719
brigadir Avatar asked Oct 02 '15 14:10

brigadir


2 Answers

Probably you might have already figured it out, but it was possible before Xcode7. The way we achieved this was, to set the "Instrument Program Flow" and "Generate Test Coverage files" flags to Yes, on your project, and later add a "flush" code somewhere inside you application, to write the coverage data files. This "flushing" part actually writes the coverage data files, which can be used later by other tools such as gcovr or lcov, to get your coverage data. Once you interact with the app, either manually or through automated tests, the coverage data gets written.

However, with Xcode7, looks like the coverage data is limited to only Xcode unit tests. I am still trying to figure out, if there is any way to gather the coverage data, by interacting with the application manually, or through automated tests.

like image 159
Eagle Claw Avatar answered Nov 04 '22 09:11

Eagle Claw


The solution was suggested by someone here: https://github.com/SlatherOrg/slather/issues/209

You could try having a XCUITest that sleeps forever, then manually use the app, and see if on termination the coverage files are generated.

I simply tried the solution and it worked perfectly for me:

class FooTests: XCTestCase {
    
    override func setUp() {
        super.setUp()
        let app = XCUIApplication()
        app.launch()
    }
    
    func testBalances() {
        sleep(30)
        XCTAssert(true)
    }
}

After the test succeeded on XCTAssert(true), I could see the code coverage for the manual use cases. You can play around with the sleep(30) to match your requirement.

like image 20
Raunak Avatar answered Nov 04 '22 11:11

Raunak