Endjin - Home

A Step by Step Guide to Testing NuGet Packages Locally

by Pascal Arnould

header-step-by-step-guide-testing-nuget-packages-locally-p1-1024px

There’s a lot of documentation available around NuGet and how to create/publish packages, etc. But when I looked for a simple step-by-step guide on how to test a package locally, I couldn’t find any. So here it is:

1. Open PowerShell as Admin

2. Install Chocolatey running the command below:

PS:\> iex ((new-object net.webclient).DownloadString(‘https://chocolatey.org/install.ps1’))

3. Install NuGet.CommandLine:

PS:\> cinst NuGet.CommandLine

4. Check that the latest version of NuGet was installed:

PS:\> nuget update -self

5. Change to the directory that contains the application assembly that you want to package and generate a .nuspec file:

PS:\> nuget spec MyApp.dll

This will create a MyApp.dll.nuspec file that describes your package as shown below:

<?xml version="1.0"?>
<package >
 <metadata>
 <id>MyApp.dll</id>
 <version>1.0.0</version>
 <authors>Pascal</authors>
 <owners>Pascal</owners>
 <licenseUrl>http://LICENSE_URL_HERE_OR_DELETE_THIS_LINE</licenseUrl>
 <projectUrl>http://PROJECT_URL_HERE_OR_DELETE_THIS_LINE</projectUrl>
 <iconUrl>http://ICON_URL_HERE_OR_DELETE_THIS_LINE</iconUrl>
 <requireLicenseAcceptance>false</requireLicenseAcceptance>
 <description>Package description</description>
 <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
 <copyright>Copyright 2014</copyright>
 <tags>Tag1 Tag2</tags>
 <dependencies>
 <dependency id="SampleDependency" version="1.0" />
 </dependencies>
 </metadata>
</package>

6. Edit the .nuspec file using either a text editor or NuGet Package Explorer. For more information about the .nuspec file format, please refer to the Nuspec Reference.

7. Once you are done editing, you can create the package itself:

PS:\> nuget pack MyApp.dll.nuspec

If no errors are reported, NuGet will create the package (e.g. MyApp.1.0.0.nupkg) in the current directory and you now have a NuGet package that can be tested locally.

8. Create a local NuGet Repository:

PS:\> new-item C:\Nuget.Local -itemtype directory

9. Copy the package to your local NuGet

PS:\> copy-item .\MyApp.1.0.0.nupkg C:\NuGet.Local

10. Next you will need to add NuGet.Local to the package sources in Visual Studio:

In Visual Studio, open up the Package Manager Settings:

package_manager_settingsjpg

Add NuGet.Local to the list of available sources:

package_sources.jpg

11. Create a new C# class library project, right-click on the project and select Manage NuGet Packages.

12. Select NuGet.Local in the list of Online sources and install your package.

install_nuget_package.jpg

As you can see from the screenshot below MyApp package has a dependency on Newtonsoft.Json:

install_nuget_package_window.jpg

If your package is setup correctly, a successful install should be reported.

install_nuget_package_success.jpg

For your next step, you can refer back to the documentation for publishing a package.

Pascal Arnould on Twitter

About the author