Software Architecture In .NET Using Architecture Tests

Introduction

Architectural testing in C# involves evaluating the overall structure and design of a software application to ensure that it meets certain architectural principles and requirements. While static code analysis tools assist in verifying or promoting general best practices, NetArchTest can be utilized to generate unit tests that enforce architectural rules in your .NET and .NET Core applications. These rules encompass guidelines related to class design, naming conventions, and codebase dependencies.

.NetArchTest is a library that allows you to write and enforce architectural rules and constraints for your .NET applications. You can use it to ensure that your code adheres to certain architectural principles. To implement NetArchTest in a .NET Core project, follow these steps.

The source code can be downloaded from GitHub.

Create a .NET Core Project

First, create a .NET Core project with clean architecture to implement architectural testing. You can do this using Visual Studio or the .NET CLI.

Clean Architecture Project

Add '.NetArchTest' NuGet Package

Create a xUnit Project and install the NuGet package ‘NetArchTest.’ You can use the NuGet Package Manager in Visual Studio to search for and install the package.

xUnit Project

Create Architectural Tests

Once you have the netArchTest library added to your project, you can create architectural tests using C# code. Architectural tests are typically written as unit tests. Create a new unit test project or use an existing one to house your architectural tests.

namespace CleanArchitecture.Tests
{
    public class ArchitectureTests
    {
        [Fact]
        public void Domain_Should_Not_HaveDependencyOnOtherProjects()
        {
            //Arrange
           var assembly =  typeof(Domains.AssemblyReference).Assembly;
            //Act
            var result = Types.InAssembly(assembly)
                .Should()
                .NotHaveDependencyOn("Application")
                .And()
                .NotHaveDependencyOn("Presentation")
                .And()
                .NotHaveDependencyOn("Infrastructure")
                .And()
                .NotHaveDependencyOn("WebAPI")
                .GetResult();
            //Assert
            Assert.True(result.IsSuccessful);
        }
    }
}

Run Tests

architecture test result view

Review Results

After running the tests, review the test results. If any architectural rules are violated, the tests will fail, indicating that there are issues in your codebase that need to be addressed.

Customize and Define More Rules

You can define additional architectural rules and tests to enforce specific constraints or principles that are important for your application.

Integrate with CI/CD (Optional)

To ensure that architectural tests are consistently run, consider integrating them into your CI/CD pipeline. This way, you can catch architectural violations early in the development process.

That's it! You've now implemented architectural testing using. NetArchTest in your .NET Core project. You can define and customize rules according to your project's architectural requirements, helping to maintain code quality and adherence to architectural principles as your codebase evolves.