Unable to install coverlet.console 3.1 plugin in vs 2019 .net frame work 4.5.2 project - console

In git lab server,I am trying to generate code coverage using yaml file as below script
stage: test
script:
- dotnet test AMSC\AMSC.sln --logger trx /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
I am able to generate code coverage in opencover format in git server.
the job was failed after generating the code coverage as below screenshort,please let me know any one have solution on this.
code coverage report in CI/CD Pipe line
ERROR: Job failed: exit status 1

Related

CodeCoverage using SonarQube scanner and NET5

I have two NET5 xUnit tests projects. On the build server I am generating code coverage report using coverlet. These reports are wired-up to SonarQube scanner based on the guidance here
Here are the steps
begin step
dotnet "C:\SonarQubeScanners\sonar-scanner-msbuild-5.1.0.28487-net5.0\SonarScanner.MSBuild.dll" begin /k:"IM" /d:sonar.host.url="http://myhost.com" /d:sonar.login="xxxxxxx" /d:sonar.cs.opencover.reportsPaths="%WORKSPACE%\Tests\MyProject1.UnitTests\TestResults\**\coverage.cobertura.xml,%WORKSPACE%\Tests\MyProject2.UnitTests\TestResults\**\coverage.cobertura.xml"
build solution
dotnet build "%WORKSPACE%\My.sln" -nr:false
run unit tests for two projects and collect data
dotnet test "%WORKSPACE%\Tests\MyProject1.UnitTests\MyProject1.UnitTests.csproj" --collect:"XPlat Code Coverage"
dotnet test "%WORKSPACE%\Tests\MyProject2.UnitTests\MyProject2.UnitTests.csproj" --collect:"XPlat Code Coverage"
end step
dotnet "C:\SonarQubeScanners\sonar-scanner-msbuild-5.1.0.28487-net5.0\SonarScanner.MSBuild.dll" end /d:sonar.login="xxxxxxx"
Question
Each time the unit test runs it create new folder (with Guid Name) under TestResults
How do I tell SonarScanner to use latest report?
Or how do I clean-up TestResult folder before every run, is there a parameter to dotnet test command?
SonarScanner also throws error wile parsing the generated report
Could not import coverage report 'D:\Jenkins\im\workspace\Tests\MyProject1.UnitTests\TestResults\9c4d50ae-4113-4018-97bb-30d919a9b1e7\coverage.cobertura.xml' because 'Missing root element <CoverageSession> in D:\Jenkins\im\workspace\Tests\MyProject1.UnitTests\TestResults\9c4d50ae-4113-4018-97bb-30d919a9b1e7\coverage.cobertura.xml at line 2'
Here are the steps I did doing to get it working
1>First delete the testresult folder before the scanning begins
rmdir /s /q "%WORKSPACE%\Tests\MyProject1.UnitTests\TestResults"
2>Modified --collect parameters as below to generate coverage report in opencover format
dotnet "C:\sonar-scanner-msbuild-5.5.3.43281-net5.0\SonarScanner.MSBuild.dll" begin /k:"IM" /d:sonar.host.url="myhost.com" /d:sonar.login="xxxxxxx" /d:sonar.cs.opencover.reportsPaths="%WORKSPACE%\Tests\**\TestResults\*\coverage.opencover.xml"
dotnet build "%WORKSPACE%\IM.sln" -nr:false
dotnet test --no-build "%WORKSPACE%\Tests\MyProject1.UnitTests\MyProject.UnitTests.csproj" --collect:"XPlat Code Coverage" -- DataCollectionRunSettings.DataCollectors.DataCollector.Configuration.Format=opencover
dotnet "C:\sonar-scanner-msbuild-5.5.3.43281-net5.0\SonarScanner.MSBuild.dll" end /d:sonar.login="xxxxxxx"

SonarQube: Unable to import test coverage

