i found many information bad practices use several asserts in 1 method (one test condition). it's clear. each method should test 1 , 1 condition. should 1 assert in cases.
but can't find information best practices situation, if one assert tests several conditions.
example:
@test public void findallperformers_returnsperformersinalphabeticalorder() { list<string> performers = reportrepository.findallperformers(); assertthat("should return performers in alphabetical order", performers, contains("bart simpson", "homer simpson", "ned flanders", "xena warrior princess")); }
first, had have 2 more test methods (conditions): findallperformers_returnsalluniqueperformers
, findallperformers_returnseachperformeronlyonce
. realized first method tests 2 conditions too!
so, should do?
- leave methods is.
- remove 2 other test methods , rename
findallperformers_returnsperformersinalphabeticalorder
method long name 3 conditions included (because tests 3 conditions!) - just remove 2 other test methods.
- leave methods invoke
findallperformers_returnsperformersinalphabeticalorder
method 2 other methods. - copy-paste
findallperformers_returnsperformersinalphabeticalorder
method contents 2 other methods. - anything else...
if test fails person looking @ failed test see findallperformers_returnsperformersinalphabeticalorder()
. looking @ alone appears method under test returned list wasn't in alphabetical order, not list didn't contain 'bart simpson'.
avoid options 4 , 5, don't solve , increase duplication/maintenance without added value (since tests fail reasons not mentioned in test name). instead try find way test want assert.