How to test code you didn't write

I was also asked at DrupalCon how you write tests for code you didn't write and how you cover all of the use cases and scenarios.

This is tricky. If you didn't write the code, how can you know all the scenarios you need to test?

Presumably, there is some documentation, Jira tickets, user stories or acceptance criteria from when the code was written that will help you with this.

Decide the most critical functionality to be tested and focus on that, as that will provide the most value.

I'd start with functional tests and test for high-level outcomes, such as whether a page loads, returns the correct status code, and contains the expected text.

Then, iteratively add more tests for other scenarios. Once you have the first one written, this should be easier as you already have the setup steps, such as which modules must be enabled and what configuration needs to be installed.

One test is better than none.

If you need to add any new features or fixes, ensure they have their own tests to keep a consistent level of coverage.

Even with a simple module, you're unlikely to be able to identify all of the scenarios, write all the tests in one go, and have everything covered.

Start with the most important test first and continue to add more iteratively.

- Oliver

Was this interesting?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.