Visual Studio 2019 Code



  1. Visual Studio 2019 Coded Ui
  2. Visual Studio 2019 Codelens
  3. Visual Studio 2019 Code Coverage
  4. Download Vs Studio Code
  • Code snippets are available for a multitude of languages, including C#, C, Visual Basic, XML, and T-SQL, to name a few. To view all the available installed snippets for a language, open the Code Snippets Manager from the Tools menu (or, press Ctrl+K, Ctrl+B), and choose the language from the drop-down menu at the top. Code snippets can be accessed in the following general ways.
  • Install Code Map and Live Dependency Validation. To create a code map in Visual Studio, first install the Code Map and Live Dependency Validation components. Open Visual Studio Installer.You can open it from the Windows Start menu, or within Visual Studio by selecting Tools Get Tools and Features. Select the Individual components tab. Scroll down to the Code tools section and select Code.

Having a high code coverage percentage boosts your confidence in your code: the more thoroughly your code is tested, the lesser are the possibilities to have bugs. Of course, You'll never have a bug-free project, that's utopian. But you can work toward reducing the possible bugs by testing each and every part of your code.

The term code coverage represents the percentage of code covered by tests: it is calculated basing on two values: line coverage, which is about the exact count of lines covered, and branch coverage which is about the branches (if-else, switch, try-catch) that have been executed by our test suite.

In this article, we're gonna see how to calculate code coverage on .NET projects and how to visualize a Code Coverage report on Visual Studio 2019.

Compared with Visual Studio 2015, Visual Studio 2017 comes with a huge performance boost. The most obvious user experience is the startup speed of VS 2017 is around 3 times faster than VS 2015, and the loading time of project solutions is shortened 24 times.

Setting up a simple project

Let's create a class library with a single class with only one method:

Visual Studio 2019 Code

Then, we have to create a test class and write some tests for the MyArray class:

Visual Studio 2019 Code

The code and the tests are pretty straightforward; but have we really covered the Replace method with enough tests to be sure not to have missed something?

Coverlet - the NuGet Package for code coverage

Visual Studio 2019 Coded Ui

The first thing to do to add code coverage reports to our project is to install Coverlet, a NuGet package, whose documentation can be accessed on GitHub.

You must add Coverlet.msbuildto every test project in your solution. So, add it with the NuGet package manager or with the CLI, running the command dotnet add package coverlet.msbuild.

This package relies on the MSBuild tool to collect code coverage data and statistics, and save them into a specific file that can be opened with other tools or applications.

Installing code coverage collector on Visual Studio 2019

The next step is to install in Visual Studio an extension that, given the code coverage report, displays the result in a human-readable format.

The tool we're gonna use is ReportGenerator, that provides support for projects based on .NET Core.

To install it, open the PowerShell with admin privileges and run the following commands:

These commands install ReportGenerator alongside global .NET tools.

And now, it's time to install the actual Visual Studio extension.

The first step is to head to the Extensions menu and select Manage Extensions.Then, search Run Coverlet Report and install it - you have to close all Visual Studio instances to install it.

Since we are integrating Coverlet with MSBuild, you have to head to Tools > Options and change the Integration Type from Collector to MSBuild.

Once everything is installed (remember to install Coverlet in all and only test projects) there's only one thing to do: try them!

Visual Studio 2019 Codelens

Our first run

First of all, run all of you tests for the first time: this helps to initialize correctly all the references to the test projects. You must do this step only the first time.

Visual

Now, under the Tools menu, click on Run Code Coverage: this command runs the tests, generates a report files and uses it to generate a full report like this:

Here we go! We have our code coverage report!

You can even drill down into details for each class to find out other the values for Branch Coverage, Line Coverage and Cyclomatic complexity.

For each class, you can see the details of the lines covered by tests. But if you are like me, you don't want to open each file to see what to do, but you'd like a way to see it directly in your IDE.

Well, just click on Toggle Code Coverage Highlighting under the Tools menu: you will see all the lines covered by tests in green, and all the ones that aren't covered by any tests in red.

This will help you speed up your development and find out possible bugs and flaws earlier.

Wrapping up

We've seen how easy it is to run code coverage on .NET Core projects.

The installation of the global tool and the extension needs to be done only the very first time. So the only thing to do when you want to see the code coverage report for your projects is to install coverlet.msbuild. Quite easy, uh?

Happy coding!

-->

When debugging a .NET application, you may find that you want to view source code that you don't have. For example, breaking on an exception or using the call stack to navigate to a source location.

Note

  • Source code generation (decompilation) is only available for .NET applications and is based on the open source ILSpy project.
  • Decompilation is only available in Visual Studio 2019 16.5 and later.
  • Applying the SuppressIldasmAttribute attribute to an assembly or module prevents Visual Studio from attempting decompilation.
2019

Generate source code

When you're debugging and no source code is available, Visual Studio shows the Source Not Found document, or if you don’t have symbols for the assembly, the No Symbols Loaded document. Both documents have a Decompile source code option that generates C# code for the current location. The generated C# code can then be used just like any other source code. You can view the code, inspect variables, set breakpoints, and so on.

No symbols loaded

The following illustration shows the No Symbols Loaded message.

Source not found

The following illustration shows the Source Not Found message.

Generate and embed sources for an assembly

In addition to generating source code for a specific location, you can generate all the source code for a given .NET assembly. To do this, go to the Modules window and from the context menu of a .NET assembly, and then select the Decompile source code command. Visual Studio generates a symbol file for the assembly and then embeds the source into the symbol file. In a later step, you can extract the embedded source code.

Extract and view the embedded source code

You can extract source files that are embedded in a symbol file using the Extract Source Code command in the context menu of the Modules window.

The extracted source files are added to the solution as miscellaneous files. The miscellaneous files feature is off by default in Visual Studio. You can enable this feature from the Tools > Options > Environment > Documents > Show Miscellaneous files in Solution Explorer checkbox. Without enabling this feature, you won't be able to open the extracted source code.

Visual Studio 2019 Code Coverage

Extracted source files appear in the miscellaneous files in Solution Explorer.

Known limitations

Requires break mode

Generating source code using decompilation is only possible when the debugger is in break mode and the application is paused. For example, Visual Studio enters break mode when it hits a breakpoint or an exception. You can easily trigger Visual Studio to break the next time your code runs by using the Break All command ().

Decompilation limitations

Generating source code from the intermediate format (IL) that is used in .NET assemblies has some inherent limitations. As such, the generated source code doesn't look like the original source code. Most of the differences are in places where the information in the original source code isn't needed at runtime. For example, information such as whitespace, comments, and the names of local variables aren't needed at runtime. We recommend that you use the generated source to understand how the program is executing and not as a replacement for the original source code.

Debug optimized or release assemblies

2019

When debugging code that was decompiled from an assembly that was compiled using compiler optimizations, you may come across the following issues:

  • Breakpoints may not always bind to the matching sourcing location.
  • Stepping may not always step to the correct location.
  • Local variables may not have accurate names.
  • Some variables may not be available for evaluation.

More details can be found in the GitHub issue: ICSharpCode.Decompiler integration into VS Debugger.

Download Vs Studio Code

Decompilation reliability

A relatively small percentage of decompilation attempts may result in failure. This is due to a sequence point null-reference error in ILSpy. We have mitigated the failure by catching these issues and gracefully failing the decompilation attempt.

More details can be found in the GitHub issue: ICSharpCode.Decompiler integration into VS Debugger.

Limitations with async code

The results from decompiling modules with async/await code patterns may be incomplete or fail entirely. The ILSpy implementation of async/await and yield state-machines is only partially implemented.

More details can be found in the GitHub issue: PDB Generator Status.

Just My Code

The Just My Code (JMC) settings allows Visual Studio to step over system, framework, library, and other non-user calls. During a debugging session, the Modules window shows which code modules the debugger is treating as My Code (user code).

Decompilation of optimized or release modules produces non-user code. If the debugger breaks in your decompiled non-user code, for example, the No Source window appears. To disable Just My Code, navigate to Tools > Options (or Debug > Options) > Debugging > General, and then deselect Enable Just My Code.

Extracted sources

Source code extracted from an assembly has the following limitations:

  • The name and location of the generated files isn't configurable.
  • The files are temporary and will be deleted by Visual Studio.
  • The files are placed in a single folder and any folder hierarchy that the original sources had isn't used.
  • The file name for each file contains a checksum hash of the file.

Generated code is C# only

Decompilation only generates source code files in C#. There is no option to generate files in any other language.