How to handle unit tests in F#?
Asked Answered
H

7

15

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.

Headroom answered 23/9, 2009 at 22:3 Comment(2)
This recent article of mine described this and a lot more (BDD testing from F#).Howes
Here's a place to start: bit.ly/1JhEbA7Natalianatalie
V
10

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).

Vaughnvaught answered 23/9, 2009 at 22:55 Comment(0)
I
11

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#.

Itch answered 23/9, 2009 at 22:10 Comment(2)
@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.Bayadere
Now FsCheck has a C# interface so you can use the most important features of FsCheck in C# too.Vaughnvaught
V
10

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).

Vaughnvaught answered 23/9, 2009 at 22:55 Comment(0)
F
8

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.

Freya answered 3/7, 2014 at 23:27 Comment(2)
This should be the accepted answer. This uses built-in unit testing functionality. Additionally (and not obviously), this approach requires adding a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll.Amathiste
@alancnet it's only built-in if you're using Visual Studio.Farce
G
4

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:

  1. Download and install TestDriven.Net
  2. Download xUnit.net, unzip to any location and run xunit.installer.exe to integrate with TestDriven.Net
  3. Download Unquote, unzip to any location
  4. Create a project within your solution for unit tests
  5. Add references to xunit.dll and Unquote.dll (from unzipped downloads) in your unit test project
  6. 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]
    
  7. 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).
    
Gaylor answered 9/2, 2011 at 20:49 Comment(0)
P
3

You might want to try NaturalSpec. It's a F# UnitTest-Framework on top of NUnit.

Parhe answered 28/1, 2011 at 11:31 Comment(0)
R
2

Try XUnit.net

Rosenberger answered 23/9, 2009 at 22:11 Comment(1)
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.Bayadere
D
2

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.

Dermatology answered 27/11, 2010 at 21:14 Comment(2)
Ryan, would you like to elaborate with an example test in F#? Or a link to a site that shows F# unit tests with no TestAttribute or TestFixtureAtrribute?Marabou
@David I added a link to my tests for Cashel.Dermatology

© 2022 - 2024 — McMap. All rights reserved.