Use Architecture Tests in your projects

Aug 14 2023

 
 

Many thanks to the sponsors who make it possible for this newsletter to be free for readers.

 

Today's issue is sponsored by Packt.
Working with React doesn't have to be complex. "React 18 Design Patterns and Best Practices" empowers you to harness React's potential, making your applications flexible, easy to manage, and high-performing. Discover and unravel the dynamic features of React 18 and Node 19. This updated fourth edition equips you with insights into the cutting-edge tools that will elevate your projects. Book Link

 
 

Background

 
 

Software architecture provides a systematic framework for constructing your system. While it's possible to adhere strictly to this design or exercise flexibility, neglecting it under pressing deadlines can lead to your system collapsing.

 

Want to keep it steady?

 

Say hello to architecture tests!

 

They're like friendly watchdogs, making sure your code's on the right path. With them, you can be sure your design's on point and everything's connected just right.

 

So, how to write architecture tests?

 

Let me show you how to do it.

 
 

Writing Architecture Tests

 
 

Writing architectural tests is no different from writing Unit tests. It is necessary to create a new test project in your environment.

 

Also, you have to install the NuGet package:

Install-Package NetArchTest.Rules

 

Why NetArchTest.Rules?

 

NetArchTest.Rules is great because it already implements the boilerplate code we need to start writing tests.

 

So, how to write rules?

 

Let's assume we have a Clean Architecture project with the following rules:

 

Domain should not have any dependencies


var result = Types
    .InAssembly(DomainAssembly)
    .ShouldNot()
    .HaveDependencyOnAny("Application", "Infrastructure")
    .GetResult();

Assert.True(result.IsSuccessful);
Here's a step-by-step breakdown:

 

1. Types.InAssembly(DomainAssembly) : We're looking at all the types (or parts) inside the Domain.

 

2. Assembly.ShouldNot().HaveDependencyOnAny("Application", "Infrastructure"): This checks that none of these types have any connection or reliance on "Application" or "Infrastructure".

 

3. .GetResult();: Collects the results of the check into the result variable.

 

4. Assert.True(result.IsSuccessful);: This is like a final check. If everything went as planned and no unwanted connections were found, result.IsSuccessful would be true. If not, something's amiss.

 

Infrastructure should depend on Application and Domain


var result = Types
    .InAssembly(InfrastructureAssembly)
    .HaveNameEndingWith("Repository")
    .Should()
    .HaveDependencyOn("Domain")
    .GetResult();

Assert.True(result.IsSuccessful);

 

Here's a step-by-step breakdown:

 

1. All "Repository" classes in Infrastructure assembly should have dependency on Domain (because all of them are using entities).

 

2 . HaveNameEndingWith("Repository"): From those entities, we're filtering to only pick those whose names end with.

 

3. .Should().HaveDependencyOn("Domain"): For these filtered entities, we're ensuring that they have a connection or reliance on something named "Domain".

 
 

Wrapping up

 
 

Architecture tests are automated tests specifically designed to verify the structural design and integrity of software systems. Unlike traditional tests that focus on functionality or performance, architecture tests scrutinize how different components of software interact with each other, ensuring they adhere to predefined design patterns and rules.

 

Why Should You Use Them?

 

1. Maintain Design Integrity:

 

As software projects grow, maintaining a consistent design and structure becomes challenging. Architecture tests help ensure the system's design remains consistent as it evolves.

 

2. Early Issue Detection:

 

 

3. Improved Collaboration:

 

Clear architectural rules and tests make it easier for developers to understand the system's design. This understanding aids in smoother collaboration, especially in large teams or when onboarding new members.

 

4. Quality Assurance:

 

A sound architecture is a pillar of high-quality software. By enforcing design rules and validating structural decisions, architecture tests contribute to the overall quality and reliability of a software system.

 

In essence, architecture tests are a proactive measure to ensure the backbone of software remains strong and consistent, saving time and potential headaches in the long run.

 

That's all from me today.

Join 13,250+ subscribers to improve your .NET Knowledge.

There are 3 ways I can help you:

Design Patterns Simplified ebook

Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.


Sponsorship

Promote yourself to 13,250+ subscribers by sponsoring this newsletter.


Join TheCodeMan.net Newsletter

Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.


Subscribe to
TheCodeMan.net

Subscribe to the TheCodeMan.net and be among the 13,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.