Thursday, September 20, 2007

Easy Testing in F#

I've found a personally acceptable answer to the debate about whether or not to test private members. This is a technique that you can use in F#.

Here are some of the issues that you might face in testing F# code:
Perhaps you see my quandary. I want my test code to be in a separate assembly from my production code, so the code-under-test needs to be exposed. The more CLS-friendly I make my assembly, the further away from the code I get. And finally, I want to test all of those little functions that I'll inevitably end up writing except I don't want to expose all of those little functions in my API.

My answer? It's simple, I just compile my SUT without including my interface (.fsi) file! When you do this, every value declared in your source (.fs) file is exposed to the world for testing. Then, when I compile for release, I include the interface file and all of that stuff is hidden.

Thus far, I've found no issues with NUnit working in this manner and it's given me the Best of Both Worlds as far as test coverage and CLS-friendly API's go!

No comments: