简体   繁体   中英

MS test does not improve code coverage in sonarqube

I am using .Net MVC with Web API project in Visual Studio 2015.

The project is configured with the SonarQube Version 8.7.

Currently the Code coverage percent is 0%.

Recently I have added a unit test project in my solution and added around 25+ test methods for my Web API Controllers.

All the test methods are passed. I am using MS Test for Unit testing.

On executing the SonarQube script I am not able to see any improvement in my Code coverage inspite of adding unit test cases.

Please guide me for what have been gone wrong resulting in 0 percent of Code coverage in SonarQube

Thank you in advance.

EDIT: After OP add a comment for more clarity, the following block is necessary for understanding issue:

From documentation that is linked below.

We support:

Import of coverage reports from VSTest, dotCover, OpenCover, Coverlet and NCover 3.
Import of unit test results from VSTest, NUnit and xUnit.

To include tests results and test coverage you need to create an XML report that will be read by SonarQube. Official documentation can be found here .

Since MS forum says that:

Visual Studio includes the VSTest and MSTest command-line tools for testing purposes.

We can use both VSTEST and MSTEST to run automated unit and coded UI tests from a command line.

My advice is to use VStest since MStest is not supported. Also, there are some cases that should be looked at, so please check the documentaion.

Run Unit Tests To Collect Code Coverage "%VSINSTALLDIR%\\Common7\\IDE\\CommonExtensions\\Microsoft\\TestWindow\\vstest.console.exe" /EnableCodeCoverage "UnitTestProject1\\bin\\Debug\\UnitTestProject1.dll"

Convert the Code Coverage Report from Binary into XML

"%VSINSTALLDIR%\Team Tools\Dynamic Code Coverage Tools\CodeCoverage.exe" analyze /output: "%CD%\VisualStudio.coveragexml" "%CD%\VisualStudio.coverage"

If anyone uses NUnit, here are steps to include it: Run Unit Tests and Save Results in file "NUnitResults.xml"

packages\NUnit.ConsoleRunner.3.7.0\tools\nunit3-console.exe --result=NUnitResults.xml "NUnitTestProject1\bin\Debug\NUnitTestProject1.dll"

Import unit test results

To import a test execution report, during the Begin step you need to pass a parameter that points to the file that will be generated:

  • sonar.cs.nunit.reportsPaths for NUnit

Full command will be something like:

dotnet sonarscanner begin /k:PROJECT_NAME /d:sonar.login=LOGIN_TOKEN /d:sonar.host.url=URL /d:sonar.cs.nunit.reportsPath=PATH_TO_NUnitResults.xml 

My first guess is that you're running VS community edition or pro. To have Visual Studio generate a coverage file, you need Enterprise , or a 3d party coverage plugin.

Requirements

The code coverage feature is available only in Visual Studio Enterprise edition.

In which case you'll need to configure dotCover or any of the other mentioned coverage tools or upgrade to VS Enterprise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM