Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create flexible unit tests?

Tags:

unit-testing

We are currently using unit tests to test our project. We have the majority of functionality covered but I think our tests are too brittle.

I was wondering if there are any specific things we can be doing to make the unit tests more flexible so they don't break for the wrong reasons.

A couple answers have mentioned being careful of mocking too much... So what are legitimate reasons for mocking? I think that may be one of our main problems, but when your application is mostly a dynamic, database-driven site, how do you get away from mocking?

like image 626
sheats Avatar asked Nov 07 '08 17:11

sheats


2 Answers

This is a somewhat simplistic answer, but shows the right mindset:

  • A test should break if the behaviour changes in a way that you care about.
  • A test should continue to work if the behaviour changes in a way that you don't care about.

So as far as is possible - without going hugely out of your way - make sure you're testing the "end result" of the method without caring how it got there. One thing to watch out for is mocking - it's incredibly useful, but can easily make your tests brittle.

like image 71
Jon Skeet Avatar answered Oct 21 '22 04:10

Jon Skeet


+1 to Jon. Well put.

I've found a lot of value in structuring my tests in a more BDD style. That is... reject the fixture-per-class mindset, instead go for fixture-per-context.

I also found that RhinoMocks 3.5's AAA syntax is much nicer.

Those cover organization and clean/readable tests.

To make my tests less brittle I've started to pull back on mocking. Mock frameworks are crucial for stubbing out dependencies, but the more you mock the more the test knows about the implementation. If the implementation changes (but behavior doesn't) then your tests shouldn't break.

like image 36
Ben Scheirman Avatar answered Oct 21 '22 03:10

Ben Scheirman