CodeCoverage using SonarQube scanner and NET5 - .net-core

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"

Related

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

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

Azure Devops BUild scripts Are Restore Build and Test Required

In Azure Devops for a .Net core application.
I have three steps
dotnet restore
dotnet build
dotnet test
But if I simply run dotnet test that forces a restore and build. Is there any reason to have the first two steps?
You can use them as follows:
dotnet restore
dotnet build --no-restore
dotnet test --no-build
In this way, you will speed up your build as it can use result of the previous command.
This is default behavior so you don't need to always run all commands to run dotent test for instance. It is convinient and still possible to opt-out from thah behavior.

DefineConstants works with Run command, but not Build command

I am struggling to understand the differences between dotnet run and dotnet build commands, in regards to constants defined in the .csproj file
example.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Test'">
<DefineConstants>$(DefineConstants);INCLUDE_TESTS</DefineConstants>
</PropertyGroup>
</Project>
program.cs
using System;
namespace define_example
{
class Program
{
static void Main(string[] args)
{
#if INCLUDE_TESTS
Console.WriteLine("It worked!");
#endif
Console.WriteLine("Hello World!");
}
}
}
If I run dotnet run -c Test, -> It worked! Hello World!
If I run dotnet build -c Test, then dotnet run {buildpath} -> Hello World!
Why does the defined constant go missing when I run the build command?
dotnet build and dotnet run are both commands that act in relation to a csproj file, not a dll or executable.
dotnet build will call the dotnet restore command and then build the project.
dotnet run will call the dotnet build command (this is important to note with respect to your issue) and then run the resulting dll or exe.
What you're currently telling dotnet to do by calling dotnet build -c Test and dotnet run is:
clean, restore, build (using the "Test" configuration)
clean, restore, build, run (with no configuration specified)
The second clean will cleanup the files produced by the first build and then you will build again as part of the run command, but without the Test configuration specified, causing the observed behaviour.
You could change dotnet run to dotnet run --no-build to tell it to assume a build has already been completed and to just assume that the files that would have been created are present and correct.
Alternatively, given that your project has an OutputType of Exe, you could just invoke the executable that is produced by the build command in the regular way as the second step. i.e. using the command example.exe.
If your OutputType was unspecified, or the default (Library) then this would produce a dll rather than an exe, and you could run it using the dotnet command: dotnet example.dll.
Useful reading on the various commands and also common properties:
dotnet command
dotnet build command
dotnet run command
Common MSBuild project properties

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.

How to run self-contained .NET Core tests?

I have an MSTest project that works fine when being executed with:
dotnet test --logger "trx;LogFileName=Result.trx" --settings tests.runsettings
I am also able to build a self-contained app out of it with:
dotnet publish -c Release -f netcoreapp2.1 --force --self-contained --runtime win-x64
But I have no idea how to run the tests from the produced output.
Calling
dotnet test .\ProjectName.dll --logger "trx;LogFileName=Result.trx" --settings tests.runsettings
fails with the message:
error MSB4025: The project file could not be loaded.
Any hints as how to run this self-contaiend MSTest-Project?
dotnet test now (2022) accepts .dll files to perform test execution.
You are using the wrong tool:
➜ ~ dotnet --help
test Runs unit tests using the test runner specified in the project.
vstest Runs Microsoft Test Execution Command Line Tool.
dotnet test is the tool used to run unit tests defined in a given project. If you are trying to run tests out of a published dll, dotnet vstest is the command you should us. You do that like this:
dotnet publish -o outputdir
dotnet vstest outputdir/your.dll

Resources