Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsJul 14, 2026·3 min de lecture

xUnit.net — Community-Focused Unit Testing for .NET

xUnit.net is a modern, extensible unit testing framework for .NET that uses a clean architecture and sensible defaults to encourage better testing practices.

Prêt pour agents

Installation agent prête

Cet actif peut être installé après choix du runtime, vérification du plan et exécution de la commande adaptée.

Native · 98/100Policy : autoriser
Surface agent
Tout agent MCP/CLI
Type
Skill
Installation
Single
Confiance
Confiance : Established
Point d'entrée
xUnit.net Overview
Commande d'installation directe
npx -y tokrepo@latest install 0377def7-7f5d-11f1-9bc6-00163e2b0d79 --target codex

À exécuter après confirmation du plan en dry-run.

Introduction

xUnit.net is the latest evolution of unit testing frameworks for the .NET ecosystem, created by the original author of NUnit v2. It emphasizes isolation, extensibility, and modern patterns, and is the default testing framework for many .NET projects including ASP.NET Core.

What xUnit.net Does

  • Provides Fact and Theory attributes for simple and parameterized tests
  • Creates a new test class instance per test method to prevent shared state bugs
  • Supports parallel test execution at the collection and assembly level
  • Outputs results compatible with Visual Studio, JetBrains Rider, and CI systems
  • Extends through custom attributes, test collectors, and output formatters

Architecture Overview

xUnit.net consists of a core library defining test attributes and assertions, a runner that discovers and executes tests, and platform-specific adapters for Visual Studio and dotnet CLI. Test discovery uses reflection to find methods decorated with Fact or Theory. Each test runs in its own class instance, and collection fixtures manage shared expensive resources.

Self-Hosting & Configuration

  • Add xunit and xunit.runner.visualstudio NuGet packages to your test project
  • Configure test parallelism via assembly-level attributes or xunit.runner.json
  • Use collection fixtures for shared setup across related tests
  • Set up CI with dotnet test which auto-discovers xUnit test projects
  • Customize output with ITestOutputHelper for per-test logging

Key Features

  • Constructor injection and IDisposable replace setup/teardown methods
  • Theory attribute with InlineData, MemberData, and ClassData for parameterized tests
  • Built-in parallel execution with configurable collection isolation
  • No static state between tests prevents hidden dependencies
  • Extensible trait system for categorizing and filtering tests

Comparison with Similar Tools

  • NUnit — established .NET testing framework with more attributes; xUnit favors constructor-based patterns over setup attributes
  • MSTest — Microsoft's built-in framework; xUnit offers better isolation and community-driven development
  • Kotest — Kotlin testing framework; xUnit serves the same purpose for the .NET ecosystem
  • Jest — JavaScript testing; xUnit is the equivalent standard for C# and .NET projects
  • pytest — Python testing; xUnit follows similar minimalist philosophy for .NET

FAQ

Q: Why does xUnit create a new instance per test? A: This ensures complete test isolation so one test cannot affect another through shared class state.

Q: How do I share setup across multiple tests? A: Use collection fixtures for expensive shared resources, or class fixtures for per-class setup.

Q: Does it work with .NET Framework and .NET Core? A: Yes, xUnit.net supports .NET Framework 4.6.2+ and all versions of .NET Core and .NET 5+.

Q: Can I run tests in parallel? A: Yes, by default tests in different collections run in parallel. Configure maxParallelThreads in xunit.runner.json.

Sources

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires