Unit Tests in F#

In the F# community, I have seen a lot of talk about how cool functional programming is, how to use continuations, monads, and so on. I haven’t seen much about using F# for unit testing how to unit test F# code. I’ve been particularly interested in unit testing since I’ve started a larger web project and once I had my MVC controller ready, I wanted to run the unit tests. Since I’m pushing myself to use F# more, I’m writing the unit tests in F#.

I just want to let you know that writing a unit test in F# is as easy as anywhere else. After including a reference to MbUnit in my test assembly, I wrote a simple test like so:

namespace somenamespace.TestSuite
open MbUnit.Framework 

type SampleTest() =
  member this.Setup() = 
    // Do setup work

  member this.Teardown() = 
    // Do tear down work

  member this.IAmATest() = 
    Assert.AreEqual(1, 1, "This was supposed to work")

For you C#/C++ folks, the empty parens () at the end of the functions just state that the member has a void return type. For the VB.NET folks, it is the same as a Sub.

And yes, all the other attributes you expect from MbUnit do work just fine in F#. When you add F# to your toolbox, you can leverage the time you spent learning unit testing frameworks. Hopefully, this makes the transition a little more appealing.

Posted 04-01-2010 8:07 AM by Scott Seely


About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET


SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
NHibernate Profiler
Balsamiq Mockups
JetBrains - ReSharper
Web Sequence Diagrams
Ducksboard<-- NEW Friend!


Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers


Community Server (Commercial Edition)