Is there a way of unit testing what modules are imported in a Python file (a bit like ArchUnit in Java)? The context is in implementing a hexagonal architecture and wanting to ensure that the domain model does not import any code that resides in an adapter. I'd like unit tests to fail if there are forbidden imports.
For example, I might like to test that no modules within foo.bar.domain
import anything from foo.bar.adapter
. Imports of foo.bar.domain
should be allowed from within foo.bar.adapter
.
Is this possible in Python and what's the best way of achieving this?