<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:
Target Name="Test" AfterTargets="Build": This line indicates that after theBuildtarget is executed, theTesttarget will also be executed.Exec Command="dotnet test @(TestProject)": This line runsdotnet teston 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-buildto thedotnet testcommand. - Ensure tests are run separately after building to avoid circular dependencies.
This should stop the infinite build and test cycle.