Capturing Code Coverage for Coded UI Tests
You would think that simply running coded UI tests with everything setup for capturing code coverage would just work, right? And if you’re running your coded UI tests on a 32-bit operating system, it will.
However, if you’re running your tests on a 64-bit operating system, chances are you’ll see the test results (pass/fail), but you won’t see any code coverage results. Why not? And how do you fix it?
By default, the test runner in Visual Studio (and on the TFS build server) run as a 32-bit process, which also means that the code coverage tools are the 32-bit versions. When you run unit tests, the test runner loads all the assemblies into the test runner’s process, which means that .NET assemblies will be run as 32-bit assemblies.
The problem occurs when you used coded UI tests. In this case, the executable assembly is not loaded into the test runners process. Instead, it’s launched in a new process. If you assembly is marked as Any CPU, and you running on a 64-bit OS, the application you’re testing will now be a 64-bit application, which means the 32-bit code coverage code won’t be able to capture any code coverage for your code UI tests.
How do you fix this? Basically you want to make sure the builds used for test runs have the EXE targeted for 32-bit rather than Any CPU. One way is to modify the solution so it builds targets the main EXE to 32-bit, as shown here (this is the Configuration Manager dialog for the solution):
The other option is to change the target platform in the build definition
Using either approach will force the assembly that you want to test to be built for the 32-bit platform.