An MSBuild task / helper to simplify testing NuGet packages by automatically ensuring the latest package is built and
placed in the output directory for test projects. To use, first install the package, then add the metadata
AddPackageAsOutput=true
to any <ProjectReference>
items like this:
<ItemGroup>
<ProjectReference Include="..\MyPackage\MyPackage.csproj" AddPackageAsOutput="true" />
</ItemGroup>
Adding that metadata will do a few things:
- Ensure the package is generated on every build
To avoid working with stale packages, the build will validate that any projects with this metadata have the GeneratePackageOnBuild
property set (by default, a project only creates a package when you run the Pack target).
-
Add the outputs of the pack operation (e.g. .nupkg and .nuspec files) as metadata on the
<ProjectReference>
-
Add all .nupkg files as
<Content>
items for your build
This ensures that the packages can be copied to your output directory for tests.
Add this snippet to your unit tests to get the path to an output NuGet package:
FileInfo package = new(Assembly.GetExecutingAssembly().Location)
.Directory!
.GetFiles("NameOfNuGetPackageToTest*.nupkg")
.OrderByDescending(f => f.LastWriteTimeUtc)
.First()