Code coverage in Xcode is a testing option supported by LLVM. When you
enable code coverage, LLVM instruments the code to gather coverage
data based on the frequency that methods and functions are called. The
code coverage option can collect data to report on tests of
correctness and of performance, whether unit tests or UI tests.
I would like to generate a code-coverage report via Xcode.
I've looked at gcovr http://gcovr.com.
...or is it more prudent to generate a report via xcodebuild?
Is there a tutorial or guide to generating a report via Xcode 8+?
There is -enableCodeCoverage YES option for xcodebuild. You can easily see possibilities of xcodebuild command by typing xcodebuild --help.
Here are Apple guildelines on code coverage usage: https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/07-code_coverage.html
You can even see the coverage report directly from Xcode.
Related
I have an iOS app in Xcode 8.2. It has a test target / scheme, for which “Gather coverage data” is checked in the scheme’s Test / Info settings. Coverage data is not gathered. I see how many times a line was iterated over in the gutter as usual, but the Report navigator’s test runs don’t indicate any coverage at all.
I’m wondering if this is because I’ve set the tests to run hostless, i.e. without needing to actually fire up my app – they’re pure logic tests.
Is this possible?
Yes a hostless XCTest target should gather code coverage data.
'iOS Unit Testing' (aka XCTest) bundles, which test a dynamic framework or something else which doesn't require the application environment to exist to run, should happily collect code coverage data and display it in Xcode. Even of the Host Application is set to None. This works either when running Xcode > Product > Test on the Scheme for the framework under test or on the Scheme for the unit tests themselves (if the test bundle is listed in the Test pane of the Scheme editor).
Your problem must be somewhere else, sorry. Its hard for me to guess what the problem is, I suggest you try making a fresh project and see if you can reproduce the problem.
I'm working iOs app,which uses swift language.It's generating coverage.profdata successfully.But I would like to generate the same thing on SonarQube.
Please let me know if you have any ideas!!
I tried the below link
swift-sonar properties
The SonarQube Swift plugin does not have built-in support to import coverage results.
The only option (and the best one) for you is to use the Generic Coverage Plugin:
First you run your coverage tool to generate the coverage.profdata file
Then you manage to transform this file into the target format supported by the Generic Coverage Plugin
And finally you reference this new coverage file in your sonar-project.properties file (like explained in the documentation of the plugin)
I'm trying to retrieve the coverage test info from Xcode 6 and failed in the attempt.
I'm flush out on applicationDidEnterBackground with __gcov_flush()
I set to YES both "Generate Debug Symbols", "Generate test Coverage files" and "Instrument program flow" as the Apple Technical Q&A explains. (https://developer.apple.com/library/ios/qa/qa1514/_index.html)
I run the app with LLVM 6.0 compiler.
I look for any .gcda files in Library/Developer/XCode/DerivedData/Build/Intermediates/App.build also as Apple Q&A says.
I can not found any coverage file either this directory or elsewhere in my mac.
Seems this is the correct way to do it until Xcode 5.1.
Any ideas are welcome.
I found coverage files in Library/Developer/Xcode/DerivedData/<MyApp - a bunch of random characters>/Build/Intermediates/MyIndex.build/Debug-iphonesimulator/MyApp.build/Objects-normal/x86_64. Did you look there?
I've also found that these only seem to exist for Objective-C files - Swift files are not instrumented and therefore no coverage files appear.
Is there a way to find code coverage using clang++ for C++ code.
I know gcov but it works only on the code compiled with GCC.
EDIT:
Clang should be able to generate code coverage these days (taking the same command line arguements as gcc), but you will probably want a newish build and I'm still having trouble running lcov on the output. (see Clang Code Coverage Invalid Output)
I've written a too called kcov which can collect code coverage as long as you compile with debug info (-g), regardless of the compiler.
It also has a plugin for the address-sanitizer coverage generator, although that stuff isn't working very well yet.
I'd say it's a lot more straight-forward to use than gcov+lcov.
I am using UIAutomation to test an app, and I would like to find out my code coverage. But since javascript has no preprocessor, that means that gcov and similar are not an option. Has anyone come up with a solution for this?
For Xcode version 4.5 and newer:
Set the “Generate Test Coverage Files” build setting to Yes.
Set the “Instrument Program Flow” build setting to Yes.
This will generate code coverage files every time you run your application in the simulator and exit the application. A detailed explanation of these two steps can be found at the beginning of http://qualitycoding.org/xcode-code-coverage/.
For any unit tests, code coverage files will be generated every time you hit the test button and the tests complete. For UIAutomation, it is a little bit more tricky. You have to ensure the application exits at the conclusion of your tests. The easiest way I found to do this is to turn off multitasking. Add UIApplicationExitsOnSuspend in your MyAppName-Info.plist file and set this option to 'YES'. Run your UI automation test and at the end of it you can exit the app either by manually pressing the home button in the simulator or using the UIATarget.localTarget().deactivateAppForDuration() method.
Note: if your app has any UI Automation tests that rely on the deactivateAppForDuration() method, the tests will terminate upon running the command.
Code Coverage is only used for Unit Testing, there is no Code Coverage for UIAutomation because there is no way to tell how many elements on screen has been "touched" by UIAutoamtion