.NET测试用例与YAML流水线不遵循构建顺序问题求助
Hey Paul, let's work through this problem together. You're dealing with two related issues here—uncontrolled build order in your YAML pipeline and a missing NuGet package error. Here's how to fix both:
The dotnet test command automatically builds projects by default, and it uses parallel execution to speed things up, which can disrupt the intended build order for dependent projects. To fix this, split your pipeline into explicit restore, build, and test steps, and disable parallel building:
- Add a dedicated restore step: Ensures all packages are downloaded before any building starts.
- Build with single-threaded execution: Use the
--maxCpuCount 1flag to force sequential building of projects. - Run tests without auto-building: Add
--no-buildto the test command so it only runs tests on already built projects.
Here's the updated YAML with these changes (note I also fixed the typo falsefo to false in your continueOnError field):
job: Job_1 displayName: Agent job 1 pool: vmImage: windows-2019 steps: - task: UseDotNet@2 displayName: '.NET Core 3.1.404' inputs: version: '3.1.404' packageType: sdk - task: DotNetCoreCLI@2 displayName: dotnet restore inputs: command: restore projects: '**/*.csproj' feedsToUse: config includeNuGetOrg: true # Ensures NuGet.org is used as a package source - task: DotNetCoreCLI@2 displayName: dotnet build inputs: command: build projects: '**/*.csproj' arguments: --configuration $(buildConfiguration) --maxCpuCount 1 - task: DotNetCoreCLI@2 enabled: true displayName: dotnet test continueOnError: false inputs: command: test projects: '**/*test/*.csproj' arguments: --configuration $(buildConfiguration) --no-build publishTestResults: true
If you need an exact project build order (e.g., ProjectA must build before ProjectB), replace the projects value in the build step with the ordered list of project paths:
projects: 'src/ProjectA.csproj src/ProjectB.csproj src/ProjectTest.csproj'
This error usually happens because the package isn't being restored properly before the non-first project builds. Try these fixes:
- Ensure NuGet.org is included as a source: The
includeNuGetOrg: truein the restore step above ensures the public NuGet feed is used, which hosts the NETStandard.Library package. - Clear cached packages: Add a step to clean the NuGet cache before restore to avoid stale or missing cache entries:
- task: NuGetCommand@2 displayName: Clear NuGet cache inputs: command: custom arguments: locals all --clear - Explicitly reference the package in affected projects: Open the problematic project's
.csprojfile and add this line inside the<ItemGroup>section (adjust the version if needed):<PackageReference Include="NETStandard.Library" Version="2.0.3" /> - Verify the .NET SDK compatibility: .NET Core 3.1.404 should fully support NETStandard.Library 2.0.3, but double-check that your project's target framework is compatible (e.g.,
<TargetFramework>netcoreapp3.1</TargetFramework>or<TargetFramework>netstandard2.0</TargetFramework>).
内容的提问来源于stack exchange,提问作者Paul