I have a SonarQube installation and am running it against a .Net core application with multiple xUnit projects. The SonarQube picks up the bugs and smells fine but like so many people, I can't get the code coverage to complete.
At present, I am generating cobertura coverage XML files using the following command:
dotnet test --collect:"XPlat Code Coverage"
I then copy these to a centralized directory from each of the test projects and use the following command to run/import the files:
dotnet SonarScanner begin /k:"my-project" /d:sonar.cs.vscoveragexml.reportsPaths=".\TestResults\*.xml"
dotnet build
dotnet SonarScanner end
According to the logs, the files are found but are not ingested.
INFO: Parsing the Visual Studio coverage XML report
C:...\TestResults\5.coverage.cobertura.xml WARN: Could not import
coverage report '..\TestResults\5.coverage.cobertura.xml' because
'Missing root element in
C:...\TestResults\5.coverage.cobertura.xml at line 2'
I've confirmed that the files contain valid XML that looks to be correct, but I'm not even sure that SonarQube is supposed to accept cobertura reports.
Is my approach the way others have gone when trying to get xUnit coverage reports into SonarQube?
Is there a better way? I'm happy to use OpenCover or similar if that is easier.
In case anyone comes across this in the future, I gave up trying to use the built in coverage in VS and used DotCover. Bit of an easier setup and now seem to have it working to a degree.
Download/extract dotCover and add the folder to Path
dotCover.exe --output=AppCoverageReport.html --reportType=HTML dotnet -- test
dotnet SonarScanner begin /k:"my-project" /d:sonar.cs.dotcover.reportsPaths=AppCoverageReport.html
dotnet build
dotnet SonarScanner end
Still having an issue where the test coverage gathering seems to be pulling in too much and as a result is causing an error. Investigating but not related to this question.

Not getting code coverage in SonarCloud from an Azure Devops .NET core build

