C# xUnit
Write a check that the computer runs for you. xUnit is a test library. A method marked [Fact] is one test. This page adds a second project, PineLab.Tests, next to the program you already have. One test expects two Tag records with the same values to be equal. One test expects two Hold objects with 12 to be different objects.
The records page printed eight lines, and it still does. dotnet run from PineLab shows pine, 12, pine x12, 12,8, pine x20, then Tag { Name = pine, Count = 20 }, True, and False. Optional Advanced continues, so that program stays. The tests do not print those lines. They answer with a count. If dotnet test says it found no tests, you are in the wrong folder or the [Fact] attributes are missing.
A second project
From the folder that holds PineLab, not from inside it, run:
dotnet new xunit -n PineLab.Tests
That makes a folder PineLab.Tests with its own .csproj and a starter file. Open the starter and replace it with the test class below. Then tell the test project about the program:
dotnet add PineLab.Tests reference PineLab
Hold and Tag are already public, so the test project can see them. Pine stays public too. If either type were not public, the test file would not compile.
The test class is Checks. It does not have a Main. xUnit finds the methods and calls them. Each method has [Fact] on the line above it. Fact comes from Xunit, so the file starts with using Xunit;.
using System;
using System.Linq;
public class Hold<T>
{
public T Item;
public Hold(T item)
{
Item = item;
}
}
public record Tag(string Name, int Count);
public class Pine
{
public static void Main()
{
Hold<string> word = new Hold<string>("pine");
Hold<int> number = new Hold<int>(12);
Func<string, int, string> label = (name, count) => name + " x" + count;
int[] counts = { 3, number.Item, 8 };
int[] big = counts.Where(n => n > 5).ToArray();
Console.WriteLine(word.Item);
Console.WriteLine(number.Item);
Console.WriteLine(label(word.Item, number.Item));
Console.WriteLine(string.Join(",", big));
Console.WriteLine(label(word.Item, big.Sum()));
Tag tag = new Tag(word.Item, big.Sum());
Tag same = new Tag("pine", 20);
Hold<int> again = new Hold<int>(12);
Console.WriteLine(tag);
Console.WriteLine(tag == same);
Console.WriteLine(number == again);
}
}
Two facts
The first test builds the same two tags the program builds and asks xUnit to compare them:
Tag tag = new Tag("pine", 20);
Tag same = new Tag("pine", 20);
Assert.Equal(tag, same);
Assert.Equal fails the test when the two values differ. For a record, equal means the same Name and the same Count, which is what the seventh line of the program already showed as True.
The second test builds two holders and asks xUnit to confirm they are not the same object:
Hold<int> number = new Hold<int>(12);
Hold<int> again = new Hold<int>(12);
Assert.NotEqual(number, again);
Assert.NotEqual fails the test when the two values compare as equal. For Hold<T>, == checks identity, which is what the eighth line already showed as False. Two objects with the same 12 are not equal, so NotEqual passes.
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);
}
}
Passed: 2
From the folder that holds both projects, run dotnet test. xUnit runs both methods. The summary line says Passed! - Failed: 0, Passed: 2, Skipped: 0, Total: 2. The numbers are what you check. The eight program lines do not appear, because nothing calls Main.
dotnet test builds first. A compile error in either project means no test runs and you do not get Passed: 2.
Run dotnet run from inside PineLab when you want the eight lines. Run dotnet test from the folder above both projects when you want the count. dotnet run inside PineLab.Tests has nothing to run: a test project has no Main, so that command fails and reports no runnable project.
Swap the two asserts and the count changes. Assert.Equal(number, again) expects the two holders to match. They do not, so that test fails and the summary says Failed: 1, Passed: 1. Assert.NotEqual(tag, same) expects the two tags to differ. They do not, so that one fails too. Put each assert back where it is in the file.
A test that expects the wrong number fails the same way. Assert.Equal(21, tag.Count); fails because Count is 20. The record page already showed that 20. Change it back to comparing the two tags.
What a fact is not
[Fact] marks a method with no arguments. xUnit calls it once. A method without [Fact] compiles and never runs, so the summary would say Passed: 1 if you dropped one attribute. Put it back.
The method must be public and return void. private void TwoTags... is invisible to xUnit. A method that returns int does not match what [Fact] marks, and the build fails.
The test names are long on purpose. TwoTagsWithTheSameValuesAreEqual says the rule. xUnit prints the method name when a test fails, so the name is the message you read. A short name like Test1 still runs, and it tells you nothing when it fails. The summary only says a test failed. The method name tells you which rule broke. Keep the long names.
The tests build their own Tag and Hold values. They do not call Pine.Main and they do not read the console. That keeps the check about the values, not about the order of the printed lines.
One assert, one rule
Each fact checks one rule. The first rule is about values: two tags with pine and 20 match. The second rule is about identity: two holders with 12 are two objects. Keeping them in separate methods means a failure names the rule. If you put both asserts in one method, the first failure stops that method and you never learn about the second rule. The summary would also say Total: 1.
The test project does not change the program's output. Delete PineLab.Tests and dotnet run inside PineLab still prints the same eight lines. The tests are a second view of the same facts, not a new program.
NuGet comes next
You can keep the eight-line program and add a second project whose two tests agree with the True and the False. The next page is NuGet. It is how a project downloads a library someone else published, and how dotnet new xunit found xUnit for you. This page stops before that. You do not edit a package list by hand here, and you do not publish PineLab.
One last pass: PineLab unchanged, PineLab.Tests beside it with a project reference, class Checks, two public methods, [Fact] on each, Assert.Equal on the two tags, Assert.NotEqual on the two holders. dotnet test reports Passed: 2. dotnet run inside PineLab still shows the eight lines. When both of those match, the tests did their job.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 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