
Inside the Pyramid: Guiding Principles for a Faster Drupal Test Suite

November 25, 2026
Getting in Shape
If I did a good job so far, you are now supposed to be all fired up and looking forward to a bright future of pyramids and skyscrapers, which brings us to the how. It won’t be easy or effortless but, of course, I think it can be done, through the following steps:
- Define clear guidelines for both production and test code, updating AI best practices accordingly.
- Improve Unit testability across core, focusing especially on the entity system.
- Add new core test tools letting us implement lightweight Integration tests relying on
UnitTestCase. - Push down existing core test coverage towards the base as much as possible.
- Make core, contrib, and custom code adopt the guidelines at the same time: no need to wait for core to be done before starting.
Some of these steps can be worked on in parallel, but coming up with guidelines is the key one, so that is what I will focus on here.
The first step in coming up with valid guidelines is to identify the core principles behind them. Aside from what we already discussed, I am going to propose a few ideas, heavily inspired by, or taken verbatim from, a very influential source: Growing Object-Oriented Software, Guided by Tests (GOOS), the manifesto of the Mockist TDD school.
Listen to the Tests
As I mentioned in the first post , one of the main reasons why currently many Unit tests need to be implemented via KernelTestBase is that the Entity objects have so many intertwined dependencies that instantiating one forces you to instantiate a significant portion of the Entity and Field systems. Trying to use test doubles does not help either for the same reason. In fact, one of GOOS’ core principles says that if a test is hard to set up, that is design feedback: it is a signal that you should refactor the class to ensure its dependencies are more manageable and, hopefully, the resulting relationships more sensible.
After all, entities most often serve as data carriers between layers of a Drupal application and usually this kind of object is not supposed to get test doubles, since there is no interesting behavior to stub or mock, which brings us to the second principle.
Don’t Mock What You Don’t Own
This is also one of GOOS’ core principles: you are only supposed to mock interfaces you own, that is, belonging to code you can change. A mock is a specific type of test double encoding expectations about which method invocations it will receive, with the related returned values, if any.
Following the previous principle, if mocking an item in your code is hard, this should be treated as design feedback, implying, again, that you should refactor your code to make the relationships between its parts saner. Of course, that is not an option when you are dealing with code you do not own.
Moreover (and this is the deal breaker), a mock object encodes your assumptions about its behavior. This is fine for a class you own and whose behavior you control, but doing this with a class you do not own is dangerous, because its internal behavior may change without you noticing, which would cause your test to keep passing when in fact it should start failing.
Stub It Instead
A related case is when an interface you do not own is so narrow and self-documenting that there is basically no risk involved in stubbing it. For instance, the TimeInterface::getCurrentTime method is so straightforward that the risk of using a mocking API to stub it is close to zero: the contract is so well defined that returning a valid value is trivial and it is all we need to do to ensure the test will behave correctly.
Speak Your Own Language
Of course, from the perspective of a module author, the Entity Field API should be seen as framework code, similar to a third-party library, not something available for refactoring. Thus, attempting to create test doubles out of its interfaces is misguided, because they are not supposed to be stubbed (no interesting behavior there), let alone mocked.
From this, one could conclude that the only correct way to deal with entities within tests is to instantiate the real objects, which forces you to rely on KernelTestBase. However, my claim is that a module developer should not need to care about how hard it is to instantiate an entity: entities are generic domain-less data carriers, while a module is all about some specific domain logic, thus its code should be structured so that entities are hidden away as much as possible, treated as an implementation detail. This makes the module code more expressive and more easily Unit-testable at the same time.
Likewise, any other framework “jargon” should be banned from module code as much as possible: any reference to persistence, network connections, file system access, or a Drupal core subsystem should be treated as an implementation detail and hidden behind a thin adapter layer or confined to equally thin integration points, typically hook implementations and event listeners.
What about module code “extending” the framework, for instance by defining a formatter? Plugin classes are integration points too, so the same approach applies: if the module includes domain logic, extract it into classes speaking the module’s own language, and keep the plugin class as a thin adapter, covered by an Integration test. If there is no domain logic at all, the plugin is just wiring and an Integration test is all it needs. Things get trickier when the framework is the domain, as in the case of a new field type. There is not much left to hide, and here the rule of thumb is simple: the more a class’s code is about the framework’s own contract, the higher in the Pyramid its tests legitimately sit. In that case, the burden of Unit testability shifts from the module author to the framework, through test tooling – for instance, a fake implementation.
An Imperfect World
What about Deuteros? What is its place in this picture? The way I see it, Deuteros fulfills the need for an easy-to-instantiate data carrier object, same idea as the
TestArticleclass we are going to look at in the next post. In an ideal world, in the rare cases in which it is appropriate to instantiate an entity and not something domain-specific (it might be overkill sometimes), we should not be forced to give up onUnitTestCasetests. In the future we will hopefully be able to instantiate entity objects more easily; in the meantime, you can rely on Deuteros, which you can think of as a fake implementation of the Entity Field API’s parts defining a data-carrying system, backed by contract tests: those ensure that the behavior does not diverge from the original one, while giving you easy-to-instantiate objects. This is particularly useful in existing codebases, when refactoring is not available as a strategy, but you still would like to rely on fast Unit testing.
Widen the Base
As I touched on before, our goal should be to widen the Skyscraper’s base, which means leveraging UnitTestCase as much as possible, regardless of the scope of our tests. This translates into a two-question rule of thumb:
- Level: what is the scope of the test? Unit, Integration or End-To-End?
- Type: what is the lightest available tool to implement it?
The default choice would be a vanilla UnitTestCase, possibly paired with lightweight helper traits, for almost everything. Of course, we won’t always manage that, but we should only go to upper stories when absolutely required, because every story costs the Drupal Association money and puts more CO2 in the atmosphere. Not to mention our precious time.
Up Next
In the next post, I put these principles to work on a small newsroom module, showing how to hide entities from domain code so it can be Unit-tested.
Want a faster, leaner test suite for your Drupal project? Talk to our team.