C# Code Coverage Tools: Your Guide to Choosing - NDepend (2023)

I promise I'll come up with a coverage of code coverage tools in no time. In this post, I will go through 6 different options and list their characteristics for you to decide.

But first I want to offer a quick warning and disclaimer.

If you're here because you're looking for a way to let management track the team's code coverage progress,Please stop and reconsider your actions..

I wrote about how in the pastCode coverage should not be a management concern, and this still applies. Code coverage is a tool that developers should use, not something that needs to be done.onefor them. full stop.

With that resolved, let's get back to helping.developerChoose a code coverage monitoring tool.

Want to see a code coverage heatmap of your codebase?

Download NDepend trial version for freeand see how it goes.

What is code coverage and why?

Okay, let's talk a little bit about what code coverage is.

And notice that this will be amuchsimple explanation, ignoring the fact that there are (slightly) different ways of measuring coverage. But the short form is this.Code coverage measures tell you which lines of code your test suite containscorreand which lines do not.

Let's look at the simplest possible example. Here is a rather dubious implementation of division:

1

2

3

4

5

6

7

Advertising e T Share(e T x, e T j)

{

e(j != 0)

give back x / j;

give back 0;

}

Let's assume this is the only method in our codebase. Let's also say that we are writing a solodevice test, from which we call Divide(2, 1) and claim that we must recover 2.

So we would have 67% code coverage. Because?

Well, in this test, the runtime would test the condition and then execute the return x/y statement, which would execute two-thirds of the method. In the absence of other tests, no automated tests run this last line.

So, generally speaking, that's the "what". As for the "why", developers can use code coverage analysis to identify gaps in their testing efforts. For example, code coverage analysis here would tell us, "Hey, you should test the case where you pass a 0 to the method at y."

Foi Sind Code-Coverage-Tools?

Sind was also Code-Coverage-Tools?

Well, as you can imagine, calculating code coverage like I just did would take a bit of work.

So developers did what developers do. They have automated code coverage detection.

In almost every language and technology stack imaginable, you have the opportunity to see how complete the unit test suite covers your code base. (Don't confuse code coverage with an assessment of thequalityof their tests, however. It's just a measure of whether the tests run the code or not.)

(Video) Fix your technical debt and improve your architecture with NDepend with Patrick Smacchia

The result is a variety of tools to choose from, allowing you to see how well your test suite covers your code base. And this can get a little confusing. So today I'm going to show you some of your options in the .NET ecosystem.

Let's take a look at these options.

1. Standard coverage of Visual Studio Code

Before you get ready to download stuff or open your wallet, understand that you canvonhas this ability. depends onYour version of Visual Studio🇧🇷 If you have the Enterprise version, you can start using this feature right away.

And it's as simple as this screenshot.

You can read about it in moreDetails on the Microsoft documentation page🇧🇷 However, this is a nice, comprehensive option that requires very little extra work, as long as you have the right version. And it has interesting features:

  • Reports coverage percentages at various granularities (eg, congregation, class, etc.).
  • You can choose all of your tests or subsets of them.
  • You can paint your IDE, which means you can preview the skin while looking at your code.
  • It comes from Microsoft, so you can expect a lot of maintenance and support if you invest in using it.

2. DotCover by Rider and ReSharper

If you have a Visual Studio Enterprise license, your life is good in many ways. But if you don't, spending $3,000 a year just to see code coverage is likely to be a hindrance.

for waterhe mustbe a crucial factor. Visual Studio Enterprise is an AWESOME tool, but if your only reason to buy is to see code coverage, you have significantly cheaper options.

TakesJetBrains dotCover, for example. that comes withresharper final, which are only $299 eachAgain🇧🇷 So you get coverage analysis and also Rider and ReSharper which are really cool tools.

You also get a lot of features with dotCover. Some highlights include

  • Detailed reports.
  • test runner.
  • Visual Studio and range measurement CI integration.
  • Navigate to Proof of Coverage.
  • An interesting "hotspot" visualization that draws attention to risky methods.
  • IDE painting.

3. Ncrunch

