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

For a new .NET test project, this tutorial follows the current xUnit.net v3 getting-started path: create a project with the xUnit v3 template, write a [Fact] for one expected behavior, add a [Theory] for multiple inputs, and run the project with its configured test runner. The commands below are specific to the v3 template path; existing v2 projects should use the v2 guidance instead.

Choose the xUnit version and runner first

xUnit.net is a unit-testing framework for C#, F#, and Visual Basic. The new-project walkthrough here uses xUnit.net v3. The official v3 guide describes support for .NET 8 or later and .NET Framework 4.7.2 or later; .NET Framework is officially supported only on Windows. Check the current getting-started guide and migration guidance before pinning package or SDK versions, because the guide’s example versions are a dated snapshot, not a permanent recommendation.

There are two runner paths to distinguish. The v3 template’s documented default uses Microsoft Testing Platform (MTP). If you need VSTest integration, the documented setup uses xunit.runner.visualstudio and Microsoft.NET.Test.Sdk. Do not mix these package instructions without choosing the corresponding runner configuration.

Create a v3 test project

Install the official templates, create a test project, and run the generated example. The templates are available for C#, F#, and VB.NET.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.
  1. Install the templates: dotnet new install xunit.v3.templates

  2. Create a project: dotnet new xunit3 -n Example.Tests

  3. Enter the project directory: cd Example.Tests

  4. Run the project: dotnet run

The generated v3 project uses the xunit.v3.mtp-v2 template configuration. The project is a stand-alone executable, rather than the library-plus-runner arrangement used by v2. See the official v3 setup instructions for current template options.

When the solution uses VSTest

If your intended workflow is VSTest—such as using Visual Studio Test Explorer or Visual Studio Code’s Testing panel—follow the adapter setup documented by xUnit.net. The needed packages are xunit.runner.visualstudio and Microsoft.NET.Test.Sdk; run the test project with dotnet test. The project configuration and runner must agree: the standalone v3 template’s default dotnet run path is not interchangeable with an unconfigured VSTest setup. Consult the v3 getting-started guide and the test-running guidance for the selected path.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Write a first test with [Fact]

A test should make a behavioral claim that can fail for a meaningful reason. For example, suppose a small service decides whether an integer is prime. A fact can check one straightforward prime input:

using Xunit;

public class PrimeServiceTests
{
    [Fact]
    public void IsPrime_Seven_ReturnsTrue()
    {
        var service = new PrimeService();

        Assert.True(service.IsPrime(7));
    }
}

This example assumes the application has a PrimeService class with an IsPrime(int) method. The test’s name records the input and expected outcome, while the assertion checks the result visible to a caller. A placeholder such as Assert.True(true) passes without testing application behavior.

Use a test-first loop

  1. Write a test describing one behavior that the method should provide.

  2. Run the test and confirm that it fails for the expected reason—for example, the method is not implemented or returns the wrong value.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
    Rank #3
    Sale
    Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
    • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
    • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
    • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
    • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
    • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.
  3. Implement the smallest change that makes the behavior pass.

  4. Run the test again, then add tests for other important cases.

Microsoft Learn’s C# xUnit tutorial uses a prime-checking service to demonstrate this test-first approach. The goal is not to write tests that pass at any cost; a useful failing result first shows that the test can detect a missing or incorrect behavior.

Use [Theory] for cases that share one rule

The xUnit.net documentation puts the distinction this way: “Facts are tests which are always true. They test invariant conditions.” The same documentation describes theories as tests that are true for a particular set of data. Use a [Theory] when the same logic should be checked with several inputs, rather than copying nearly identical test methods.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft
using Xunit;

public class PrimeServiceTests
{
    [Theory]
    [InlineData(2)]
    [InlineData(3)]
    [InlineData(5)]
    [InlineData(7)]
    public void IsPrime_PrimeNumber_ReturnsTrue(int value)
    {
        var service = new PrimeService();

        Assert.True(service.IsPrime(value));
    }

    [Theory]
    [InlineData(0)]
    [InlineData(1)]
    [InlineData(4)]
    [InlineData(9)]
    public void IsPrime_NonPrimeNumber_ReturnsFalse(int value)
    {
        var service = new PrimeService();

        Assert.False(service.IsPrime(value));
    }
}

Each [InlineData] row supplies an argument to the same test method. A runner reports each theory data set as an individual test, so a failure identifies the case to inspect. Here, the two theories check both sides of the rule, including 0 and 1, which are not prime.

Run tests and interpret failures

For the v3 standalone template in this walkthrough, run dotnet run from the test project directory. For a VSTest-configured project, use dotnet test. Microsoft’s tutorial also demonstrates a solution workflow with separate application and test projects; in that setup, run the solution’s tests with the runner and project configuration you selected.

When a test fails, read the reported test name and case, then compare the expected and actual values. A failing assertion is evidence about the behavior covered by that test, not proof that every other behavior is correct. Fix the implementation or correct the test only after checking that its expected result matches the intended rule. With theories, use the reported input to narrow the failure to a particular data row.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Common setup and test problems

Or skip the browser setup:

xUnit tests verify .NET code behavior; ScreenshotNeo is a separate option for developers who also need website screenshots. Make one GET request to capture a page:

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 removes cookie and consent banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are not billed; its MCP server lets AI agents take screenshots; and 1,000 screenshots a month are free with no card, with paid plans starting at $5 for 3,000. Learn about ScreenshotNeo or sign up for 1,000 free screenshots a month with no card.

Frequently Asked Questions

Can I use xUnit.net with languages other than C#?

Yes. xUnit.net supports C#, F#, and Visual Basic.

Does each [InlineData] row count as a separate test?

Yes. A runner reports each theory data set as an individual test.

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.

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