One Assert per test method?

62 views
Skip to first unread message

Daniel Freire

unread,
Mar 13, 2011, 9:29:52 PM3/13/11
to test-driven...@googlegroups.com
Hi All, 

this is my first mail and I have an issue: Is correct make only one assert per test method?

best reguards

--
Daniel C. Freire
Analista Desenvolvedor
MCP, MCTS - Web Applications 2.0
(31) 8467-2331
@danielfreire



--
Daniel C. Freire
Analista Desenvolvedor
MCP, MCTS - Web Applications 2.0
(31) 8467-2331
@danielfreire

Mike Mahon

unread,
Mar 13, 2011, 11:34:10 PM3/13/11
to test-driven...@googlegroups.com

personally id make as many as need and fail w  a specific message

> --
> You received this message because you are subscribed to the Google Groups "Test Driven Development" group.
> To post to this group, send email to test-driven...@googlegroups.com.
> To unsubscribe from this group, send email to test-driven-devel...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/test-driven-development?hl=en.
>

Tim Gifford

unread,
Mar 14, 2011, 8:55:49 AM3/14/11
to test-driven...@googlegroups.com

Daniel,

I try to stay to 1 assert per test. I've found that if I have multiple asserts I'm testing more than one thing. If I'm testing more than one thing the test will break for multiple reasons and take longer to fix.

Thanks,
Tim Gifford
@timgifford

On Mar 13, 2011 10:11 PM, "Daniel Freire" <danie...@gmail.com> wrote:

danie...@gmail.com

unread,
Mar 14, 2011, 8:00:51 AM3/14/11
to test-driven...@googlegroups.com
Can you post some test method sample?

Maurício Aniche

unread,
May 27, 2015, 11:23:51 AM5/27/15
to test-driven...@googlegroups.com
Hi,

Sorry to reopen the thread. It is just I just joined the group! :)

I *do not* like the "one assert per test" rule. If you are using a OO language, you have an object that has many attributes. A behavior can change many of them. I want to see all the changes in a single test, making them all together. Breaking up into many tests will just increase the size of your unit test, and the feedback will be almost the same (JUnit fails in the right assert, all you have to do is to see it :)

My rule is to test one feature per test. If a method has many different branches, then I break all of them in different test methods. I've done a study in past, showing that, when you assert many different objects in the same test, it is probably because the production code is more complex than it should be. Paper [1] and slides [2], if you are interested.

[1] http://www.aniche.com.br/wp-content/uploads/2013/04/csmr2013-asserts.pdf
Maurício


Reply all
Reply to author
Forward
0 new messages