GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - TNG/ArchUnitNET: A C# architecture test library to specify and assert architecture rules in C# for automated testing.

BranchesTags
Open more actions menu

Latest commit

 

History

1,285 Commits
1,285 Commits

Folders and files

NameName
Last commit message
Last commit date
.config
.config
 
 
.github
.github
 
 
.mise/tasks
.mise/tasks
 
 
ArchUnitNET.MSTestV2
ArchUnitNET.MSTestV2
 
 
ArchUnitNET.MSTestV2Tests
ArchUnitNET.MSTestV2Tests
 
 
ArchUnitNET.MSTestV3Tests
ArchUnitNET.MSTestV3Tests
 
 
ArchUnitNET.MSTestV4
ArchUnitNET.MSTestV4
 
 
ArchUnitNET.MSTestV4Tests
ArchUnitNET.MSTestV4Tests
 
 
ArchUnitNET.NUnit
ArchUnitNET.NUnit
 
 
ArchUnitNET.NUnitTests
ArchUnitNET.NUnitTests
 
 
ArchUnitNET.TUnit
ArchUnitNET.TUnit
 
 
ArchUnitNET.TUnitTests
ArchUnitNET.TUnitTests
 
 
ArchUnitNET.xUnit
ArchUnitNET.xUnit
 
 
ArchUnitNET.xUnitTests
ArchUnitNET.xUnitTests
 
 
ArchUnitNET.xUnitV3
ArchUnitNET.xUnitV3
 
 
ArchUnitNET.xUnitV3Tests
ArchUnitNET.xUnitV3Tests
 
 
ArchUnitNET
ArchUnitNET
 
 
ArchUnitNETTests
ArchUnitNETTests
 
 
ExampleTest
ExampleTest
 
 
Licenses
Licenses
 
 
Logo
Logo
 
 
TestAssemblies
TestAssemblies
 
 
TestAssembly
TestAssembly
 
 
documentation
documentation
 
 
.csharpierignore
.csharpierignore
 
 
.git-blame-ignore-revs
.git-blame-ignore-revs
 
 
.gitconfig
.gitconfig
 
 
.gitignore
.gitignore
 
 
.gitleaks.toml
.gitleaks.toml
 
 
.readthedocs.yml
.readthedocs.yml
 
 
AGENTS.md
AGENTS.md
 
 
ArchUnit.sln
ArchUnit.sln
 
 
ArchUnit.sln.DotSettings
ArchUnit.sln.DotSettings
 
 
ArchUnitNET.mtp.slnf
ArchUnitNET.mtp.slnf
 
 
ArchUnitNET.vstest.slnf
ArchUnitNET.vstest.slnf
 
 
CONTRIBUTING.md
CONTRIBUTING.md
 
 
DCO
DCO
 
 
LICENSE
LICENSE
 
 
NOTICE
NOTICE
 
 
README.md
README.md
 
 
codecov.yml
codecov.yml
 
 
coverlet.runsettings
coverlet.runsettings
 
 
global.json
global.json
 
 
hk.pkl
hk.pkl
 
 
mise.toml
mise.toml
 
 
renovate.json
renovate.json
 
 
strongNameKey.snk
strongNameKey.snk
 
 

Repository files navigation

ArchUnit

ArchUnitNET Build Status License Nuget codecov

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

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);
        }
    }
}

Run the tests

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.

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.

Contributors ✨

Thanks to all the people who have contributed to the project.

About

A C# architecture test library to specify and assert architecture rules in C# for automated testing.

Resources

Readme
Apache-2.0 license

Contributing

Contributing
Activity
Custom properties

Stars

1.4k stars

Watchers

18 watching

Forks

84 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.