Run Tests On Build

<ItemGroup>
    <TestProject Include="..\IntranetCore.Web.Tests\IntranetCore.Web.Tests.csproj" />
</ItemGroup>

<Target Name="Test" AfterTargets="Build">
    <Exec Command="dotnet test --no-build @(TestProject)" />
</Target>

The problem you're encountering is related to the build target cycle. Here's a breakdown of what's happening:

  1. Target Name="Test" AfterTargets="Build": This line indicates that after the Build target is executed, the Test target will also be executed.
  2. Exec Command="dotnet test @(TestProject)": This line runs dotnet test on the test project (IntranetCore.Web.Tests).

The issue arises because the dotnet test command implicitly performs a build on the test project (IntranetCore.Web.Tests.csproj) before running the tests. Since IntranetCore.Web.Tests.csproj references IntranetCore.Web.csproj, it triggers a build of IntranetCore.Web. This creates a circular dependency, where building IntranetCore.Web triggers the test, which builds IntranetCore.Web.Tests, which in turn rebuilds IntranetCore.Web, and so on—leading to an infinite loop.

Solution

You can break the loop by separating the dotnet build and dotnet test steps. Here’s a better approach:

Remove the test target from the build process: Do not automatically trigger tests after the build. Instead, keep the build and test processes distinct. Here's the modified csproj:

<ItemGroup>
    <TestProject Include="..\IntranetCore.Web.Tests\IntranetCore.Web.Tests.csproj" />
</ItemGroup>

<Target Name="Test" AfterTargets="Build">
    <Exec Command="dotnet test @(TestProject) --no-build" />
</Target>

The important change here is the --no-build option in the dotnet test command. This prevents the dotnet test command from rebuilding the project and triggering the infinite loop.

Run build and test separately: Instead of relying on the build to trigger tests, run the build and test separately. You can run dotnet build to build the project and dotnet test to execute the tests.

By separating the two processes and ensuring that dotnet test doesn’t build the project again, you avoid the infinite loop.

Summary of Changes:

  • Add --no-build to the dotnet test command.
  • Ensure tests are run separately after building to avoid circular dependencies.
    This should stop the infinite build and test cycle.
Last updated: 8/22/2024 10:45:35 AM

Latest Updates

© 0 - 2025 - Mike Brind.
All rights reserved.
Contact me at Mike dot Brind at Outlook.com