A library for creating nupkgs and nuspecs to use in tests.


Keywords
nuget, nupkgs
License
MIT
Install
Install-Package NuGet.Test.Helpers -Version 2.1.55+git.f63a14b

Documentation

NuGet.Test.Helpers

icon

Helper library for creating NuGet test packages. The intent of this library is to make it easy to generate NuGet packages and nuspecs in unit and functional tests for testing libraries that work with NuGet packages and NuGet feed generators.

Github NuGet
.NET test NuGet

Getting NuGet.Test.Helpers

Install via the .NET CLI:

dotnet add package NuGet.Test.Helpers

Or find release builds on NuGet.org.

Supported frameworks

  • net8.0
  • net9.0
  • net10.0

Usage

Creating a test package

using (var folder = new TestFolder())
{
    // Create a package with files and dependencies
    var nupkg = TestNupkg.Create("MyPackage", "1.0.0");
    nupkg.AddFile("lib/net8.0/MyPackage.dll");
    nupkg.AddDependency("Newtonsoft.Json", "[13.0.0, )");

    // Save the .nupkg to disk
    var path = nupkg.Save(folder);

    // Use the generated package in your test
    using (var reader = new PackageArchiveReader(path.FullName))
    {
        var identity = reader.GetIdentity();
        // assert, verify, etc.
    }
}

Customizing package metadata

var nuspec = new TestNuspec()
{
    Id = "MyPackage",
    Version = "2.0.0-beta.1",
    Authors = "TestAuthor",
    Description = "A test package",
    Tags = "test example"
};

var nupkg = nuspec.CreateNupkg();
nupkg.AddFile("lib/net8.0/MyPackage.dll", fileBytes);
nupkg.Save(outputDir);

Capturing NuGet log output

var logger = new TestLogger();

// Pass logger to NuGet APIs...

// Inspect logged messages
var allMessages = logger.GetMessages();
var warnings = logger.GetMessages(LogLevel.Warning);

API overview

Class Description
TestNupkg Creates .nupkg package files with configurable files and dependencies
TestNuspec Defines package metadata (id, version, authors, dependencies, etc.)
TestNupkgFile Represents a file entry inside a .nupkg archive
TestFolder Creates a temporary directory that auto-cleans on dispose
TestLogger In-memory ILogger implementation for capturing NuGet log messages

Building from source

Windows

build.ps1

Linux / macOS

build.sh

Contributing

We welcome contributions. If you are interested in contributing you can report an issue or open a pull request to propose a change.

License

MIT License