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.
I'd rather use FsUnit or FsTest to write tests in F#, it feels more natural than OO xUnit style tests.
EDIT 2014: I now consider FsUnit/FsTest to be mostly useless syntax sugar. And "more natural than OO" doesn't mean absolutely anything. A few months ago I wrote my current thoughts on testing here (I recommend reading the entire thread).
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#.
I'd rather use FsUnit or FsTest to write tests in F#, it feels more natural than OO xUnit style tests.
EDIT 2014: I now consider FsUnit/FsTest to be mostly useless syntax sugar. And "more natural than OO" doesn't mean absolutely anything. A few months ago I wrote my current thoughts on testing here (I recommend reading the entire thread).
In VS2013 you can use the below.
open Microsoft.VisualStudio.TestTools.UnitTesting
[<TestClass>]
type testrun() =
[<TestInitialize>]
member x.setup() =
//your setup code
[<TestMethod>]
member x.yourTestName() =
//your test code
Hint: If you are looking for UI unit testing then you can use this setup with Canopy.
I use a combination of xUnit.net, TestDriven.Net (Visual Studio Add-in for running tests, free for "students, open source developers and trial users"), and my own open source Unquote library (which also works with NUnit and any other exception-based assertion framework). This has worked out great and getting started is really easy:
- Download and install TestDriven.Net
- Download xUnit.net, unzip to any location and run xunit.installer.exe to integrate with TestDriven.Net
- Download Unquote, unzip to any location
- Create a project within your solution for unit tests
- Add references to xunit.dll and Unquote.dll (from unzipped downloads) in your unit test project
The following is a simple example of a .fs file in the unit test project containing xUnit.net / Unquote style unit tests.
module Tests open Swensen.Unquote open Xunit [<Fact>] let ``description of first unit test`` () = test <@ (11 + 3) / 2 = String.length ("hello world".Substring(4, 5)) @> [<Fact>] let ``description of second unit test`` () = let x = List.rev [1;2;3;4] x =? [4;3;1;2]
Run all the unit tests in the project by right-clicking the project in the solution explorer and selecting Run Test(s). Both of the previous example tests will fail with the following printed to the Visual Studio Output window:
------ Test started: Assembly: Tests.dll ------ Test 'Tests.description of second unit test' failed: [4; 3; 2; 1] = [4; 3; 1; 2] false C:\Solution\Project\Tests.fs(12,0): at Tests.description of second unit test() Test 'Tests.description of first unit test' failed: (11 + 3) / 2 = String.length ("hello world".Substring(4, 5)) 14 / 2 = String.length "o wor" 7 = 5 false C:\Solution\Project\Tests.fs(7,0): at Tests.description of first unit test() 0 passed, 2 failed, 0 skipped, took 1.09 seconds (xUnit.net 1.7.0 build 1540).
You might want to try NaturalSpec. It's a F# UnitTest-Framework on top of NUnit.
Try XUnit.net
As of version 2.5, NUnit allows you to use static members as tests. Also, the class-level TestFixtureAttribute is only necessary for generic or classes with non-default constructors. NUnit also has a backward-compatible convention that a test member may start with the word "test" instead of using TestAttribute, so you can almost write idiomatic F# with NUnit > 2.5.
Update You can see some test examples without the TestFixtureAttribute in the Cashel library. I continued using the TestAttribute since it appears few test runners correctly pick up tests when it is not present, so that part of the NUnit post may be incorrect or at least misleading.
© 2022 - 2024 — McMap. All rights reserved.