Architecture Test Framework For Go Project
ArchUnit is a simple and flexible extensible library for checking the architecture of Golang project. with it, you can make your project's architecture visible, testable and stable by setting a set of predefined architectural rules.
- Maintaining architectural integrity: Architecture tests help ensure that the intended architectural design and principles are followed throughout the development process. They help prevent architectural decay and ensure that the system remains consistent and maintainable over time.
- Detecting architectural violations: Architecture tests can identify violations of architectural rules and constraints. They help catch issues such as circular dependencies, improper layering, or violations of design patterns. By detecting these violations early, developers can address them before they become more difficult and costly to fix.
- Enforcing best practices: Architecture tests can enforce best practices and coding standards. They can check for adherence to coding conventions, naming conventions, and other guidelines specific to the architectural style or framework being used. This helps maintain a consistent codebase and improves code quality.
- Supporting refactoring and evolution: Architecture tests provide confidence when refactoring or making changes to the system. They act as a safety net, ensuring that the intended architectural structure is not compromised during the refactoring process. This allows developers to make changes with more confidence, knowing that they won't introduce unintended side effects.
- Facilitating collaboration: Architecture tests serve as a form of documentation that communicates the intended architectural design to the development team. They provide a shared understanding of the system's structure and help facilitate collaboration among team members. Developers can refer to the tests to understand the architectural decisions and constraints in place.
- This project implements the principles of Hexagonal architecture, which has been proven best practice of software architecture.You can easily apply rules with below aspects
- Fully tested and easy to use, it can be used with any other popular go test frameworks.
- NRTW(No Reinventing The Wheel). Keep using builtin golang toolchain at most.
- Import the library
go get github.com/kcmvp/archunit
- Write a simple test
func TestAllPackages(t *testing.T) {
pkgs := AllPackages().packages()
assert.Equal(t, 12, len(pkgs))
err := AllPackages().NameShouldBeSameAsFolder()
assert.NotNil(t, err)
}
It's better to keep all the architecture tests in one file
- PackageNameShouldBeSameAsFolderName
- PackageNameShouldBe
- SourceNameShouldBe
- MethodsOfTypeShouldBeDefinedInSameFile
- ConstantsShouldBeDefinedInOneFileByPackage
- ShouldNotReferLayers
- ShouldNotReferPackages
- ShouldOnlyReferLayers
- ShouldOnlyReferPackages
- ShouldBeOnlyReferredByLayers
- ShouldBeOnlyReferredByPackages
- DepthShouldLessThan