• Stars
    star
    730
  • Rank 59,618 (Top 2 %)
  • Language
    C#
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

ArchUnit

ArchUnitNET Build Status License Nuget

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

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.

More Repositories

1

ArchUnit

A Java architecture test library, to specify and assert architecture rules in plain Java
Java
3,038
star
2

boost-python-examples

Some examples for the use of boost::python
C++
823
star
3

ArchUnit-Examples

Examples for ArchUnit (A Java architecture test library, to specify and assert architecture rules in plain Java)
Java
528
star
4

JGiven

Behavior-Driven Development in plain Java
Java
431
star
5

junit-dataprovider

A TestNG like dataprovider runner for JUnit with many additional features
Java
243
star
6

virtual-office

Virtual Office gives you transparency on what Zoom.us rooms are currently occupied and who is present
TypeScript
122
star
7

keycloak-mock

A Java library to test REST endpoints secured by Keycloak via OpenID connect.
Java
110
star
8

ngqp

Declaratively synchronize form controls with the URL
TypeScript
81
star
9

next-generation-scrum-poker

Prototype for a new Scrum Poker
TypeScript
46
star
10

please-cli

An AI helper for creating CLI commands
Shell
42
star
11

cumulus

Cumulus. Threat modeling the Clouds.
TeX
26
star
12

ApiCenter

A repository for all your API specifications
TypeScript
25
star
13

momo-scheduler

A scheduler that persists jobs in MongoDB
TypeScript
16
star
14

gs-rs

Rust
15
star
15

svnfiltereddump

A tool to extract parts from Subversion repositories
Python
14
star
16

flink-connector-email

IMAP / SMTP connectors for Apache Flinkยฎ
Java
13
star
17

ngx-structurals

Structural utility directives for Angular
TypeScript
13
star
18

mustard-cli

A command line interface for bitbucket to review, comment and approve pull requests
C++
12
star
19

sinon-helpers

Create easily configurable sinon stubs that mimic constructors and keep track of their instances
JavaScript
12
star
20

jgiven-intellij-plugin

IntelliJ Plugin for JGiven
Kotlin
11
star
21

openpgp-validation-server

Automated Validation of OpenPGP Keys
Go
7
star
22

jiradeps

Graphically assess the dependencies of your Jira stories
Python
6
star
23

property-loader

Original PropertyLoader reworked
Java
6
star
24

value-provider

Java
5
star
25

rest-demo-jersey

Spring-boot and Jersey based REST service showing the integration of rest-schemagen.
Java
4
star
26

please-pwsh

An AI helper for creating CLI commands in Powershell
PowerShell
4
star
27

NextGenerationTranslatorBackend

A translation backend for AI translation models
Python
3
star
28

homebrew-please

A Homebrew tap for the Please CLI
Ruby
2
star
29

apt-please

2
star
30

karma-child-process

JavaScript
2
star
31

xtab-converter

Convert existing dbunit XLS files to XML data sets
Java
1
star
32

TNG

1
star
33

SnapToJsClipper

JavaScript
1
star