Hello,
I have setup a post build event in my shared test library, this library has not tests but is referenced by all my test projects, I did this because I want to automatically generate the reports every-time the projects are built.
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="cd $(SolutionDir)
dotnet test" />
</Target>
however I am getting the following error when I try to build the solution and the build hangs:
C:\...\packages\coverlet.msbuild\2.6.2\build\coverlet.msbuild.targets(41,5): error MSB4044: The "Coverlet.MSbuild.Tasks.CoverageResultTask" task was not given a value for the required parameter "InstrumenterState".
Directory.Build.Props
`
<PropertyGroup>
<IsTestProject>false</IsTestProject>
<IsTestProject Condition="$(MSBuildProjectName.EndsWith('Tests')) OR $(MSBuildProjectName.EndsWith('Test'))">true</IsTestProject>
<CollectCoverage Condition="!$(IsTestProject)">false</CollectCoverage>
<CollectCoverage Condition="$(IsTestProject) AND $(CollectCoverage) == ''">true</CollectCoverage>
</PropertyGroup>
<PropertyGroup Label="CollectCodeCoverageResults" Condition="$(IsTestProject) AND $(CollectCoverage)">
<CoverletOutputFormat>opencover</CoverletOutputFormat>
<_SlnDir>..\..\</_SlnDir>
<_BaseDir>..\..\coverage-results\</_BaseDir>
<CoverletBaseDir>$(_BaseDir)Coverage\</CoverletBaseDir>
<CoverletResultsDir>$(_BaseDir)Results\</CoverletResultsDir>
<CoverletBaseDir Condition="$([MSBuild]::IsOSUnixLike())">$(CoverletBaseDir.Replace('\', '/'))</CoverletBaseDir>
<SolutionDir Condition="$([MSBuild]::IsOSUnixLike())">$(_SlnDir.Replace('\', '/'))</SolutionDir>
<CoverletResultsDir Condition="$([MSBuild]::IsOSUnixLike())">$(CoverletResultsDir.Replace('\', '/'))</CoverletResultsDir>
<CoverletOutput>$(CoverletBaseDir)$(MSBuildProjectName).xml</CoverletOutput>
<Exclude>[xunit*]*,[*Tests]*,[*Test]*</Exclude>
</PropertyGroup>
`
Directory.Build.Targets
`
<ItemGroup>
<DotNetCliToolReference Include="dotnet-reportgenerator-cli" Version="4.1.10" />
<PackageReference Include="coverlet.msbuild" Version="2.6.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
<PackageReference Include="xunit.runner.reporters" Version="2.4.1" />
<PackageReference Include="xunit.runner.utility" Version="2.4.1" />
<PackageReference Include="TeamCity.VSTest.TestAdapter" Version="1.0.13" />
</ItemGroup>
<Target Name="CovertReportResults" AfterTargets="GenerateCoverageResult" Condition="$(CollectCoverage)">
<!-- Merges the reports into one for both cobertura and sonarqube formats -->
<Exec Command="dotnet reportgenerator -reports:$(CoverletBaseDir)*.xml -targetdir:$(CoverletResultsDir) -reporttypes:Cobertura;SonarQube -verbosity:Verbose" WorkingDirectory="$(ProjectDir)" />
<!-- Gives the summary in an Azure Pipelines look & feel -->
<Exec Command="dotnet reportgenerator -reports:$(CoverletResultsDir)Cobertura.xml -targetdir:$(CoverletResultsDir) -reporttypes:HtmlInline_AzurePipelines;Cobertura -verbosity:Verbose" WorkingDirectory="$(ProjectDir)" />
<Exec Command="dotnet reportgenerator -reports:$(CoverletResultsDir)Cobertura.xml -targetdir:$(SolutionDir) -reporttypes:TextSummary -verbosity:Verbose" WorkingDirectory="$(ProjectDir)" />
</Target>
`
I worked around this by combining Traversal SDK and conditional switch. You can use dirs.proj file to put all your needed project on a build tree and let the sdk handle the traversal. Then, with your conditional switch property, I guess you can workaround this problem. My conditional switch looks like below:
<!-- Coverlet settings -->
<PropertyGroup>
<!-- Enable coverlet by default -->
<EnableCoverlet Condition=" $(EnableCoverlet) == '' AND ($(MSBuildProjectName.ToLower().EndsWith('test')) OR $(MSBuildProjectName.ToLower().EndsWith('tests'))) ">true</EnableCoverlet>
</PropertyGroup>
<ItemGroup Condition=" $(EnableCoverlet) == 'true' ">
<PackageReference Include="coverlet.msbuild" Version="2.6.2" />
</ItemGroup>
@yhvicey thanks for your response, any chance you could send me an example including the traversal snippet?
Imaging we have a project folder like below:
(root)
|- src
| |- Project
| |- Project.csproj
| |- dirs.proj // Ignore this, use it if you like
|- test
| |- TestProject
| | |- TestProject.csproj
| |- dirs.proj // Let's call it second dirs.proj
|- dirs.proj // Let's call it first dirs.proj
Your first dirs.proj will look like below:
<Project Sdk="Microsoft.Build.Traversal">
<ItemGroup>
<ProjectReference Include="src\dirs.proj" />
<ProjectReference Include="test\dirs.proj" />
</ItemGroup>
</Project>
Your second dirs.proj will look like below:
<Project Sdk="Microsoft.Build.Traversal">
<ItemGroup>
<ProjectReference Include="TestProject/TestProject.csproj" />
<!-- Other projects -->
</ItemGroup>
</Project>
Note: Use global.json to specify versions for those SDKs globally, otherwise you will have to specify them like <Project Sdk="Microsoft.Build.Traversal/x.x.x"> in every traversal project. Below is a sample of global.json:
{
"msbuild-sdks": {
"Microsoft.Build.NoTargets": "1.0.73",
"Microsoft.Build.Traversal": "2.0.12"
}
}
And you can use this pattern recursively. I guess the root cause here is this coverlet version (2.6.2 in my case) cannot handle non-test project properly, so we have to avoid making coverlet available for those non-test projects, like your root project. By using traversal, you can make IsTestProject untouched in those dirs.proj and achieve your goal (Build one root dirs.proj to generate overall report).
Besides, I doubt this might be caused by your property. IsTestProject is an common property which helps SDKs decide if it's a test project, so I think this might be worked around by using other property instead, like EnableCoverlet.
thank's @yhvicey for help!
@farhad-taran-cko let know us if it works!
for the record, I've faced the same error, and I've realized that I've had added coverlet nuget to business projects. When I removed it, it's been fixed.
in my case after getting that error, I've installed MS.Net.Test.Sdk which cleared the error so in my test project package I do have
coverlet.msbuild version="2.6.3"
Microsoft.NET.Test.Sdk version="16.2.0"
Microsoft.CodeCoverage version="16.2.0" --- which a dependency of Sdk
<
@farhad-taran-cko any news?
Close for stale discussion.
Feel free to re-open if needed.
I have an open source project in GitHub, and we have problem to reporting testing result from issue https://github.com/Romanx/Cake.Coverlet/issues/30 , we install Coverlet.MSBuild to Test project, but we got this error in appveyor .
Did you can any help, please reopen this issue @MarcoRossignoli
@soroshsabz seem not related to coverlet I did some test with your project you need to update some ref to run nunit test with dotnet test
https://docs.microsoft.com/en-us/dotnet/core/testing/unit-testing-with-nunit#creating-the-test-project

run
dotnet test /p:CollectCoverage=true

Thanks you very much :) @MarcoRossignoli
You're welcome!
Ah @soroshsabz if you can use vstest integration that is better than msbuild https://github.com/tonerdo/coverlet#vstest-integration-preferred-due-to-know-issue if you're in trouble let me know.
Most helpful comment
in my case after getting that error, I've installed MS.Net.Test.Sdk which cleared the error so in my test project package I do have
coverlet.msbuildversion="2.6.3"Microsoft.NET.Test.Sdkversion="16.2.0"Microsoft.CodeCoverageversion="16.2.0" --- which a dependency of Sdk<