Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is my code really not unit-testable?

Tags:

unit-testing

A lot of code in a current project is directly related to displaying things using a 3rd-party 3D rendering engine. As such, it's easy to say "this is a special case, you can't unit test it". But I wonder if this is a valid excuse... it's easy to think "I am special" but rarely actually the case.

Are there types of code which are genuinely not suited for unit-testing? By suitable, I mean "without it taking longer to figure out how to write the test than is worth the effort"... dealing with a ton of 3D math/rendering it could take a lot of work to prove the output of a function is correct compared with just looking at the rendered graphics.

like image 739
Mr. Boy Avatar asked Mar 25 '10 11:03

Mr. Boy


1 Answers

Code that directly relates to displaying information, generating images and even general UI stuff, is sometimes hard to unit-test.

However that mostly applies only to the very top level of that code. Usually 1-2 method calls below the "surface" is code that's easily unit tested.

For example, it may be nontrivial to test that some information is correctly animated into the dialog box when a validation fails. However, it's very easy to check if the validation would fail for any given input.

Make sure to structure your code in a way that the "non-testable" surface area is well-separated from the test and write extensive tests for the non-surface code.

like image 126
Joachim Sauer Avatar answered Oct 09 '22 12:10

Joachim Sauer