How do I stop my FSUnit tests using F# full syntax

204 Views Asked by At

We are using the wonderful FSUnit for our unit testing. This works fine, except the bodies of our tests insist on using full F# syntax (with 'in' at the end of each line etc.) instead of #light syntax. For example:

module MyTests

open System
open NUnit.Framework
open FsUnit
open MyModule

[<TestFixture>] 
type ``Given a valid file`` () =

    let myFile = createSomeFile()

    [<Test>] member x.
     ``Processing the file succeeds`` () =
        let actual = processFile myFile in
        actual |> should be True

Note the 'in' at the end of the first line of the test. Without that, the test won't compile - which is fine for short tests but is becoming a pain for longer test methods. We've tried adding an explicit #light in the source but that seems to make no difference. This is part of a large project with many modules, all of which - other than the test modules - are happily using light syntax (without any explicit #light). What's triggering full syntax in the test modules?

1

There are 1 best solutions below

1
On BEST ANSWER

You need to use a bit different indentation when writing members of the class. The following should be fine:

[<TestFixture>]  
type ``Given a valid file`` () = 
    let myFile = createSomeFile() 

    [<Test>] 
    member x.``Processing the file succeeds`` () = 
        let actual = processFile myFile
        actual |> should be True 

The first problem is that the name of the member should be indented further than . and the second problem is that the body of the member should be indented further than the member keyword - in your version, the keyword is written after [<Test>] so it would work if you indented the body further.

Adding in solves the problem, because that's telling the compiler more explicitly how to interpret the code (and so it does not rely on indentation rules).

Aside - with some unit testing frameworks, it is also possible to use module which gives you a bit lighter syntax (but I'm not sure how that works if you need some initialization - i.e. to load a file):

[<TestFixture>]  
module ``Given a valid file`` = 
    let myFile = createSomeFile() 

    [<Test>] 
    let ``Processing the file succeeds`` () = 
        let actual = processFile myFile
        actual |> should be True