Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are brittle unit tests always a bad thing?

Tags:

unit-testing

At times I find a very brittle test to be a good thing because when I change the intent of the code under test I want to make sure my unit test breaks so that I'm forced to refactor ... is this approach not recommended when building a large suite of regression tests?

like image 490
Toran Billups Avatar asked Oct 14 '08 00:10

Toran Billups


3 Answers

Unit tests must be brittle -- it must be easy to break them. If they don't break, then they're not unit tests at all; they're code comments.

...

or am I missing the point of the question?


Edit: I should clarify my earlier answer.

I was being a bit pedantic about the language. "brittle" just means "easy to break". A unit test should be easy to break. The term "brittle tests" should really be "overly-brittle tests"; tests that break when they shouldn't. Even so, it's much, much easier to fix an over-brittle test than to fix a bug that slipped through an under-brittle test, so go ahead and write your brittle tests!

like image 182
Pitarou Avatar answered Nov 08 '22 10:11

Pitarou


The general statement admonishing brittle unit tests applies mostly to shops which haven't fully embraced unit testing. For instance, when trying to convert from having no tests to having a full suite of unit tests, or when your project is the unit testing pilot project. In these cases developers get used to false positives from unit tests and begin to ignore them. Then the unit tests fall behind the production code and either get left behind or require a major effort to update.

I would say you should always aim for the least brittle tests you can that fully test your function/module, but if you have 1 or 2 that are brittle you should be okay in most cases.

like image 33
Bryan Anderson Avatar answered Nov 08 '22 12:11

Bryan Anderson


IMO, as long as your tests make sure that your app code does what it should do, and if changed, the tests fail, then your tests are fine. Could you define what exactly you mean by "brittle"?

Just make sure that your tests really cover every aspect of your app code. (Within reason).

like image 6
Mark Avatar answered Nov 08 '22 12:11

Mark