Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it okay if my unit tests contain 1-2 methods per class?

When I'm testing my simple geometric library, I usually have one or two methods in every test class. All I check is whether objects do proper coordinates calculation or not. Is it okay (meaning the number of methods)?

like image 367
chester89 Avatar asked Dec 09 '22 18:12

chester89


1 Answers

Don't get hung up on the number of methods. Just think about the ways in which your code could fail and add tests, preferably as separate methods, as you think of them.

The reason for separate methods is mostly to allow each test to be focused on just one aspect of your code and therefore keeping the test code short and simple to understand.

It's also acceptable to have a test method perform a number of Assertions, but once again these assertions should be checking a similar aspect of your code.

In some cases test code may need to be more complex eg: longer methods with calls to helper methods, but in general it is often possible to avoid this.

A good practical book that I've read on unit testing is Pragmatic Unit Testing in C# with nUnit. It provides handy guidance on many questions such as this.

like image 176
Ash Avatar answered Dec 29 '22 00:12

Ash