Loading...

C# NuGet

Read the line that brought xUnit into the test project. NuGet is the library list for a .NET project. A PackageReference names a library and a version. The test project already has one for xunit. That is why [Fact] and Assert compiled on the last page. This page does not add a library and does not change either file you already run.
The xUnit page left two checks in place. dotnet run inside PineLab still prints eight lines: pine, 12, pine x12, 12,8, pine x20, Tag { Name = pine, Count = 20 }, True, and False. dotnet test from the folder above both projects still reports Passed: 2. Optional Advanced continues, so Hold<T>, Tag, label, Where, and the two facts stay. If Passed: 2 is gone before you edit the project file, you are not in that folder, or the test project is not the one dotnet new xunit created.

The line in the test project

Open PineLab.Tests.csproj. It is XML. Inside an ItemGroup you will find PackageReference elements. A current SDK template writes several. The one this page uses looks like this, with the version the template chose in place of the dots:
<PackageReference Include="xunit" Version="..." />
Include is the package id. xunit is the library that contains [Fact] and Assert. Version is the template's number. Do not memorize it and do not type a different one. Two machines can show two numbers. Both are fine while the id stays xunit.
Two more ids matter, and a current template writes them too. Microsoft.NET.Test.Sdk is the test host. xunit.runner.visualstudio is the runner that lets dotnet test find your facts. The runner element often spans more than one line and may contain PrivateAssets. You are looking for the Include value, not a one-line shape. A coverage package such as coverlet.collector may also be there. This page does not call it. Leave every line as the template wrote it.
The test file does not mention NuGet. It only uses the types the package provides. Here is that file again, unchanged. using Xunit; is the name the xunit package supplies.

The two facts, unchanged, compiled because of the package

csharp
using Xunit;

public class Checks
{
    [Fact]
    public void TwoTagsWithTheSameValuesAreEqual()
    {
        Tag tag = new Tag("pine", 20);
        Tag same = new Tag("pine", 20);
        Assert.Equal(tag, same);
    }

    [Fact]
    public void TwoHoldObjectsAreNotTheSameObject()
    {
        Hold<int> number = new Hold<int>(12);
        Hold<int> again = new Hold<int>(12);
        Assert.NotEqual(number, again);
    }
}

Two projects, two lists

From the PineLab.Tests folder, run dotnet list package. The table includes the id xunit. It also includes the test SDK and the runner. The version column is whatever the template wrote. You check the id.
From the PineLab folder, run dotnet list package again. xunit is not in that list. The program project has no PackageReference at all. System and System.Linq come with the SDK. Hold<T>, Tag, Func, and Where did not come from a package you added. That is why dotnet run never asked NuGet for a library.
The reference you added with dotnet add PineLab.Tests reference PineLab is a project reference, not a package. It lets the tests see Hold and Tag. It does not appear in the package list. If the tests cannot find Tag, the project reference is missing. If the tests cannot find Fact, the package line is missing. Those are different failures.

What each package is for

xunit is the library. Delete only that PackageReference, save the file, and run dotnet test. The test file no longer compiles: Xunit, Fact, and Assert are unknown names. You do not get Passed: 2. The program project never used that package, so dotnet run inside PineLab still prints the eight lines. Put the xunit line back exactly as it was, version included. dotnet test reports Passed: 2 again.
Microsoft.NET.Test.Sdk is the host dotnet test launches. Without that line the tests are not started, and you do not get Passed: 2. Put it back.
xunit.runner.visualstudio is the adapter that discovers the two methods. Without it the test code can still compile, because [Fact] lives in the xunit package, not in the runner. dotnet test then finds no tests, so it does not report Passed: 2. Put the runner line back.

📊 Three packages the tests need

Package What you lose without it
xunit the test file does not compile, so there is no Passed: 2
TestSdk the test host does not start, so there is no Passed: 2
Runner the code compiles and dotnet test finds no tests

Leave the versions alone

dotnet add PineLab.Tests package xunit asks NuGet for a version and writes a PackageReference. The template already did that. Running it again can move you to a newer version than the one your tests already passed with. This page does not run it. The checked project keeps the template's version.
A line you do not use, such as a coverage collector, is not what makes Passed: 2. Removing it leaves the count alone. Removing xunit, the test SDK, or the runner does not. If you are not sure which line you touched, put the whole ItemGroup back from the template by creating the test project again and copying the references. Do not copy them into PineLab.csproj. The program project stays without packages.
NuGet restores before a build. The first dotnet test on the last page already downloaded these packages into a cache on the machine. You do not commit that cache. You commit the PackageReference lines, or in this course you simply keep them in the project file. dotnet test restores again when the cache is empty. You do not have to type dotnet restore yourself for this check.
The packages come from the public NuGet feed unless the machine is configured otherwise. This page does not add a feed, a key, or a private source.

The program never took a package

Nothing in Pine.Main names a package. The eight lines come from Hold, the lambda, Where, and Tag. All of those are in the SDK or in your own file. Adding a PackageReference to PineLab.csproj for a library you never call would not change the eight lines, and it would put a package on the project that has stayed empty. Do not add one.
The tests stay a second view of the same values. Assert.Equal(tag, same) still passes because a record compares Name and Count. Assert.NotEqual(number, again) still passes because Hold<T> is a class. NuGet did not change those rules. It only made the words Fact and Assert available to the compiler.
If dotnet list PineLab package starts showing xunit, the line was added to the wrong project. Remove it from PineLab.csproj and confirm it is still in PineLab.Tests.csproj. Then the program list is empty of packages and the test list still shows xunit.

The course stops before you publish

You can keep the eight lines, keep Passed: 2, and point at the PackageReference that made the tests compile. There is no next page. Optional Advanced ends here. Publishing a library is a different job: dotnet pack builds a package from a project, and dotnet nuget push uploads it to a feed. This course stops before both. You do not pack PineLab and you do not upload it.
One last pass: PineLab.csproj has no PackageReference, PineLab.Tests.csproj still has Include="xunit" with the template's version, plus the test SDK and the runner, the test class is unchanged, dotnet run inside PineLab shows the eight lines, and dotnet test reports Passed: 2. When both commands match and the package id is in the test project only, NuGet has done the one job this course asked of it.

🧠 Test Your Knowledge

Ready to Start

Test Your Knowledge

Challenge yourself with this interactive quiz and see how well you understand the topic

❓
4
Questions
🎯
70%
To Pass
♾️
∞
Time
🔄
∞
Attempts

📝 Instructions

  • Read each question carefully
  • Select the best answer for each question
  • You can retake the quiz as many times as you want
  • Your progress will be shown at the top