How to use code coverage in Xcode 7? - xcode

I know about new feature in Xcode 7 - Code Coverage. I know how to turn it on:
go to Edit scheme -> Test -> Info tab -> enable Gather coverage data.
I did it, run tests, and... Where is Test Report for my tests?

Since you turned it on in your scheme, and then build and test:
Go to your Report navigator
Then select your last test:
Finally choose Coverage tab:

Remember also to enabled test coverage in your bot settings, there is new picker in Xcode 7 (Disabled by default)

Related

How do I show code coverage in Swift Packages?

On my M1 mac, using Xcode 13.3, I created a package and displayed the code coverage bar (Editor menu –> Code Coverage).
After running tests, there is no indication of code coverage at all in the source code.
How do I get code coverage when testing a package?
The problem is that you didn't turn on Code Coverage for the library's scheme. Turn it on:
See the checkbox at the bottom: Code Coverage? Check that checkbox.
Now run the package test and coverage will be gathered. The best way to see it, in my experience, is actually in the report navigator:

Xcode - is adhoc build debug or release?

I have flag in other swift flags for debug so I can have macro in code for DEBUG. Will it trigger in ad-hoc ? How will work ad-hoc build ? As debug or release ?
The default is release but you can change this in the scheme editor
Watch your scheme details to see which configuration is used for the Archive stage (as I assume you're making the Ad-Hoc build from the archived product).
In the menu bar, navigate to Product > Scheme > Edit Scheme… and switch to the one you're currently building if needed.

How do I automatically perform unit tests on each build?

How do I automatically perform unit tests on each build?
I tried to add the Unit Tests target to the Project Target as a dependency, but that doesn't seem to run the actual tests.
Set the Test After Build build setting to Yes. Choose Product > Build For > Build For Testing to build the project and run the tests.
Xcode 5 Update
Xcode 5 does not support the Test After Build build setting. I don't know of any way to automatically run tests on each build in Xcode 5. From the OS X menu bar choose Product > Test or press Command + u to run unit tests in Xcode 5.
To establish the relationship between your targets:
In the Scheme picker, select "Edit Scheme…"
Select the Test phase
Make sure you're seeing the Info tab, not the Arguments tab
Click '+' and specify your testing target
You should see a list of tests. Click 'OK'
Then to run the tests, choose Product > Test, or simply ⌘U from the keyboard. This will:
Build your main target
Build your test target
Execute your main target according to its type, running the tests
Here's what I had to do to get Xcode 4 to automatically run unit tests on build.
Enable "Test After Build" for your main target. (See Mark Szymczyk's answer)
Enable your Test target to run during the build phase of your main target's build scheme.
Click Product > Manage Schemes menu item
Double click your target's scheme (I only had one in my list)
Unfold the Build accordion on the left
Click the Build option in the accordion
You should see you test project on the right, make sure the Run checkbox is clicked.
I thought I'd give a high-level explanation of the steps that worked for me. (OS X 10.7.4, Xcode 4.3.3) If you are unfamiliar with things like Build Phases and Schemes, here's a great resource from Apple: http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Conceptual/Xcode4UserGuide/Building/Building.html
For a new project, make sure you check the "Include Unit Tests" checkbox on the page where you enter the name of the new project. Click on the project icon in the Project Navigator to see the two targets that have been created automatically. They'll be called something like "MyCocoaApp" and "MyCocoaAppTests". Select the MyCocoaAppTests target (NOT the MyCocoaApp one!) and select "Build Settings" from the choices at the top of the view. Enter "test after build" in the search bar near the top of the view. You should see a single setting for "Test After Build" and it's set to NO by default. Click on the NO setting and change it to YES. One more step. Bring up the scheme for the project. (Product Menu -> Edit Scheme...) Select "Build" in the column on the left. You should see your two targets and some check boxes to the right. On the line for "MyCocoaAppTests", check the "Run" checkbox and hit "OK". You're done. Type Command-B. Your app will build, the unit tests will build, the unit tests will run and fail on an error that is there by default.
If you have to add unit testing to an existing project, it's a bit more involved. First, follow these instructions by Apple: https://developer.apple.com/library/mac/#documentation/developertools/Conceptual/UnitTesting/02-Setting_Up_Unit_Tests_in_a_Project/setting_up.html#//apple_ref/doc/uid/TP40002143-CH3-SW1
If you don't miss any steps, you should now have unit tests set up that run when you select (Product Menu -> Test). Now, do the two steps described above: 1) Set the "Test After Build" setting to YES for the MyCocoaAppTest target; 2) Check the "Run" checkbox for the MyCocoaAppTest target in the Build section of the Scheme for the app. LAST STEP: You have to add a new "Run Script" build phase to the MyCocoaAppTest target (NOT the MyCocoaApp one!). Select the test target, click on "Build Phases", click on the "Add Build Phase" icon in the lower right, select "Add Run Script". A "Run Script" section will open up below the other build phases. The script field has this text in it: "Type a script or drag a script file from your workspace". In that field, enter: "${SYSTEM_DEVELOPER_DIR}/Tools/RunUnitTests". (Including the quotation marks.)
That's it. Hit Command-B. Your app should build. Your tests should build and then run. Good luck. Phew.

Cannot find any coverage data (ASP.NET MVC2)

I am having some issues with getting Code Coverage working with an out-of-the-box ASP.NET MVC2 Web App
VS2010 Ultimate, File > New Project > ASP.NET MVC 2 Web Application > Yes, Create unit test project with Visual Studio Unit Test. I then Rebuild All, Run All Unit Tests, Go to Code Coverage and get the following message:
Cannot find any coverage data (.coverage or .coveragexml) files. Check test run details for possible errors.
All the unit tests passed. (And I haven't touched a line of code yet)
I did find the following on the web:
http://www.vbforums.com/showthread.php?t=615377
which says to do the following:
Test -> Edit Test Settings -> Local
In the test settings dialog, click
"Data and Diagnostics" Ensure "Code
Coverage" are checked, and
double-click on it Check of the dll's
you want code coverage enabled for.
But, when I go to Test > Edit Test Seetings, all I see is the grayed out menu item stating "No Test Settings Available".
Any ideas?
Edit: slowly gaining traction. See: How to create the vsmdi/testrunconfig file when importing a Visual Studio test project?
I have had this same problem occur when I added a test project from another source (ie added to, but not created in the current solution). When doing this, local.testsettings, Solution.vsmdi, and TraceAndTestImpact.testsettings are NOT created inside your solution.
This fix is really quite simple, though. Simply right click on your solution and click Add -> New item. A new window will appear. On the left-hand side under General and Performance should be Test Settings. This should let you add a .testsettings file that you can now edit.
More information can be found here:
http://msdn.microsoft.com/en-us/library/ee256991%28v=vs.100%29.aspx

VS2008: Unit Testing with Code Coverage doesn't work with /CLR

I'm trying to set up unit testing with code coverage in VS2008, for a C++/CLI DLL which is compiled with /clr (not /clr:safe or /clr:pure - it has to be /clr because it uses MFC).
The unit tests work perfectly but the coverage information only works if I compile with /clr:safe or /clr:pure. For /clr the Code Coverage Results window shows the following message:
Empty results generated: none of the
instrumented binary was used. Look at
test run details for any
instrumentation problems.
I've also tried "going offroad" but when I load the coverage file into VS, it also contains empty results.
Annoyingly I can't find anywhere that specifically says whether Code Coverage works with /CLR, so I just had to try it myself.
If it should work, can anyone see what I'm doing wrong here?
[File]->[New]->[Project]
Select Class Library, enter MyProj as the project name, click OK
Right-click on MyProj project, select [Properties]
Select [Configuration Properties]->[General]
Ensure "Common Language Runtime support" is set to /CLR
Add this code to Class1:
public:
static int calc() { return 69; }
Build solution
[Test]->[New Test]->[Unit Test], click OK, click Create
Add this code to TestMethod1:
Assert::AreEqual(MyProj::Class1::calc(), 69);
Right-click on TestProject1 project, select [References]
Click "Add New Reference"
Select MyProj in the "Projects" tab, click OK, click OK again
[Test]->[Edit Test Run Configuration]->[Local Test Run]
Select [Code Coverage]
Check MyProj.dll, click Apply, click Close
[Test]->[Run]->[All Tests in Solution]
The Test Results window shows TestMethod1 has passed.
The Code Coverage Results window shows the following message:
Empty results generated: none of the
instrumented binary was used. Look at
test run details for any
instrumentation problems.
Right-click on MyProj project, select [Properties]
Select [Configuration Properties]->[General]
Change "Common Language Runtime support" to /CLR:SAFE or /CLR:PURE, click OK
Build solution
[Test]->[Run]->[All Tests In Solution]
The Test Results window shows TestMethod1 has passed.
The Code Coverage Results window now shows correct coverage information.
Based on http://msdn.microsoft.com/en-us/library/ms182534.aspx
1-project must be in debug
2.-in the project properties you must select x 86 platform.
3 Unregister the project being tested in the GAC.
Yeh, I think it all has to be in CLR:Safe project to work. I don't fully understand why, but I'm in the same boat as you.

Resources