DEV Community

Sahil
Sahil

Posted on

what is an example of too much test cases?

Sometimes I wonder that I am writing too many test cases which test the exact same thing. Assuming there is a class

class KlassA
  def do_something
    b = KlassB.new
    b.do_something_more
  end

  def do_something_else
    c = KlassC.new
    c.do_some_other_thing
  end
end
Enter fullscreen mode Exit fullscreen mode

Now, if I have written test cases for KlassB, do_something_more and KlassC, do_some_other_thing. Should I write test cases for KlassA, do_something and KlassA, do_something_else ? Should I even write test for KlassA at all.

Why or Why not ?

Top comments (3)

Collapse
 
reefloretto profile image
Reef Loretto

It depends. Given only the context of the code you have written, it looks like KlassA is responsible for receiving certain inputs and then delegating to methods exposed by either KlassB or KlassC (#do_something_more or #do_some_other_thing, in this case). I might actually not test KlassA at all, and instead write an integration test that goes through KlassA and makes assertions about the final state of your application (what side-effects are triggered, are DB records created/updated, etc.)

Collapse
 
grsahil20 profile image
Sahil

Thanks for your input @reefloretto , I had a same feeling. But since I have never been heavy on writing test cases, I wanted to validate my thought over it.

Collapse
 
nflamel profile image
Fran C.

There's a very good talk from Sandi Metz which explains how to decide when is enough (regarding unit tests) youtube.com/watch?v=URSWYvyc42M

As for integration/E2E/whatever tests what I do is to take into account that tests have a cost and that I want to get the biggest benefit from them with the smaller cost possible.

If I can avoid adding a test, I do. Unless of course adding the test saves me money/time/whatever other costs.

In this case, my questions would be:

  • Do you need to have tests for KlassB and KlassC at all?
  • Are they only collaborators of KlassA or are they widely used around your app? Then test only KlassA unless you can find another very good reason to test B and C.
  • Is KlassA only delegating into those? Then I wouldn't test KlassA except for some integration.
  • Is KlassA doing some other query or does it have any other side effect? Then I would definitely test those as well.