Back to Xunit

Home

site/index.md

latest11.8 KB
Original Source

{.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 their 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
text
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#
text
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:

csharp
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
text
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
text
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#
text
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:

fsharp
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
text
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
text
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
text
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:

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
text
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 }

StablePrerelease
Core Framework v33.2.2{: .release }None
Core Framework v22.9.3{: .release }None
Analyzers1.27.0{: .release }None
Visual Studio adapter3.1.5{: .release }None

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

Latest NuGet Packages{ #packages }

{: .table .latest }

Latest stableLatest CI (how to use)Build status
xunit.v3
xunit
xunit.analyzers
xunit.runner.visualstudio

Issues and Discussions{ #issues }

Github Projects{ #github }

{: 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