Looking for:
Microsoft code analysis tool .net visual studio 2015 free download
Code Quality is a loose approximation of writing useful, correct and maintainable code. Writing good quality code helps in reducing bugs at a later stage in product development. Visual Studio provides many built-in tools for writing quality code. This article will discuss various tools available to developers for testing using Visual Studio.
Writing unit tests, using IntelliTest, finding Code Coverage will also result in writing qualitative code. Once the code is written, unit testing helps developers find logical errors in the code.
In TDD, the test is written first and then the code is written for the test method. We will discuss how to create unit test for code which is already available.
Create a solution and add a class library with some functionality to it. We will write unit tests for the methods in the class library. Add Unit Test Project to the current solution. Select a method for which we need to create a unit test, right click and select Create Unit Tests. Select the option of new Test Project. The Unit Test Project is added, reference set to the class library and you can select Empty body, throw exception or add the statement for Assert.
Failure as shown in this figure. For this example, select the default Assert failure statement and the stub for Test Method looks as follows. The class gets an attribute as TestClass, and the method gets attribute as TestMethod. Without any of these attributes, the test method will be ignored. Let us add code to test the method. You can run the test using Test Explorer. If it is not visible, go to Test — Windows — Test Explorer to view it.
Select the test method, right click and choose run. There are four additional attributes for initializing and cleaning up for class or test. ClassInitialize and ClassCleanup methods will be executed when you run the first test in the class, and when you finish running the last test in the class. Similarly TestInitialize and TestCleanup methods are called before test execution and after test running. In the previous example, the test method took the same set of parameters and gave the same result.
In order to achieve this, we need to convert this test method to data driven test. The data can be provided via xml file, csv file or even via database.
Change the name of the XML file to anything you wish. Go to properties of the file, and change the Copy to Output Directory to Copy if newer.
Now we need to add a DataSource parameter to the method, the code looks as follows. Add a reference to System. Data assembly, and add the code for TestContext. Run the test and observe that it gets executed three times, same as the number of records in the data file.
With this tool, you can find out how many tests are passing or failing. You can also provide the code to fix issues. Writing an exhaustive test suite for a very complex piece of code, requires a lot of efforts. There is a tendency to omit some test data which may lead to bugs getting captured at a far later stage.
IntelliTest takes care of this problem. It will help in early detection of bugs, and lead to better qualitative code. It will create a Test Suite for all the methods in the class, and generate data. Every code is analysed depending upon any if statements, loops. It shows what kind of exceptions will be thrown. If we select the test case, it shows us the details and how code can be added.
In this example, Divide by zero exception can be handled by adding an attribute. We can add the necessary code. We have the option of actually creating a Test Project and adding all the test methods to it. IntelliTest adds Pex attributes to the code, as can be seen from following image.
IntelliTest when created, tries to find the path for high code coverage. Let us find out how Code Coverage will help in increasing Code Quality. Code Coverage determines which code is getting executed when the unit test is run. If there are complex and multiple lines of code, Code Coverage will help in finding out if a portion of the code is not being tested at all. We can take necessary action based on these findings. Code coverage can be computed when you are executing tests using Test Explorer.
If we navigate to the actual assembly, we can see the blue and red coloured code which indicates if the code was executed or not. Code Coverage is measured in blocks. A block is a code which has only one entry and exit point. If the control passes through a block, it is considered as covered. In order to view the previous results, Import the results.
To send the results to someone, export it. At times, we can use a third party testing framework for unit testing. You can download and install various third party testing framework with Visual Studio. Find NUnit and Install.
A reference to nunit. Until now, we have seen all the testing related tools for developers. These tools help in identifying bugs at an early stage thereby improving overall code quality. The code being written needs to follow certain rules. Code Analysis also called as Static Code Analysis helps in finding out if there are areas in the code not following a set of accepted rules. This tool is a part of Visual Studio, and can be applied to any project. We can specify that code analysis is to be enabled at the time of build, or it can be applied as and when required.
Using these rulesets, either one or multiple rulesets can be applied to a project. The default behaviour gives warnings if the rules are not adhered to. In order to see the rulesets, right click on the project to apply code analysis, and select properties. Select the tab for Code Analysis and click on the drop down to get a list of rulesets.
Various rules are displayed for each category. We can change any of these rules and create a copy of the ruleset. We cannot modify the existing ruleset, but the copied one can be edited as required. Open the ruleset and expand one of the categories. For most of the rules, we get a warning if the rule is not followed. By changing it to error, we can ensure that the developer cannot ignore the rule, as its a human tendency to ignore any warnings. After changing one or multiple rules in a ruleset, we can provide a name for it, and save this copy of ruleset for future reference.
Later this set can be applied to a project, to get the errors and or warnings as set earlier. Note: Code Analysis was previously known as FxCop. This tool helps in finding out if there are any programming related issues in the code. Visual Studio can measure maintainability index for the code.
It also finds the complexity of the code. As the name suggests, Code Metrics provides information if the code is maintainable or not. Originally this algorithm was developed by Carnegie-Mellon University. Microsoft adopted this and incorporated it as a part of Visual Studio from Visual Studio version onwards.
Cyclomatic Complexity is based on loops and various decisions in code. Class Coupling finds the number of dependencies on other classes.
More the class coupling, lower is the index. Depth of Inheritance is for inheritance of classes from the Object class. Lines of code is the actual number of executable lines. The index is between the range of 0 to The higher the maintainability index, the better are chances of maintaining it. For using this tool, you only need to select the project or complete solution, right click and select Calculate Code Metrics. The results will be shown with the classification of parameters mentioned.
This tool was introduced by Microsoft in Visual Studio
Code analysis using Roslyn analyzers – Visual Studio (Windows) | Microsoft Docs.Visual Studio Release Notes | Microsoft Learn
NET compiler platform (Roslyn) analyzers inspect your C# or Visual Basic code for code quality and code style issues. The first-party. Configure a rule set for replace.me Framework project. Open the Code Analysis tab on the project’s property pages. You can do this in either of.