I'm going to change the subject a bit now that I've covered some of the major players in the .NET code coverage tool space. So far, I've listed what I consider "traditional" code coverage tools.

NCcrunchis different.

I'm seriousesa code coverage tool andOrprovide code coverage data. It also provides the IDE color that some of the other tools provide. (If you look at the screenshot of my environment above, you can see red and green dots - these are NCrunch telling me the tests are failing and passing, respectively.)

(Video) Make the most of your code coverage data

But it also offers an amazing feature.

NCrunch constantly runs your tests in real-time as you type. With NCrunch you don't need to run your tests, or even compileto get feedback on whether the code changes break anything. change of pointswhile you write🇧🇷 forksThe price is very reasonable, starting at $159 per seat for a single developer.

Of course, NCrunch has other features besides this great feature:

  • Small and customizable memory footprint.
  • Running tests in parallel.
  • Online exception details and easy debugging built into VS.
  • Code coverage data and performance metrics.

4. Ncover (put in maintenance mode)

So far, we've covered some code coverage tools that come with other products. While they integrate well into your development environment, they don't focus exclusively on the issue of code coverage.

Ncubierta, Despite that,esuniquely focused. By specializing, NCover provides extremely detailed information that not only supports coverage measurement, but also integrates it for the entire team. Tracks trends in coverage and provides detailed reports.

NCover is a comprehensive tool for this purpose. Some features include

  • Detailed, centralized coverage data.
  • Extensive documentation and user support.
  • Compatibility with 32 and 64 bits and optimization of memory consumption.
  • IDE painting.

Unfortunately, NCover is in maintenance mode right now (2022/2023), so don't expect any new features.

5. OpenCover (set to file mode)

Unfortunately, OpenCover is currently (2022/2023) in file mode and will not evolve with the new .NET release.

Since everything so far requires a bit of investment, it's worth asking if there are any free tools out there. And the answer is yes. Here it isOpencollect.

OpenCover supports .NET 2 and higher (Windows only) and works with 32-bit and 64-bit processors and canget it through NuGet🇧🇷 It originally grew out of attempts to develop a tool called PartCover and eventually became its own tool.

features are

  • 32 and 64 bit compatibility.
  • Support for branch and statement coverage (two slightly different methods of measuring coverage).
  • Generation of an XML-based report that is combined withreport generator, creates a nice HTML-based coverage report.
  • Free and open source so you can adapt it to your needs.

6. It depends

I'll end (unsurprisingly on the NDepend blog) by mentioning NDepend.

Although NDepend does not measure code coverage directly, it doeslikes to import data from other coverage toolsand let yourself do somethingSeriouspowerful things with him.

(Video) Adding NDepend to your Nant Script

NDepend offers a ton of metrics out of the box, and when you combine them with imported coverage data, you can perform important assessments of your codebase such as:risk assessment method🇧🇷 You can also take advantage of NDepend's heat map featuresGet powerful visual effectswhere you have good coverage and where you don't.

But my favorite part is how you can integrate that dataCQ Linqto create your own coverage-related rules and policies.

For example, by default NDepend tells you if you have full coverage since the last baseline. However, you can easily customize it, for example to discount all code created by a code generator or all code inherited from a certain type.

When it comes to integrating test coverage data with other metrics about your code, the sky's the limit.

And that brings us back to my initial disclaimer at the beginning.

Code coverage is not something management should use to move developers to action; Instead, developers should use it to ensure they're happy with their codebases. Use one or more of these tools to measure your coverage, then use NDepend to generate serious actionable insights based on coverage.

Then you have the kind of clean, reliable code base that management never worries about.

Eric Dietrich

I'm a passionate software developer and an active blogger. Read more about me belowmy house.

FAQs

How do you code coverage in C#? ›

Understanding Of Code Coverage Analysis In C#
  1. On the Test menu, choose Analyze Code Coverage. Image 1: Analyze code coverage. Code Coverage Results. Image 2: Code coverage results.
  2. To see which lines have been run, choose. Show Code Coverage Coloring IconShow Code Coverage Coloring. Test Method. Image 3: Test method.
27 Jul 2016

How do you choose code coverage? ›

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

Which tool should be used to monitor code coverage? ›

Cobertura. Cobertura is an open-source tool for measuring code coverage. It does so by instrumenting the byte code.

What is 80% code coverage? ›

Put simply, code coverage tells you how much of your code your tests are reaching. 80% code coverage means 80% of your code is executed during test runs. For test-driven development, you need to aim for 100%.

Which is better xUnit or NUnit? ›

MSTest is concerned, the biggest difference between xUnit and the other two test frameworks (NUnit and MSTest) is that xUnit is much more extensible when compared to NUnit and MSTest. The [Fact] attribute is used instead of the [Test] attribute.

What is the best code coverage? ›

With that being said it is generally accepted that 80% coverage is a good goal to aim for. Trying to reach a higher coverage might turn out to be costly, while not necessary producing enough benefit. The first time you run your coverage tool you might find that you have a fairly low percentage of coverage.

What does 75% code coverage mean? ›

Before moving the code in production, Salesforce ensures that your code has a minimum of 75% code coverage. This means that you have tested your code and it would not break in the production environment.

What are the major code coverage tools? ›

Code Coverage tools used
  • Code Cover: It is an Open Source glass-box testing tool which is frequently used in Java and COBOL. ...
  • JaCoCo: ...
  • Open Clover: ...
  • NCover: ...
  • Vector Software: ...
  • Cobertura: ...
  • Test well CTC++:
17 Feb 2021

Is SonarQube code coverage tool? ›

SonarQube itself does not calculate coverage. To include coverage results in your analysis, you must set up a third-party coverage tool and configure SonarQube to import the results produced by that tool. Below, you'll find guidelines and resources, as well as language- and tool-specific analysis parameters.

What are the different types of code coverage? ›

Following are the types of code coverage Analysis:
  • Statement coverage and Block coverage.
  • Function coverage.
  • Function call coverage.
  • Branch coverage.
  • Modified condition/decision coverage.

Which is very effective tool to ensure the test coverage? ›

TestLink. TestLink is another web-based test management tool available for anyone for free of cost. It has better efficiency in terms of maintenance and assessing the Test Coverage.

Is dotCover part of ReSharper? ›

dotCover is a code coverage tool that can work independently of ReSharper but it can be also used together with it. dotCover simply analyzes your code and presents a report that shows how much of your code is covered by unit tests.

Is dotCover command line free? ›

dotCover console runner is a command-line tool distributed free of charge as an archive, as a NuGet Package (Windows, macOS, Linux), or as a . NET global tool.

Is JaCoCo a code coverage tool? ›

JaCoCo stands for Java Code Coverage. It is a free code coverage library for Java, which has been created by the EclEmma team. It creates code coverage reports and integrates well with IDEs like IntelliJ IDEA, Eclipse IDE, etc.

Can you get 100% code coverage? ›

A 100% code coverage does not mean that 100% of lines are covered, but that 100% of the code which must be tested is actually tested.

Can we achieve 100% code coverage? ›

No. 100% code coverage sounds like a good idea on paper, but it's difficult to achieve and can be very expensive. Developers spend too much time on unit tests that don't provide any value, and 100% code coverage can cause more issues in the code than it prevents.

How do you get 100% condition coverage? ›

To achieve 100% condition coverage, your test cases need to demonstrate a true and false outcome for both conditions. For example, a test case where x is equal to 4 demonstrates a true case for both conditions, and a case where x is equal to 7 demonstrates a false case for both conditions.

Does Microsoft use xUnit? ›

Microsoft is using xUnit internally, one of its creators is from Microsoft. xUnit was also created by one of the original authors of NUnit. There are no [Setup] and [Teardown] attributes, this is done using the test class' constructor and an IDisposable. This encourages developers to write cleaner tests.

Who owns NUnit? ›

NUnit
NUnit 2.4.6 GUI on Windows
Original author(s)Charlie Poole, James Newkirk, Alexei Vorontsov, Michael Two, Philip Craig, Rob Prouse, Simone Busoli, Neil Colvin
Developer(s)The NUnit Project, .NET Foundation
Stable release3.13.2 / 27 April 2021
Repositorygithub.com/nunit
8 more rows

