0

I'm trying to find a way to run a specific target only once per solution after tests are done.

I have a solution with multiple test projects. Each has integration with coverlet.msbuild, hence they each produce a coverage report (opencover) file in a designated folder.

What I want to achieve is run another target, namely ReportGenerator, but only after all the tests are done and all the coverage report files are created. I've successfully created those report after the VSTest target. But that means I generate the report multiple times in a single run, because each testing project is running a VSTest target separately.

this is my latest attempt

<Project>
    <PropertyGroup>
        <CollectCoverage>true</CollectCoverage>
        <CoverletOutput>../../coverage/opencover/$(MSBuildProjectName).xml</CoverletOutput>
        <CoverletOutputFormat>opencover</CoverletOutputFormat>        
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="coverlet.collector" Version="3.1.0">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
        <PackageReference Include="coverlet.msbuild" Version="3.1.0">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
        </PackageReference>
        <PackageReference Include="ReportGenerator" Version="5.0.4" />
    </ItemGroup>
    
    <Target Name="GenerateHtmlCoverageReport" AfterTargets="GenerateCoverageResultAfterTest">
        <ReportGenerator
            ReportFiles="@(CoverletReport)"
            TargetDirectory="../../coverage/html-coverage-report"
            ProjectDirectory="$(MSBuildProjectDirectory)"
            ClassFilters="-*OrleansGeneratedCode*"
            HistoryDirectory="../../coverage/history"
            ReportTypes="HTML"
            VerbosityLevel="Verbose" />
    </Target>
    
</Project>

Note: this is a separate props file included in the Directory.Build.props that is used by all test projects, just to avoid duplication.

Any ideas?

Eugene Krapivin
  • 1,671
  • 2
  • 17
  • 32
  • What do you do to run the tests in the first place? Do you call `dotnet test` on the solution file or on the projects individually? – Martin Ullrich Mar 14 '22 at 20:05
  • @MartinUllrich I'm running `dotnet test` on the solution locally, in the CI we are running `dotnet test` with filter to run the unit & functional together (with coverage) and a separate run for the integration tests (without coverage). this thing gets even more interesting when I'm running multi TFM tests to ensure my multi TFM libraries. Frankly it looks that I'm better off a `ps1`/`sh` script or a nuke/cake build that trying to make good with `msbuild` in this case. – Eugene Krapivin Mar 23 '22 at 10:32

0 Answers0