Several asserts in a single test

Should I write more than one assert in a single test? Most of the time the answer is NO. Each test is expressing a single behavior in the Subject Under Test and usually a single assert is enough to state that. However, sometimes we need more than one assert to complete the verification step. So how do I know when are several asserts allowed?

Ask this questions:
Can I write all these asserts before writing a single line of production code (SUT) so that the minimum implementation satisfies all of them?

The minimum implementation that makes the test pass is really considering a single behavior, or you have to think of more than one flow because of the asserts?

If the answer is "YES" to the first question and "YES, it shows a single behavior" then you are allowed to write more than one assert in the same test.

Sometimes methods that returns lists of elements are good candidates for more than one assert. But not always!

Some people write one assert, then make the test pass and add another assert to the same test. In my opinion the second assert belongs in its own test.

  • kevin

    This really answered my problem, thanks!

  • gutter cleaner

    good blog thank you Be mindful and try to keep in mind that Only health is wealth and like the excellent advice inside you weblog and suggest then other pregnancy women and show keep your pregnancy gallery pictures..