What is the best unit testing framework for .NET core? ›

NET framework helps developers build applications for Windows. These applications also need to be tested just like any other software. There are many choices available for testers to test their application. However, the ones that stand out are MSTest, NUnit, and xUnit.Net.

What is the difference between code coverage and test coverage? ›

While code coverage helps you verify if each code in the software application is being executed by existing tests or not, test coverage indicates whether those tests are covering all the functional requirements of the application or not.

What is good test coverage percentage? ›

Test Coverage, Define minimum, optimal and overkill percentage with pros and cons. Test Coverage: Test coverage is a technique where our test cases cover application code and on specific conditions those test cases are met. Minimum Test Coverage Rate: Keeping it between 60 - 70%.

What does 100% statement coverage mean? ›

In statement coverage testing, 100% statement coverage is said to be achieved if all the statements are tested at least once. This type of testing is usually performed by the developers using development tools.

Does 100% condition coverage gives 100% all use coverage? ›

Since 100% condition cannot give 100% p-use or 100% c-use coverage, it also cannot give 100% all-use coverage (which subsumes c-use and p-use coverage).

How much code coverage is required for Trigger? ›

Code Coverage

You must have at least 75% of your Apex covered by unit tests to deploy your code to production environments. All triggers must have at least one line of test coverage.

What is the purpose of code coverage tools? ›

Code coverage tools are specific to particular programming languages. They use a range of criteria to measure coverage, including the number of code lines, methods or functions, branches and conditions. You can use a code coverage tool to identify your codebase parts that are not currently covered by automated tests.

How do code coverage tools work? ›

Code coverage is a white-box testing technique performed to verify the extent to which the code has been executed. Code coverage tools use static instrumentation in which statements monitoring code execution are inserted at critical junctures in the code.

Is Jenkins a code coverage tool? ›

This Jenkins plugin integrates and publishes multiple coverage report types. It has been developed since GSoC 2018.

What are the top 3 benefits of SonarQube? ›

Following are the advantages of SonarQube:
  • SonarQube is open source.
  • SonarQube supports various languages such as C# and Java.
  • SonarQube reports duplicate code, code coverage, unit testing, code complexity historical, and so on.
  • We can integrate SonarQube with build tools, such as Gradle and ant.
3 Nov 2022

Is Maven a code coverage tool? ›

JaCoCo-Maven (abbreviation for Java Code Coverage) plugin is an open-source code coverage tool for Java. It creates code coverage reports and integrates well with IDEs(Integrated development environments) like Eclipse IDE. It also integrates smoothly with CI/CD tools (e.g. Jenkins, Circle CI, etc.)

What are two advantages of code coverage? ›

Benefit of code coverage measurement:

It creates additional test cases to increase coverage. It helps in finding areas of a program not exercised by a set of test cases. It helps in determining a quantitative measure of code coverage, which indirectly measure the quality of the application or product.

How can I improve my new coverage code? ›

There are a number of approaches:
  1. Write More Tests.
  2. Generate Tests Automatically.
  3. Remove Dead/Zombie Code.
  4. Remove Redundant/Cloned Code.
  5. Write/Execute More Sophisticated Tests.

How many test cases are needed for coverage? ›

Condition coverage checks if both the outcomes(“true” or false”) of every condition have been exercised. The outcome of the decision point is only relevant for checking the conditions. It requires two test cases per condition for two outcomes.

What are 3 testing strategies? ›

The test strategy describes the test level to be performed. There are primarily three levels of testing: unit testing, integration testing, and system testing. In most software development organizations, the developers are responsible for unit testing.

How do you automate test coverage? ›

Step 1: Develop metrics for defining good test coverage at your company. Step 2: Map out all your app's features and user scenarios and rank by priority. Step 3: Find the gaps in your current test plan. Step 4: Use automation tools like Rainforest QA to ramp up test coverage.

Why should I use ReSharper? ›

ReSharper Overview

NET developers and teams rely on ReSharper to write and maintain code in a more manageable and enjoyable way, adopt the best coding practices, and deliver higher quality applications faster.

Does ReSharper Support VS 2022? ›

ReSharper 2021.3 release supports Visual Studio 2022 out-of-the-box.

Why do we need ReSharper? ›

Not only does ReSharper warn you when there is a problem in your code, but it also provides quick-fixes to eliminate errors and code smells automatically.

How does dotCover integrate with Visual Studio? ›

dotCover integrates into Visual Studio 2010, 2012, 2013, 2015, 2017, 2019, and 2022.
...
Unit testing and code coverage
  1. Choose ReSharper | Unit Tests | Unit Tests from the main menu. ...
  2. In the Unit Test Explorer window, select the tests whose coverage you want to analyze. ...
  3. Click Cover Unit Tests.
1 Aug 2022

What is NCrunch? ›

NCrunch is a fully automated testing extension, engineered to make coding and testing a breeze. Forget about stopping to run your tests and let NCrunch do the work for you. Code and test at the speed you think!

What is difference between JaCoCo and SonarQube? ›

SonarQube has a broader approval, being mentioned in 163 company stacks & 271 developers stacks; compared to JaCoCo, which is listed in 5 company stacks and 11 developer stacks.

What is code coverage in C#? ›

Code coverage is a measurement of the amount of code that is run by unit tests - either lines, branches, or methods.

Is Selenium a code coverage tool? ›

Code coverage : indicates the percentage of code that is covered by the test cases through both manual testing and Selenium or any other test automation framework. For example, if your source code has a simple if…else loop, the code coverage would be 100% if your test code would cover both the scenarios i.e. if & else.

Does code coverage need 100%? ›

Code coverage measures how effectively automated tests are testing code. There is no set rule on how much unit test code needs to cover production code. Some experts recommend up to 100%. The problem with this approach is that unit tests can be very time-consuming once you pass a certain threshold, say 70%.

What is C0 C1 C2 coverage? ›

C0/C1/C2 is a coverage standards which is correspondence to coverage degree of routes of testing program. C0 Statement coverage-Has each line of the source code been executed? C1 Branch coverage-Has each control structure (such as an if statement) evaluated both to true and false?

What is difference between code coverage and test coverage? ›

While code coverage helps you verify if each code in the software application is being executed by existing tests or not, test coverage indicates whether those tests are covering all the functional requirements of the application or not.

Can we automate code coverage? ›

Automation is the most efficient way to scale up test coverage because it allows you to use the time you would have spent running tests to plan new test scenarios and build new tests. If you're doing manual testing, then there's a linear relationship between test coverage and time spent testing.

Is high code coverage good? ›

A high percentage of code coverage results in lower chances of unidentified bugs. It is best to set a minimum rate of code coverage that must be achieved before testing in production to reduce the chances of bugs being detected later in development.

What is the minimum code coverage? ›

Code Coverage

You must have at least 75% of your Apex covered by unit tests to deploy your code to production environments. All triggers must have at least one line of test coverage. We recommend that you have 100% of your code covered by unit tests, where possible.

What is C0 C1 and Mcdc? ›

C0: Statement Coverage. Number of lines run / Number of lines to be run * 100(%) C1: Branch Coverage. Number of branches run / Number of branches to be run * 100(%) MC/DC: Modified Condition Decision Coverage.

What does C0 and C1 mean? ›

C0 - Gotten char once. C1 - Twice. C2 - Three times.

Videos

1. Episode 15 – NDepends on How Good Your Code Is
(Coding Blocks)
2. Architectural Improvements with NDepend (Patrick Smacchia)
(Andrea Angella)
3. Focusing on code changes since a baseline from Visual Studio
(NDepend)
4. NDepend Trend Charts
(NDepend)
5. Purging the Technical Debt by Using Static Code Analysis Tools
(JetBrainsTV)
6. Static Code Analysis Using PVS-Studio // Static Code Analysis For Java, C++, and C# // Thetips4you
(Thetips4you)
Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated: 12/20/2022

Views: 6424

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.