Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it advisable to write a test case for every class in your program?

Im am just getting introduced to unit testing and test driven development. Thus far, I have only used Junit as testing framework. A question which emerged and for which I have not yet found a definite answer is: how much test cases do I need to write? Do I have to write a test case for every single class in my program? Or is this a stupid question because unit testing implies testing at the lowest (i.e. class) level?

I think writing a test case for every class is probably the more safe way to go (after all the more you test the lower the number of unforeseen bugs). But I was wondering if there are any widely agreed upon strategies regarding the amount of test cases to write?

like image 569
phuibers Avatar asked Dec 02 '22 01:12

phuibers


1 Answers

If you're trying on TDD, then you shouldn't be writing any code at all without having a failing test that tells you to do so. By implication, then, you'll never have a class that doesn't have one or more tests for it. A rule of thumb that tends to get quoted is that you should end up with about 2.5 times as much test source as main source.

like image 123
Ryan Stewart Avatar answered May 19 '23 10:05

Ryan Stewart