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?
This is a somewhat simplistic answer, but shows the right mindset:
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.
+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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With