Is there any library like arch unit for Django?

Question:

I’ve been searching for a library or tool to test my Django project architecture, check the dependencies, layers, etc. like Arch Unit for Java. But until now, I didn’t find anything. I don’t even know if it’s viable doing these kinds of tests in Python/Django projects. I know that Django itself already checks for cyclic dependencies.

Asked By: Yuri Costa

||

Answers:

Check out what you can find under Python complexity metrics.

A tool called Wily may be of use. However, what counts as good practices will be very different for Java and Python.

Answered By: JL Peyret

The closest that I know of is https://github.com/seddonym/import-linter

It’s capable only of linting – as the name suggests – imports between modules so it has far fewer capabilities than ArchUnit, but I used it with great success in several clean/hexagonal architecture projects.

To use it to the full extent you need to wisely split your projects into modules so that the import statements "follow" (or rather "match") your designated architecture.

Some example of the rules that I often use:

[importlinter:contract:8]
name = Modules .core cannot depend on shared infrastructure
type = forbidden
source_modules =
  src.services.service1.modules.module1.core
  src.services.service2.modules.module1.core
  src.services.service2.modules.module2.core

forbidden_modules =
  src.shared.infrastructure


[importlinter:contract:3]
name = modules inside services shall be independent
type = independence
modules = 
  src.services.service1.modules.module1
  src.services.service2.modules.module1
  src.services.service2.modules.module2
Answered By: Antash

There is also pytest-archon. Its syntax is simpler than import-linter’s and uses pytest instead of separate config files.

Answered By: contramao