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
[<TestFixture>]
type SampleTest() =
[<FixtureSetUp>]
member this.Setup() =
// Do setup work
()
[<FixtureTearDown>]
member this.Teardown() =
// Do tear down work
()
[<Test>]
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