• Skip to primary navigation
  • Skip to main content

cultivate

  • Home
  • Services
  • Products
  • About us
  • Blog
  • Newsletter
  • Contact

Test Anti-pattern: Adding more than 3 expectations to a test case

Many developers hate having to write tests. Besides literally not knowing how to do it, the reason is often a test suite full of anti-patterns that is not fun to work with.

Here is one of the dozens of testing anti-patterns I identified over the years:

๐Ÿ›‘ Test Anti-pattern ๐Ÿ›‘

Adding more than 3 expectations to a test case


More than three expectations in a test case is a strong indicator that multiple test cases are mixed together in one test.

โŒ avoid:

it('returns false when password is invalid', () => {
  expect(isValidPassword('password')).toBe(false)
  expect(isValidPassword('UpCumQRA')).toBe(false)
  expect(isValidPassword('emn@pg*zyUMs')).toBe(false) // no number
  expect(isValidPassword('2ybgxuNeTm')).toBe(false) // no special char
})

All test cases deserve their own test with a proper name.

โœ… prefer:

it('rejects password without number', () => {
  expect(isValidPassword('P@ssword')).toBe(false)
})

it('rejects password without special character', () => {
  expect(isValidPassword('Passw0rd')).toBe(false)
})

it('rejects password without uppercase letter', () => {
  expect(isValidPassword('p@ssw0rd')).toBe(false)
})

it('rejects password without lowercase letter', () => {
  expect(isValidPassword('P@SSW0RD')).toBe(false)
})

Tests that mix multiple test cases in one test, like in the bad example above, often carry implicit knowledge, and it will be difficult and expensive to maintain these tests.

Notice also how the lousy code example above relies on comments to explain itself. Cleaner code can eliminate the need for comments in most cases. In the improved example, the tests take a double role. They verify the correctness of the code, and they also document the code.

Distinct test cases will make it also easier to find bugs when something goes wrong.

By the way, if you are working with Jest, there is also a linting rule that can remind you and your team to identify, refactor, and avoid tests with more than three expectations.

See you around,
– David

Test-first

Master the Art of Test-driven Development (TDD) and learn how the Top 1% of Developers create software.

Enroll now

Connect

  • Email
  • LinkedIn
  • Phone
  • YouTube

Legal

Imprint
Privacy Policy

Copyright © 2023 ยท cultivate GmbH

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. ACCEPTCookie settingsRead More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT