![]() |
VOOZH | about |
dotnet add package TngTech.ArchUnitNET --version 0.13.3
NuGet\Install-Package TngTech.ArchUnitNET -Version 0.13.3
<PackageReference Include="TngTech.ArchUnitNET" Version="0.13.3" />
<PackageVersion Include="TngTech.ArchUnitNET" Version="0.13.3" />Directory.Packages.props
<PackageReference Include="TngTech.ArchUnitNET" />Project file
paket add TngTech.ArchUnitNET --version 0.13.3
#r "nuget: TngTech.ArchUnitNET, 0.13.3"
#:package TngTech.ArchUnitNET@0.13.3
#addin nuget:?package=TngTech.ArchUnitNET&version=0.13.3Install as a Cake Addin
#tool nuget:?package=TngTech.ArchUnitNET&version=0.13.3Install as a Cake Tool
<img src="https://raw.githubusercontent.com/TNG/ArchUnitNET/refs/heads/main/Logo/ArchUnitNET-Logo.png" height="64" alt="ArchUnit">
Visit our documentation at https://archunitnet.readthedocs.io/en/latest/.
ArchUnitNET is a free, simple library for checking the architecture of C# code. It is the C# fork of https://www.archunit.org/ for Java. ArchUnitNET can check dependencies between classes, members, interfaces, and more. This is done by analyzing C# bytecode and importing all classes into our C# code structure. The main focus of ArchUnitNET is to automatically test architecture and coding rules.
To use ArchUnitNET, install the ArchUnitNET package from NuGet:
PS> Install-Package TngTech.ArchUnitNET
If you want to use xUnit, NUnit or MSTestV2 for your unit tests, you should instead install the corresponding ArchUnit extension:
PS> Install-Package TngTech.ArchUnitNET.xUnit
PS> Install-Package TngTech.ArchUnitNET.xUnitV3
PS> Install-Package TngTech.ArchUnitNET.NUnit
PS> Install-Package TngTech.ArchUnitNET.MSTestV2
Then you will want to create a class to start testing. We used xUnit with the ArchUnit extension here, but it works similarly with NUnit or other Unit Test Frameworks.
using ArchUnitNET.Domain;
using ArchUnitNET.Loader;
using ArchUnitNET.Fluent;
using Xunit;
//add a using directive to ArchUnitNET.Fluent.ArchRuleDefinition to easily define ArchRules
using static ArchUnitNET.Fluent.ArchRuleDefinition;
namespace ExampleTest
{
public class ExampleArchUnitTest
{
// TIP: load your architecture once at the start to maximize performance of your tests
private static readonly Architecture Architecture = new ArchLoader().LoadAssemblies(
System.Reflection.Assembly.Load("ExampleClassAssemblyName"),
System.Reflection.Assembly.Load("ForbiddenClassAssemblyName")
).Build();
// replace <ExampleClass> and <ForbiddenClass> with classes from the assemblies you want to test
//declare variables you'll use throughout your tests up here
//use As() to give them a custom description
private readonly IObjectProvider<IType> ExampleLayer =
Types().That().ResideInAssembly("ExampleAssembly").As("Example Layer");
private readonly IObjectProvider<Class> ExampleClasses =
Classes().That().ImplementInterface("IExampleInterface").As("Example Classes");
private readonly IObjectProvider<IType> ForbiddenLayer =
Types().That().ResideInNamespace("ForbiddenNamespace").As("Forbidden Layer");
private readonly IObjectProvider<Interface> ForbiddenInterfaces =
Interfaces().That().HaveFullNameContaining("forbidden").As("Forbidden Interfaces");
//write some tests
[Fact]
public void TypesShouldBeInCorrectLayer()
{
//you can use the fluent API to write your own rules
IArchRule exampleClassesShouldBeInExampleLayer =
Classes().That().Are(ExampleClasses).Should().Be(ExampleLayer);
IArchRule forbiddenInterfacesShouldBeInForbiddenLayer =
Interfaces().That().Are(ForbiddenInterfaces).Should().Be(ForbiddenLayer);
//check if your architecture fulfils your rules
exampleClassesShouldBeInExampleLayer.Check(Architecture);
forbiddenInterfacesShouldBeInForbiddenLayer.Check(Architecture);
//you can also combine your rules
IArchRule combinedArchRule =
exampleClassesShouldBeInExampleLayer.And(forbiddenInterfacesShouldBeInForbiddenLayer);
combinedArchRule.Check(Architecture);
}
[Fact]
public void ExampleLayerShouldNotAccessForbiddenLayer()
{
//you can give your rules a custom reason, which is displayed when it fails
//(together with the types that failed the rule)
IArchRule exampleLayerShouldNotAccessForbiddenLayer = Types().That().Are(ExampleLayer).Should()
.NotDependOnAny(ForbiddenLayer).Because("it's forbidden");
exampleLayerShouldNotAccessForbiddenLayer.Check(Architecture);
}
[Fact]
public void ForbiddenClassesShouldHaveCorrectName()
{
Classes().That().AreAssignableTo(ForbiddenInterfaces).Should().HaveNameContaining("forbidden")
.Check(Architecture);
}
[Fact]
public void ExampleClassesShouldNotCallForbiddenMethods()
{
Classes().That().Are(ExampleClasses).Should().NotCallAny(
MethodMembers().That().AreDeclaredIn(ForbiddenLayer).Or().HaveNameContaining("forbidden"))
.Check(Architecture);
}
}
}
Since ArchUnitNET is reading the architecture form the analyzed binaries, it is recommended to run ArchUnitNET-based tests in Debug configuration.
dotnet test -c Debug
For more details on known edge cases, see the documentation.
Check out test examples for the current release at ArchUnitNET Examples.
ArchUnitNET is published under the Apache License 2.0. For more information concerning the license, see Apache License 2.0.
Thanks to all the people who have contributed to the project.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 is compatible. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 5 NuGet packages that depend on TngTech.ArchUnitNET:
| Package | Downloads |
|---|---|
|
TngTech.ArchUnitNET.xUnit
xUnit Extension for the C# Version of ArchUnit (see: archunit.org) |
|
|
TngTech.ArchUnitNET.NUnit
NUnit Extension for the C# Version of ArchUnit (see: archunit.org) |
|
|
TngTech.ArchUnitNET.MSTestV2
MSTestV2 Extension (compatible with MSTestV3) for the C# Version of ArchUnit (see: archunit.org) |
|
|
TngTech.ArchUnitNET.xUnitV3
xUnit v3 Extension for the C# Version of ArchUnit (see: archunit.org) |
|
|
TngTech.ArchUnitNET.TUnit
TUnit Extension for the C# Version of ArchUnit (see: archunit.org) |
Showing the top 2 popular GitHub repositories that depend on TngTech.ArchUnitNET:
| Repository | Stars |
|---|---|
|
karaoke-dev/karaoke
Will be the best karaoke system.
|
|
|
valentinajemuovic/banking-kata-dotnet
Banking Kata (.NET)
|
| Version | Downloads | Last Updated |
|---|---|---|
| 0.13.3 | 586,285 | 3/5/2026 |
| 0.13.2 | 206,800 | 1/23/2026 |
| 0.13.1 | 181,073 | 12/15/2025 |
| 0.13.0 | 65,489 | 12/5/2025 |
| 0.12.3 | 381,718 | 9/26/2025 |
| 0.12.2 | 7,334 | 9/24/2025 |
| 0.12.1 | 364,325 | 7/14/2025 |
| 0.12.0 | 13,998 | 7/11/2025 |
| 0.11.4 | 408,960 | 4/28/2025 |
| 0.11.3 | 263,652 | 2/28/2025 |
| 0.11.3-preview.1 | 1,300 | 1/24/2025 |
| 0.11.2 | 271,800 | 1/24/2025 |
| 0.11.1 | 390,734 | 10/25/2024 |
| 0.11.0 | 343,618 | 8/23/2024 |
| 0.11.0-preview.1 | 417 | 8/19/2024 |
| 0.10.6 | 1,857,572 | 7/12/2023 |
| 0.10.5 | 573,556 | 11/12/2022 |
| 0.10.4 | 234,792 | 8/7/2022 |
| 0.10.3 | 27,664 | 8/3/2022 |
| 0.10.2 | 19,810 | 7/31/2022 |