• Blog
  • Info Support
  • Career
  • Training
  • International Group
  • Info Support
  • Blog
  • Career
  • Training
  • International Group
  • Search
logo InfoSupport
  • Latest blogs
  • Popular blogs
  • Experts
      • All
      • Bloggers
      • Speakers
  • Meet us
  • About us
    • nl
    • en
    • .NET
    • 3D printing
    • Advanced Analytics
    • Agile
    • Akka
    • Alexa
    • Algorithms
    • Api's
    • Architectuur
    • Artificial Intelligence
    • ATDD
    • Augmented Reality
    • AWS
    • Azure
    • Big Data
    • Blockchain
    • Business Intelligence
    • Chatbots
    • Cloud
    • Code Combat
    • Cognitive Services
    • Communicatie
    • Containers
    • Continuous Delivery
    • CQRS
    • Cyber Security
    • Dapr
    • Data
    • Data & Analystics
    • Data Science
    • Data Warehousing
    • Databricks
    • DataOps
    • Developers life
    • DevOps
    • Digital Days
    • Digital Twin
    • Docker
    • eHealth
    • Enterprise Architecture
    • Event Sourcing
    • Hacking
    • Infrastructure & Hosting
    • Innovatie
    • Integration
    • Internet of Things
    • Java
    • Machine Learning
    • Microservices
    • Microsoft
    • Microsoft Bot Framework
    • Microsoft Data Platform
    • Mobile Development
    • Mutation Testing
    • Open source
    • Pepper
    • Power BI
    • Privacy & Ethiek
    • Python
    • Quality Assistance & Test
    • Quality Assurance & Test
    • Requirements Management
    • Scala
    • Scratch
    • Security
    • SharePoint
    • Software Architecture
    • Software development
    • Software Factory
    • SQL Server
    • SSL
    • Start-up
    • Startup thinking
    • Stryker
    • Test Quality
    • Testing
    • TLS
    • TypeScript
    • Various
    • Web Development
    • Web-scale IT
    • Xamarin
    • All
    • Bloggers
    • Speakers
Home » Why my coverage didn’t reach 100%
  • Why my coverage didn't reach 100%

    • By Martijn Beenes
    • .NET 16 years ago
    • .NET 0 comments
    • .NET .NET
    Why my coverage didn't reach 100%

    Some time ago I encountered an interesting feature (!) of the test engine within Visual Studio 2005. I had written a couple of unit tests for some code I had developed. After running the tests I reviewed the coverage results. I'd expected to reach 100% coverage. Instead, the coverage results showed me that I had missed a code block.

    By clicking on the method name containing the non-covered code block, I went straight to the code. It looked like this:

    private object GetTypedValue(string type, string value)

    {

       // Return the correct type.

       switch (type)

       {

          case "bigint":

             return Int64.Parse(value);

          case "bit":

             return bool.Parse(value);

          case "char":

          case "nchar":

          case "ntext":

          case "nvarchar":

          case "text":

          case "varchar":

             return value;

          case "datetime":

          case "smalldatetime":

             return DateTime.Parse(value);

          case "decimal":

             return decimal.Parse(value);

          case "float":

          case "real":

             return float.Parse(value);

          case "int":

             return int.Parse(value);

          case "money":

          case "smallmoney":

             return SqlMoney.Parse(value);

          case "smallint":

             return Int16.Parse(value);

          case "xsd:base64Binary":

             return Convert.FromBase64String(value);

          default:

             throw new NotSupportedException("…");

       }

    }

    At first glance I noticed nothing strange. All my code paths seemed to have been executed during the tests. Then I noticed that the begin (“{“) and end (“}”) tags of my method were marked as covered but the same tags specifying the beginning and end of the switch statement were not.

    A college of mine (thanks Ka Wai) pointed me to the Microsoft forums where this problem is discussed. It seems this problem can occur when using a foreach or switch statement. (The problem with the switch statement will only occur when you have 6 or more case statements.)

    When using the coverage capabilities of Visual Studio 2005, some code is compiled with your source code. The CLR is generating some extra code under the covers that is used for the foreach and switch statements. This has to do with some optimization the CLR makes.

    So some extra code is run when the tests are executed. The code coverage tool is therefore actually telling the truth. Some of the IL code isn’t actually covered by the unit tests. Unfortunately the not covered IL code doesn’t correspond with a specific statement in your code so it won’t show up in Visual Studio.

    If you really want to achieve 100% coverage you can use a for loop instead of a foreach statement and a nested if-else construction instead of the switch statement. But I don’t recommend it. In my opinion, you should never change your code in favor of a test or coverage. You should rather favor readability and maintainability of the code.

    Share this

Martijn Beenes

View profile

Related IT training

Go to training website

Related Consultancy solutions

Go to infosupport.com

Related blogs

  • Configuring SQL Server encrypted connections using Powe…

    Configuring SQL Server encrypted connections using Powe… Léon Bouquiet - 1 month ago

  • Where to position SpecFlow in the Test Pyramid?

    Where to position SpecFlow in the Test Pyramid? Ronald Bosma - 4 months ago

  • Building a custom Kubernetes operator in C#

    Building a custom Kubernetes operator in C# Willem Meints - 8 months ago

Data Discovery Channel

  • Data+AI Summit 2023

  • Blijf je Azure cloud omgeving de baas met CloudXcellence

  • MLOps

Nieuwsbrief

* verplichte velden

Contact

  • Head office NL
  • Kruisboog 42
  • 3905 TG Veenendaal
  • T +31 318 552020
  • Call
  • Mail
  • Directions
  • Head office BE
  • Generaal De Wittelaan 17
  • bus 30 2800 Mechelen
  • T +32 15 286370
  • Call
  • Mail
  • Directions

Follow us

  • Twitter
  • Facebook
  • Linkedin
  • Youtube

Newsletter

Sign in

Extra

  • Media Library
  • Disclaimer
  • Algemene voorwaarden
  • ISHBS Webmail
  • Extranet
Beheer cookie toestemming
Deze website maakt gebruik van Functionele en Analytische cookies voor website optimalisatie en statistieken.
Functioneel Always active
De technische opslag of toegang is strikt noodzakelijk voor het legitieme doel het gebruik mogelijk te maken van een specifieke dienst waarom de abonnee of gebruiker uitdrukkelijk heeft gevraagd, of met als enig doel de uitvoering van de transmissie van een communicatie over een elektronisch communicatienetwerk.
Voorkeuren
De technische opslag of toegang is noodzakelijk voor het legitieme doel voorkeuren op te slaan die niet door de abonnee of gebruiker zijn aangevraagd.
Statistieken
De technische opslag of toegang die uitsluitend voor statistische doeleinden wordt gebruikt. De technische opslag of toegang die uitsluitend wordt gebruikt voor anonieme statistische doeleinden. Zonder dagvaarding, vrijwillige naleving door uw Internet Service Provider, of aanvullende gegevens van een derde partij, kan informatie die alleen voor dit doel wordt opgeslagen of opgehaald gewoonlijk niet worden gebruikt om je te identificeren.
Marketing
De technische opslag of toegang is nodig om gebruikersprofielen op te stellen voor het verzenden van reclame, of om de gebruiker op een website of over verschillende websites te volgen voor soortgelijke marketingdoeleinden.
Manage options Manage services Manage vendors Read more about these purposes
Voorkeuren
{title} {title} {title}