Coverage always at 0% on sonarqube even i added test unit - c#

I created a test unit for a class and it passed well on my local, on sonarqube it is shown as 0% for Coverage, I found post advice to add coverlet.msbuild I added but still no news:
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.0" />
<PackageReference Include="coverlet.msbuild" Version="3.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
Any idea?

Try adding the following to your .csproj
<PropertyGroup>
<DebugType>Full</DebugType>
</PropertyGroup>
<PackageReference Include="Microsoft.CodeCoverage" Version="16.9.4" />

Can you try coverlet.collector instead of coverlet.msbuild? I have personally used coverlet collector several times successfully.
<PackageReference Include="coverlet.collector" Version="1.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
According to this GitHub issue.
Collectors are pretty new(last arrived) and are the best way to do coverage because are strictly integrated with vstest platform(dotnet test) and due to that is the default choice for every .NET core xunit project by Microsoft.
So if you create xunit project Microsoft xunit template inject by design our coverage engine as first class coverage tool running with dotnet test --collect:"XPlat Code Coverage"
It may be possible that you are running into this known issue.

Related

Cannot read the external namespaces (Microsoft.AspNetCore.MVC...) in Roslyn (in another project it is working)

I have set up one Roslyn analyzer project that is reading properly the (external and internal) namespaces from Symbol (open solution in workspace -> read documents -> read classes -> read class symbol); however, when I created another xUnit project for adding some unit tests, I was not able to read anymore the namespaces that are external to the project (e.g. Microsoft.AspNetCore.Mvc.****).
Does someone know why is this happening? I also tried to run this same project (that was working) in a different machine and it was not reading the external namespaces neither.
I have included the exact same packages in both projects but it is simply not working and showing ErrorTypeSymbol for symbols that have external namespaces.
Note: I open the Solution in the Workspace in the same way on both projects...
Edit: The error message is "This program location is thought to be unreachable.".
Edit 2: It seems like Microsoft.NET.Test.Sdk package is causing the problem and after checking in the Workspace.Diagnostics it shows the following error:
[Failure] Msbuild failed when processing the file '/home/username/RiderProjects/SampleApi/SampleApi/SampleApi.csproj' with message: /usr/share/dotnet/sdk/5.0.404/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.FrameworkReferenceResolution.targets: (89, 5): The "ProcessFrameworkReferences" task failed unexpectedly.
System.IO.FileNotFoundException: Could not load file or assembly 'NuGet.Frameworks, Version=5.11.1.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'NuGet.Frameworks, Version=5.11.1.5, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
at Microsoft.NET.Build.Tasks.ProcessFrameworkReferences.ExecuteCore()
at Microsoft.NET.Build.Tasks.TaskBase.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask)
This is the .csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Build.Locator" Version="1.4.1" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
<PackageReference Include="Moq" Version="4.18.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>
Downgrade to 16.2.0 works fine. But it is not a good "solution".
I created an issue: https://github.com/microsoft/vstest/issues/4212

Unable to create an object of type 'ApplicationDbContext' in asp .net core 5

I made a project with 5 Class libraries in asp .net core 5.
'ApplicationDbContext' class is in the 'Data' Class library and ny models are in the 'Model' class library. when I run 'add-migration' in Package Manager Console, it works properly but when I run 'dotnet ef migrations add' in console, it shows me this error: 'Unable to create an object of type 'ApplicationDbContext''.
Here are the Nuget packages needed:
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.13" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.13">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
1.Open the package management console: Tools->Nuget Package Manager->Package Manager Console
2.cd {path where your csproj file resides}
3.Now type dotnet ef migrations add InitialMigration

C# EF Migrations don't create table

It seems like I kinda broke my project. When I use e.g. add-migration initial to add a new migration, it generates a migration file in the project folder which looks correct, but I dont see it in my solution (in VS 22). When I try to update the database, it just generates the __EFMigrationsHistory -table, but everything else is missing and the command ends with No migrations were applied. The database is already up to date.
It feels like i broke the whole migration thing. Does someone has an idea on this?
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.0" />
</ItemGroup>

Adding Reference to Class LIbrary in Azure Function project causes assembly not found

I dont really have much to go on here, but I have an Azure Function project (.NET Core 3.1 with Azure Functions v3)
This function has a Http Trigger which works perfectly on its own
However, as soon as I reference one of my existing projects, I get the error
System.Private.CoreLib: Exception while executing function: MyFunction.
FunctionApp2: Could not load file or assembly 'Microsoft.Extensions.Primitives,
Version=5.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
Is this related to assembly binding? How can I force this version to be available to my function application?
I dont have a web.config so the standard assembly binding wont work
Below is the file
I have tried adding the assembly directly to the project and that makes no difference
The project I am referencing doesnt have this package either so I am not sure which package this comes from
My function app has the following packages
<ItemGroup>
<PackageReference Include="Fody" Version="6.5.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="LoadAssembliesOnStartup.Fody" Version="4.5.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="4.3.0" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.13" />
</ItemGroup>
I tried to use Fody to ensure that the referenced project assembly is loaded on startup but that made no difference
As soon as I remove my other project it works fine
Paul
I found the problem eventually, there is an issue with Redis and Azure Functions
I had to downgrade Redis and it worked

Is there a way to create a .Net Core class library that reference ASP.NEt core without beeing a netcoreapp

We are making a app template that make use of a shared API library with API controllers. (Avaiable from Nuget)
Since API controllers take use of Microsoft.AspNetCore we are forced to make the library target netcoreapp30.
That again forces us to create startup.cs and program.cs in that library to make it compile.
Is there a way to make a class library refernce needed aspnetcore stuff without beeing required to create the startup.cs and program.cs? (compiler complains about missing start point without them)
Startup.cs and program.cs we have in the app using the nuget.
Our project file for the library
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
</ItemGroup>
</Project>

Categories