views:

359

answers:

4

How do you create unit tests in F#? I typically use the UnitTest portion of Visual Studio with a [TestClass] and [TestMethod] attributes and use the Test View to run these. I know I can just create a script file and run these, but I like the way that it is currently handled.

+6  A: 

Check out fscheck. It's a port of Haskell's Quickcheck. Fscheck allows you to specify properties a function must satisfy which it will then verify against a "large number of randomly generated cases".

It's something you can't easily do with an imperative language like C#.

ctford
Take a look at Pex: http://research.microsoft.com/en-us/projects/Pex/
Mauricio Scheffer
@Mauricio: I agree that it's possible to do what FsCheck does (randomly generating test cases) in an OO setting (another example: RANDOOP). Maybe one day we will have one specification language (which looks much nicer in a functional setting in my very biased opinion) that can generate both randomly generated cases and covering cases like Pex does.
Kurt Schelfthout
+2  A: 

Try XUnit.net

Asaph
Agreed. (only?) xUnit.NET can run test cases in static methods, so if you want to stay in the mainstream .NET unit test frameworks that is the one that takes the least effort to use.
Kurt Schelfthout
MbUnit can do this too
Mauricio Scheffer
+4  A: 

I'd rather use FsUnit or FsTest to write tests in F#, it feels more natural than OO xUnit style tests.

Mauricio Scheffer
+1  A: 

As far as I know, there is currently no integration between F# and Visual Studio unit testing (MSTest). People have only had success with running MSTest.exe manually on F# test projects. The integration in Visual Studio (where new tests show up in the Test View etc) does not work with F# test projects.

All the other test frameworks mentioned in the other answers should work fine.

Kurt Schelfthout