xiaoyvr / NetArchTest

A fluent API for .Net that can enforce architectural rules in unit tests.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NetArchTest

Build Status

A fluent API for .Net Standard that can enforce architectural rules in unit tests.

A fork and rewrite from BenMorris/NetArchTest which is inspired by the ArchUnit library for Java.

Rationale

This project allows you create tests that enforce conventions for class design, naming and dependency in .Net code bases. These can be used with any unit test framework and incorporated into a build pipeline. It uses a fluid API that allows you to string together readable rules that can be used in test assertions.

There are plenty of static analysis tools that can evaluate application structure, but they are aimed more at enforcing generic best practice rather than application-specific conventions. The better tools in this space can be press-ganged into creating custom rules for a specific architecture, but the intention here is to incorporate rules into a test suite and create a self-testing architecture.

The project is inspired by ArchUnit, a java-based library that attempts to address the difficulties of preserving architectural design patterns in code bases over the long term. Many patterns can only be enforced by convention, which tends to rely on a rigorous and consistent process of code review. This discipline often breaks down as projects grow, use cases become more complex and developers come and go.

Examples

// Classes in the presentation should not directly reference repositories
var result = Types.InCurrentDomain()
    .That(ResideInNamespace("NetArchTest.SampleLibrary.Presentation"))
    .Should(!HaveDependencyOn("NetArchTest.SampleLibrary.Data"))
    .GetResult();

// Classes in the "data" namespace should implement IRepository
result = Types.InCurrentDomain()
    .That(HaveDependencyOn("System.Data")
        & ResideInNamespace(("ArchTest")))
    .Should(ResideInNamespace("NetArchTest.SampleLibrary.Data"))
    .GetResult();

// All the service classes should be sealed
result = Types.InCurrentDomain()
    .That(ImplementInterface(typeof(IWidgetService)))
    .Should(BeSealed())
    .GetResult();

About

A fluent API for .Net that can enforce architectural rules in unit tests.

License:MIT License


Languages

Language:C# 100.0%