Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is code coverage already working for Swift?

Tags:

I am trying to setup test code coverage for a Swift application on Xcode 6 Beta 4.
I was able to do so in the past using ObjectiveC with Xcode 5, however I think I had to call the void __gcov_flush() method, to trigger generation of the .gcda/.gcno files.
I set both properties for instrumentation and code coverage to YES for the app and test targets.
However after running the tests, when I look in:
DerivedData/some subfolders/i386 all I see are files of type:
.d,.dia,.o,.swiftmodule and so on, but nothing that indicates code coverage.

Therefore I have two questions:

  1. Is code coverage expected to work for Swift using Xcode6 beta 4?
  2. If yes and it is necessary to flush gcov first, how do I do that from Swift?

Thanks

like image 603
user3886079 Avatar asked Jul 29 '14 01:07

user3886079


People also ask

How do I enable code coverage in Swift?

To enable code coverage, click the scheme editor in the toolbar. Select the CodecovDemo scheme and choose Edit Scheme. Select the Test action on the left. Check the Code Coverage box to gather coverage data.

Where is code coverage in Xcode 13?

You can find it under the Reports navigator. (View menu > Navigators > Reports or ⌘ - command + 9 ). After you open it, under the latest Test report, you should find a Coverage report, click on that, and it will contain the coverage information of that test run.

How do I check my code coverage?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.


2 Answers

Xcode 6 Beta 4 does not have code coverage for Swift working. There is an open Apple issue with id: 17450338 against it.

like image 51
user3886079 Avatar answered Nov 09 '22 23:11

user3886079


Code coverage for Swift is available in Xcode 7. You can turn it on in the test settings for your scheme, by ticking the checkbox labeled "Gather coverage data."

Note that Apple has introduced a new format for the coverage data, profdata files.

Links:

  • Xcode Overview: Using Code Coverage (Mac) (Apple docs)
  • Xcode Overview: Using Code Coverage (iOS) (Apple docs)
  • Leveraging Swift Code Coverage with Xcode 7 (blog post, not mine)
like image 45
mistercake Avatar answered Nov 10 '22 01:11

mistercake