DEV Community

Oliver Davies
Oliver Davies

Posted on • Originally published at oliverdavies.uk on

Tests can assert multiple things

Similar to "a method should only have one return statement", I've seen similar advice when working with tests: "Tests should only have a single assertion".

I don't think this is true, and in my experience, you need multiple assertions to have a thorough test.

And, whilst similar assertions add some duplication, they can make the intent clearer and give better error messages.

Instead, I focus on one test case per test.

If I'm testing the following:

  • A blog page exists.
  • Only post nodes are visible.
  • Only published posts are visible,
  • Posts are returned in a specified order.

These will be split into separate tests - making it easier to read and maintain the code and have faster execution times by running only the tests I want with the minimum amount of code in each - regardless of how many assertions are in each.

P.S. Are you still using Drupal 7 and don’t know what’s involved to upgrade to Drupal 10? Book a Drupal 7 upgrade consultation call or an upgrade roadmap.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay