TngTech.ArchUnitNET.xUnit
0.10.5
See the version list below for details.
dotnet add package TngTech.ArchUnitNET.xUnit --version 0.10.5
NuGet\Install-Package TngTech.ArchUnitNET.xUnit -Version 0.10.5
<PackageReference Include="TngTech.ArchUnitNET.xUnit" Version="0.10.5" />
paket add TngTech.ArchUnitNET.xUnit --version 0.10.5
#r "nuget: TngTech.ArchUnitNET.xUnit, 0.10.5"
// Install TngTech.ArchUnitNET.xUnit as a Cake Addin #addin nuget:?package=TngTech.ArchUnitNET.xUnit&version=0.10.5 // Install TngTech.ArchUnitNET.xUnit as a Cake Tool #tool nuget:?package=TngTech.ArchUnitNET.xUnit&version=0.10.5
<img src="Logo/ArchUnitNET-Logo.png" height="64" alt="ArchUnit">
ArchUnitNET
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.
An Example
To use ArchUnitNET, install the ArchUnitNET package from NuGet:
PS> Install-Package 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 ArchUnitNET.xUnit
PS> Install-Package ArchUnitNET.NUnit
PS> Install-Package ArchUnitNET.MSTestV2
Create a Test
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);
}
}
}
Further Info and Help
Check out test examples for the current release at ArchUnitNET Examples.
License
ArchUnitNET is published under the Apache License 2.0. For more information concerning the license, see Apache License 2.0.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.5
- TngTech.ArchUnitNET (>= 0.10.5)
- xunit.assert (>= 2.4.1)
-
.NETStandard 2.0
- TngTech.ArchUnitNET (>= 0.10.5)
- xunit.assert (>= 2.4.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on TngTech.ArchUnitNET.xUnit:
Package | Downloads |
---|---|
Cash2u.UnitTesting
Library contains objects for testing |
GitHub repositories (3)
Showing the top 3 popular GitHub repositories that depend on TngTech.ArchUnitNET.xUnit:
Repository | Stars |
---|---|
sveinungf/spreadcheetah
SpreadCheetah is a high-performance .NET library for generating spreadsheet (Microsoft Excel XLSX) files.
|
|
valentinajemuovic/banking-kata-dotnet
Banking Kata (.NET)
|
|
ardalis/modulith
Modulith is a dotnet new template for Modular Monoliths. It streamlines the creation of new .Net solutions and the addition of modules to existing ones.
|
Version | Downloads | Last updated |
---|---|---|
0.11.1 | 18,636 | 10/25/2024 |
0.11.0 | 67,470 | 8/23/2024 |
0.11.0-preview.1 | 203 | 8/19/2024 |
0.10.6 | 683,602 | 7/12/2023 |
0.10.5 | 297,564 | 11/12/2022 |
0.10.4 | 119,317 | 8/7/2022 |
0.10.3 | 21,608 | 8/3/2022 |
0.10.2 | 14,924 | 7/31/2022 |
0.10.1 | 108,819 | 4/29/2022 |
0.10.0 | 707 | 4/27/2022 |
0.9.1 | 85,444 | 3/7/2022 |
0.9.0 | 137,129 | 12/1/2021 |
0.8.5 | 334 | 12/1/2021 |
0.8.4 | 21,020 | 10/19/2021 |
0.8.3 | 8,550 | 10/7/2021 |
0.8.2 | 2,683 | 9/28/2021 |
0.8.1 | 3,604 | 9/27/2021 |
0.8.0 | 3,405 | 9/20/2021 |
0.7.3 | 470 | 9/20/2021 |
0.7.2 | 4,298 | 8/30/2021 |
0.7.1 | 13,567 | 8/25/2021 |
0.7.0 | 12,129 | 7/22/2021 |
0.6.2 | 376 | 7/22/2021 |
0.6.1 | 7,163 | 6/18/2021 |
0.6.0 | 629 | 6/10/2021 |
0.5.6 | 18,110 | 5/21/2021 |
0.5.5 | 415 | 5/20/2021 |
0.5.4 | 8,395 | 4/28/2021 |
0.5.3 | 6,388 | 3/24/2021 |
0.5.2 | 937 | 3/19/2021 |
0.5.1 | 413 | 3/18/2021 |
0.5.0 | 5,708 | 2/26/2021 |
0.4.3 | 1,933 | 1/28/2021 |
0.4.2 | 821 | 1/20/2021 |
0.3.4 | 12,890 | 12/3/2020 |
0.3.3 | 3,407 | 10/31/2020 |
0.3.2 | 1,345 | 10/5/2020 |
0.3.0 | 471 | 8/28/2020 |
0.2.4 | 467 | 8/24/2020 |
0.2.3 | 1,305 | 2/16/2020 |
0.2.2 | 579 | 12/17/2019 |
0.2.1 | 7,915 | 11/26/2019 |
0.2.0 | 1,122 | 10/31/2019 |