DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk6 min

How to Run Your First xUnit Test Script

Create a .NET test project, run your first xUnit.net test, and learn why v3 and v2 use different templates and runner commands.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For a new xUnit.net project, install the xUnit.net v3 templates, create a test project with dotnet new xunit3, then run it with dotnet run. If you already have an xUnit.net v2 project, keep to its matching setup: the documented v2 route uses dotnet new xunit and dotnet test. The commands differ because the project templates and test runners differ; do not mix them.

Run a new xUnit.net v3 test project

The .NET SDK supplies the dotnet command-line interface (CLI) used below. The steps follow xUnit.net’s v3 getting-started guide. Its examples are documentation examples, not a promise that your output or generated files will be identical: templates and defaults can vary with SDK and template releases.

  1. Install and check the .NET SDK. Install the SDK for your operating system, open a new terminal, and run dotnet --version. The guide’s sample prints 10.0.102; that is an example SDK version, not a required version. If the command is not found, install the SDK or fix your PATH, then open a fresh terminal.
  2. Install the xUnit.net v3 templates. Run dotnet new install xunit.v3.templates. The package includes xunit3 and xunit3-extension templates, with C#, F#, and VB.NET support. Use xunit3 for an ordinary test project.
  3. Create a project. Run these commands in the terminal:
    mkdir MyFirstUnitTests
    cd MyFirstUnitTests
    dotnet new xunit3

    The template restores the generated project as part of creation in the guide’s example. If you create your test project inside a solution or an existing repository instead, make sure you run later commands from the directory containing the test project’s .csproj.

  4. Inspect the generated test. In the C# template, open UnitTest1.cs. The documented example contains a class with a [Fact] test and Assert.True(true). That assertion is a placeholder: it proves the runner can discover and execute a test, but checks no application behavior.
  5. Run the test project. From the test-project directory, run:
    dotnet run

    A successful run should report test discovery and execution, with no errors or failures. The guide’s example reports one test; treat that as sample output, not as a fixed count or byte-for-byte output for every SDK and template.

Replace the placeholder with a useful assertion

A test is valuable when its assertion checks behavior that matters. For example, suppose the project has an Add method. A test can assert that adding 2 and 2 returns 4:

using Xunit;

public class UnitTest1
{
    [Fact]
    public void Add_TwoNumbers_ReturnsTheirSum()
    {
        Assert.Equal(4, Add(2, 2));
    }

    private static int Add(int left, int right) => left + right;
}

This compact example keeps the method in the test class so it can stand alone. In an actual application, test the method in the production project rather than defining a second copy inside the test. A deliberately incorrect expected value, such as Assert.Equal(5, Add(2, 2)), should fail; the runner’s diagnostic identifies expected and actual values and a source location. A failure here is useful evidence that the test can detect an incorrect result.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a fact for an invariant

A [Fact] test checks a condition expected to hold for the particular case it exercises. xUnit.net’s v3 guide puts it this way: “Facts are tests which are always true. They test invariant conditions.”

Use a theory for several inputs

A [Theory] runs the same test logic against supplied data, commonly with [InlineData]. For example:

using Xunit;

public class AdditionTests
{
    [Theory]
    [InlineData(2, 2, 4)]
    [InlineData(-1, 1, 0)]
    [InlineData(0, 5, 5)]
    public void Add_ReturnsExpectedSum(int left, int right, int expected)
    {
        Assert.Equal(expected, Add(left, right));
    }

    private static int Add(int left, int right) => left + right;
}

The runner executes this theory once per supplied row. If one row fails, its input helps identify the case to investigate.

Choose the command that matches your xUnit version and runner

Do not choose a command solely because it is familiar: use the setup configured for the project. The v3 getting-started guide’s default project is configured for Microsoft Testing Platform and runs with dotnet run. The separate xUnit.net v2 guide demonstrates a VSTest setup and runs with dotnet test.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Setup Template command Documented runner path Execution command
xUnit.net v3, new project dotnet new xunit3 The getting-started default uses Microsoft Testing Platform. The guide says selecting VSTest adds xunit.runner.visualstudio and Microsoft.NET.Test.Sdk. dotnet run for the default documented setup; follow the matching runner instructions if you choose VSTest.
xUnit.net v2, existing or intentionally created project dotnet new xunit The v2 guide’s VSTest route references xunit, xunit.runner.visualstudio, and Microsoft.NET.Test.Sdk. dotnet test for the documented VSTest setup.

The v2 guide, dated July 4, 2025, describes xUnit.net v2 as being in maintenance mode: critical bug fixes continue, while new feature work is in v3. That is not a reason to replace packages in an existing project blindly. Check its project file and follow the instructions for the configured version and runner. For template and runner details, see the xUnit.net What’s New page as well as the applicable getting-started guide.

Run tests from an editor if you prefer

A terminal is enough to create and run the first test. If you prefer an IDE, the xUnit.net documentation also describes Visual Studio Test Explorer and VS Code with Microsoft’s C# Dev Kit. Discovery depends on the test project and runner package configuration; for example, the documented VSTest path uses its runner-related package references. An editor is optional, not a prerequisite for the command-line route.

Troubleshoot common first-run problems

  • dotnet is not recognized or not found: the SDK may not be installed, or the terminal may not see its installation directory on PATH. Install the SDK for your operating system, check the installation, and open a fresh terminal before retrying dotnet --version.
  • The template name is unavailable: for the v3 template, install xunit.v3.templates with dotnet new install xunit.v3.templates, then retry dotnet new xunit3. The documented v2 template name is xunit, not xunit3.
  • No tests are discovered, or the command behaves unexpectedly: confirm that you are in the test project directory and that you are using the command for its version and runner setup. The v3 default shown in the guide uses Microsoft Testing Platform and dotnet run; the documented v2 VSTest route uses dotnet test and its package references.
  • A test fails: read the expected and actual values and the reported source location. If you intentionally changed the expected result to test failure reporting, restore the correct assertion; otherwise, inspect the code and the inputs for the failing case.
  • Editor discovery differs from the terminal: check that the editor’s test integration matches the project runner and that the required runner packages are present for the chosen configuration. Use the version-specific xUnit.net instructions rather than adding v2 and v3 setup pieces interchangeably.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

xUnit tests run in a .NET project, so ScreenshotNeo is not needed to create or execute this test. If your developer workflow also needs website screenshots, ScreenshotNeo offers a one-request API. For example, save a screenshot of https://stripe.com as shot.webp with cURL:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

See the ScreenshotNeo API documentation for request options. It accepts consent banners like a visitor and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits cost nothing, and response headers indicate the page verdict and billing status. ScreenshotNeo also has an MCP server with screenshot and PDF tools for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Learn about ScreenshotNeo, or sign up free for 1,000 screenshots a month with no card.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.