What is Code Coverage?
Code coverage is the way to identify or measure what percentage of the source code of a program or a product has been tested. Also, it helps to analyze which parts of a program or product are tested and which are not covered or partially covered by the collection of test cases. If after executing all the test cases still you find that there are code blocks and lines available that are not covered, it means the program or product has not been thoroughly tested.
Code coverage helps to improve the quality of testing; you can identify the areas where more focus is required to increase the coverage and deliver a quality product.
In this article, we’ll discuss how code coverage can be done for Windows Azure Service using Microsoft Visual Studio Team Suite (VSTS).
Benefit of Code Coverage
Code coverage for Windows Azure services is a bit new in the technology world but Microsoft provides a simple and effective way to perform code coverage analysis using VSTS to get all the benefits of it; some of them are as follows:
- Improve the quality of the product to identify the areas that are not covered by test case execution.
- Help in defining the granular level testing strategy to test assembly, namespace and class, etc.
- Easily find dead or unused code, which is not related to the actual requirement.
- Improve overall test case quality to test untested code or a complex code piece of product.
Steps to Achieve Code Coverage Using VSTS
Code coverage data for Azure services can easily be collected by running test cases on dev fabric using Microsoft VSTS.
Microsoft VSTS allows you to follow a set of steps to achieve detailed, line-by-line statistics for which code has been tested and not tested.
You can follow the steps belo9w to set up the environment and collect the code coverage:
As a pre-requisite, you need to install the items below:
- Install Microsoft Visual Studio 2010/2012.
- Install dev fabric (Windows Azure SDK latest)
Code Coverage Setup
To get code coverage for Windows Azure service, you need to first instrument the service by using the steps below.
1. Open command prompt in privileged mode.
2. Change directory to “Program FilesMicrosoft Visual Studio 10.0Team ToolsPerformance Toolsx64” for VSTS 2010 and “Program Files (x86)Microsoft Visual Studio 11.0Team ToolsPerformance Toolsx64” for VSTS 2012.
cd … Team ToolsPerformance Toolsx64
3. Instrument dlls for the Azure service by running the command vsinstr /coverage “…SampleService.dll”
Instrument dlls for the Azure Service
Code Coverage Collection
After instrumenting the Azure service you need to start the service to capture the code coverage results by using below steps.
1. Start coverage monitor by running the command vsperfcmd /start:coverage /output:”My_coverage”.
2. Open Azure solution and deploy the instrumented Windows Azure service on dev fabric using run key (F5).
Deploy the Instrumented Windows Azure Service
Here, you need to make sure that the coverage monitor is started before deploying the Azure service on dev fabric.
3. Once you run your Azure solution it will open Internet Explorer.
4. Browse the location, Programs->Windows Azure SDK v1.2->Windows Azure SDK Command Prompt and open it in Administrator mode:
Open in Administrator Mode
5. Run the command csrun /devfabric:start in opened “Windows Azure SDK Command Prompt”
6. After starting dev fabric, run the command csrun /devstore:start in opened “Windows Azure SDK Command Prompt”
Run csrun /devstore:start
7. You can Change configuration values i.e. service endpoints, connection strings etc.
8. Once you are done with all the required settings, start executing tests pointing to the instrumented Azure service deployed on dev fabric.
9. After completion of test case execution you need to stop the coverage monitor by running the command vsperfcmd /shutdown in “Command Prompt”.
Run vsperfcmd /shutdown
10. If you get any error waiting for the process …*.exe to shutdown, you need to shutdown the dev fabric by using the command /devfabric:shutdown in “Windows Azure SDK Command Prompt”
11. Now, it’s time to analyze the results, open the file My_coverage.coverage and check results.
Check the Results
Once you open the file, which stored the coverage results, you can see that how many blocks of code were covered and how many were not covered. You can further browse the results and get the detail level of statistics to analyze completely to learn the status of test case execution. After seeing these results you can easily improve your test case suite to cover the rest of the functionality of the product and code blocks that are still uncovered or partial covered.
In this article, we explored that how code coverage can help to improve the overall quality of a program or a product. You can leverage Microsoft VSTS to get code coverage for Windows Azure Service and .Net as well.
Above, we discussed a step by step approach to collect the code coverage numbers and how these numbers can help a developer or a quality engineer to decide which part of the code requires more attention. You can get further detail on MSDN.
About the Author:
Anoop has worked with Microsoft for almost six and half years now and has 11+ years of IT experience. He has worked on end to end delivery of enterprise scale BI/DW projects. He has a strong knowledge of database, data warehouse and business intelligence application design and development. Also, he worked extensively on SQL Server, designing of ETL using SSIS, SSAS, SSRS and SQL Azure.
Anoop is a Microsoft Certified IT Professional (MCITP) in Microsoft SQL Server – Database Development 2008, Business Intelligence 2005 and Microsoft Certified Technology Specialist (MCTS) in Microsoft SQL Server 2008 – Implementation and Maintenance.
Anoop has a Post Graduate degree in Computer Science from Birla Institute of Technology, Mesra, India.