I have set up a pipeline for my .NET Core project in Azure Devops using the '.NET Core with SonarCloud' template. When I build the analysis gets run in SonarCloud but with 0% Code coverage (I have tests in my solution).
No matter what configuration tweaks I make to the build I cannot get the code coverage working.
What am I missing?
I came across this article and https://dejanstojanovic.net/aspnet/2019/may/publishing-code-analysis-to-sonarcloud-from-azure-build-pipeline/ implemented the powershell script described in it but still I get no code coverage in SonarCloud
I tried using coverlet as described here but still no joy
https://gunnarpeipman.com/aspnet/azure-devops-code-coverage/
My pipeline consists of the following tasks
.NET Core - Restore
Prepare Analysis Configuration
.NET Core - Build
.NET Core - Test
Run Code Analysis
Publish Quality Gate Result
My test task is configured:
Arguments: --configuration $(BuildConfiguration)
Publish test results and code coverage - checked
In the console of the Run Code Analysis task I get:
10:43:54.7 Fetching code coverage report information from TFS...
10:43:54.702 Attempting to locate a test results (.trx) file...
10:43:54.753 Looking for TRX files in: C:\\TFSBuilds\\TJPYHG04-GHJ01\\_work\\475\\TestResults
10:43:54.755 No test results files found
10:43:54.81 Did not find any binary coverage files in the expected location.
10:43:54.811 Falling back on locating coverage files in the agent temp directory.
10:43:54.812 Searching for coverage files in C:\\TFSBuilds\\TJPYHG04-GHJ01\\_work\\_temp
10:43:54.814 No coverage files found in the agent temp directory.
hope this answer still relevant to you.
Recently I have similar problem as you and I am also using Azure DevOps for my case.
This is how I solved it.
Step 1 - Change directory into your unit testing sub folder (same as the unit-testing .csproj file) and run the following dotnet command.
dotnet add package coverlet.msbuild
Step 2 - Add followings into SonarCloudPrepare Task Additional Properties or append directly into the yml file (if you're using yml instead of classic editor)
extraProperties: |
sonar.exclusions=**/obj/**,**/*.dll
sonar.cs.opencover.reportsPaths=$(Build.SourcesDirectory)/**/coverage.opencover.xml
sonar.cs.vstest.reportsPaths=$(Agent.TempDirectory)/*.trx
The directory is up to your choice to configure.
Or you can also create a file at your repo titled sonar-project.propertiesand store all the relevant SonarCloud properties inside.
Step 3 - Add followings into your dotnet test task
- task: DotNetCoreCLI#2
inputs:
command: 'test'
arguments: '--configuration $(BuildConfiguration) /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx'
testRunTitle: 'dotnet test'
You may notice there's a tickbox for "Publish test results and code coverage" but I still prefer using /p:CollectCoverage=true.
You may also test locally to run the dotnet test /p:CollectCoverage=true /p:CoverletOutputFormat=opencover --logger trx command and coverage.opencover.xml will be generated at your unit testing folder.
Refer to reference 2 & 3 for more parameters and their description.
Side Note: If you're doing any Sonar test inclusion properties, note that Sonar.Tests and Sonar.Test.Inclusions their Test(s) are different. lol
References:
Using SonarCloud in Azure pipelines
Analysis Parameters (official docs)
Test Coverage & Execution (official docs)
Hope this helps :)
Not getting code coverage in SonarCloud from an Azure Devops .NET core build
This issue may caused by vstest output path changed recently:
The output path of the vstest coverage file change from
D:\a\1\s\TestResults\... to D:\a\_temp\...
Which broke subsequent scripts in the pipeline (like, codecoverage.exe to convert to xml and later import to sonarqube).
Microsoft suggest that use the rest APIs to check for the test artefacts and re-download them to the build agent.
More investigation on this issue, you can check the thread Azure DevOps (VSTS) extension no longer import coverage and unit tests automatically for the issue tracking.
Fortunately, SonarSourcer team have just release new versions of the SonarQube (v4.6.3) and SonarCloud (v1.6.3) extensions to address the coverage issue and the regression.
Hope this helps.

VSTS dotnet run step does not find build from previous step

I have the following successful dotnet build release pipeline step configured:
Task version: 2.*
Display name: dotnet build
Command: build
Path to project(s): **/AppConsole.csproj
Arguments:
It is immediately following by a failing dotnet run step:
Task version: 2.*
Display name: dotnet run
Command: run
Path to project(s): **/AppConsole.csproj
Arguments:
Which exits with the message:
[command]C:\agent_dply\_work\_tool\dotnet\dotnet.exe run C:\agent_dply\_work\r40\a\Testing\Tester\AppConsole.csproj
Couldn't find a project to run. Ensure a project exists in C:\agent_dply\_work\r40\a.
Or pass the path to the project using --project
##[error]Error: The process 'C:\agent_dply\_work\_tool\dotnet\dotnet.exe' failed with exit code 1
##[error]Dotnet command failed with non-zero exit code on the following projects : C:\agent_dply\_work\r40\a\Testing\Tester\AppConsole.csproj
How do I tell the dotnet command where the project is when running under VSTS? I've tried, as it recommends, using the --project flag, but it gives similar errors.

setup teamcity with .core, run command fails

I am trying to migrate from Jenkins to Teamcity 2018
So far I have 4 build steps:
Dotnet restore
dotnet build
dotnet run
dotnet test
When teamcity runs 3rd step, it fails.
it tries to run :
dotnet.exe run --project .\Prime.csproj #D:\TeamCity\buildAgent\temp\agentTmp\5d23e7ecee784cabb12baefd7175c67d.rsp
and it gives error
Unhandled Exception: System.FormatException: Unrecognized argument format: '#D:\TeamCity\buildAgent\temp\agentTmp\5d23e7ecee784cabb12baefd7175c67d.rsp'.
I think, it because dotnet cli doesn't accept the # part..
Have anyone seen such error before?
Solution has 2 projects: Prime (the main code) and a tests project with all the tests. The tests project runs just with with 'dotnet test' command
Using dotnet core 2.0 .
So the only way forward which I found is to run .core app in Docker.
So, you need to publish the app to a folder and then put that code into a docker image and then set up your test project to run against docker instance of the app.

Resources