GitHub

title Home

.NET Foundation logo{.dnflogo}

About xUnit.net

xUnit.net is a free, open source, community-focused unit testing tool for C#, F#, and Visual Basic. xUnit.net v3 supports .NET 8.0 or later, and .NET Framework 4.7.2 or later.

xUnit.net works with the .NET SDK command line tools, Visual Studio, Visual Studio Code, JetBrains Rider, NCrunch, and any development environment compatible with Microsoft.Testing.Platform or VSTest.

xUnit.net is part of the .NET Foundation and operates under a code of conduct. It is licensed under Apache 2 (an OSI approved license). The project is governed by a Project Lead.

{ .about-links }

Follow: xUnit.net on Mastodon, xUnit.net on Bluesky, Brad Wilson, James Newkirk
JetBrains Rider support is provided and supported by JetBrains.
NCrunch support is provided and supported by Remco Software.
The xUnit.net logo was designed by Nathan Young.

Quick Start with .NET SDK{ #quick-start }

C#

Install the xUnit.net v3 project templates:

dotnet new install xunit.v3.templates
The following template packages will be installed:
    xunit.v3.templates
Success: xunit.v3.templates::2.0.3 installed the following templates:
Template Name                   Short Name        Language    Tags
------------------------------  ----------------  ----------  ----------
xUnit.net v3 Extension Project  xunit3-extension  [C#],F#,VB  Test/xUnit
xUnit.net v3 Test Project       xunit3            [C#],F#,VB  Test/xUnit

Create a unit test project:

dotnet new xunit3 --language C#
The template "xUnit.net v3 Test Project" was created successfully.
Processing post-creation actions...
Restoring C:\Dev\SampleProject\SampleProject.csproj:
Restore succeeded.

Edit UnitTest1.cs:

namespace SampleProject;
public class UnitTest1
{
    public static int Add(int x, int y) =>
        x + y;
    [Fact]
    public void Good() =>
        Assert.Equal(4, Add(2, 2));
    [Fact]
    public void Bad() =>
        Assert.Equal(5, Add(2, 2));
}

Execute the tests:

dotnet run
xUnit.net v3 In-Process Runner v2.0.3+216a74a292 (64-bit .NET 8.0.17)
  Discovering: SampleProject
  Discovered:  SampleProject
  Starting:    SampleProject
    SampleProject.UnitTest1.Bad [FAIL]
      Assert.Equal() Failure: Values differ
      Expected: 5
      Actual:   4
      Stack Trace:
        UnitTest1.cs(14,0): at SampleProject.UnitTest1.Bad()
  Finished:    SampleProject
=== TEST EXECUTION SUMMARY ===
    SampleProject  Total: 2, Errors: 0, Failed: 1, Skipped: 0, Not Run: 0, Time: 0.054s

F#

Install the xUnit.net v3 project templates:

dotnet new install xunit.v3.templates
The following template packages will be installed:
    xunit.v3.templates
Success: xunit.v3.templates::2.0.3 installed the following templates:
Template Name                   Short Name        Language    Tags
------------------------------  ----------------  ----------  ----------
xUnit.net v3 Extension Project  xunit3-extension  [C#],F#,VB  Test/xUnit
xUnit.net v3 Test Project       xunit3            [C#],F#,VB  Test/xUnit

Create a unit test project:

dotnet new xunit3 --language F#
The template "xUnit.net v3 Test Project" was created successfully.
Processing post-creation actions...
Restoring C:\Dev\SampleProject\SampleProject.fsproj:
Restore succeeded.

Edit UnitTest1.fs:

module UnitTest1
open Xunit
let Add(x: int, y: int) =
    x + y
[<Fact>]
let Good() =
    Assert.Equal(4, Add(2, 2))
[<Fact>]
let Bad() =
    Assert.Equal(5, Add(2, 2))

Execute the tests: dotnet run

dotnet run
xUnit.net v3 In-Process Runner v2.0.3+216a74a292 (64-bit .NET 8.0.17)
  Discovering: SampleProject
  Discovered:  SampleProject
  Starting:    SampleProject
    UnitTest1.Bad [FAIL]
      Assert.Equal() Failure: Values differ
      Expected: 5
      Actual:   4
      Stack Trace:
        UnitTest1.fs(14,0): at UnitTest1.Bad()
  Finished:    SampleProject
=== TEST EXECUTION SUMMARY ===
    SampleProject  Total: 2, Errors: 0, Failed: 1, Skipped: 0, Not Run: 0, Time: 0.052s

Visual Basic

Install the xUnit.net v3 project templates:

dotnet new install xunit.v3.templates
The following template packages will be installed:
    xunit.v3.templates
Success: xunit.v3.templates::2.0.3 installed the following templates:
Template Name                   Short Name        Language    Tags
------------------------------  ----------------  ----------  ----------
xUnit.net v3 Extension Project  xunit3-extension  [C#],F#,VB  Test/xUnit
xUnit.net v3 Test Project       xunit3            [C#],F#,VB  Test/xUnit

Create a unit test project:

dotnet new xunit3 --language VB
The template "xUnit.net v3 Test Project" was created successfully.
Processing post-creation actions...
Restoring C:\Dev\SampleProject\SampleProject.vbproj:
Restore succeeded.

Edit UnitTest1.vb:

Imports Xunit
Public Class UnitTest1
    Public Function Add(x As Integer, y As Integer) As Integer
        Return x + y
    End Function
    <Fact>
    Public Sub Good()
        Assert.Equal(4, Add(2, 2))
    End Sub
    <Fact>
    Public Sub Bad()
        Assert.Equal(5, Add(2, 2))
    End Sub
End Class

Execute the tests: dotnet run

dotnet run
xUnit.net v3 In-Process Runner v2.0.3+216a74a292 (64-bit .NET 8.0.17)
  Discovering: SampleProject
  Discovered:  SampleProject
  Starting:    SampleProject
    SampleProject.UnitTest1.Bad [FAIL]
      Assert.Equal() Failure: Values differ
      Expected: 5
      Actual:   4
      Stack Trace:
        UnitTest1.vb(16,0): at SampleProject.UnitTest1.Bad()
  Finished:    SampleProject
=== TEST EXECUTION SUMMARY ===
    SampleProject  Total: 2, Errors: 0, Failed: 1, Skipped: 0, Not Run: 0, Time: 0.052s

Latest Release Notes{ #release-notes }

{: .latest }

Stable Prerelease
Core Framework v3 4.0.0{: .release } None
Core Framework v2 [1] 2.9.3{: .release } None
Analyzers 2.0.0{: .release } None
Visual Studio adapter 4.0.0{: .release } None

For older release notes, see the full release notes list.

Latest NuGet Packages{ #packages }

{: .table .latest }

Latest stable Latest CI (how to use) Build status
xunit.v3
xunit [1] N/A
xunit.analyzers
xunit.runner.visualstudio

Issues and Discussions{ #issues }

Github Projects{ #github }

Links to Resources{ #links }

Powered by NDepend{: width="175" }

Sponsors{ #sponsors }

Help support this project by becoming a sponsor through GitHub Sponsors.

Additional copyrights{ #copyrights }

Portions copyright The Legion Of The Bouncy Castle

Footnotes

  1. Core Framework v2 is in maintenance mode. Critical bug fixes will be issued, but no new feature work is being done. All new features are taking place with Core Framework v3 only. Developers are encouraged to upgrade from v2 to v3 at their earliest convenience.

Read the original on github.com ↗