When making a test class with FsUnit, [<Test>] is not a valid attribute on a member. Why?

StackOverflow https://stackoverflow.com/questions/7697798

  •  07-02-2021
  •  | 
  •  

Question

I'm fiddling with a new F# project (of which I haven't made many), and I'm aiming to do it in a TDD fashion. So, I'm trying to familiarize myself with using FsUnit, since I have a lot of experience using NUnit in C# projects, and it seems a pretty common framework to use.

My code looks like the following:

module DatabaseReaderTest

open NUnit.Framework
open FsUnit

[<TestFixture>]
type DatabaseReaderTest ()=

    [<Test>]
    member x.ResultsAreReturnedFromDatabase =
        DatabaseReader.result.GetSqlString(1) |> should equal "first"

As far as I can tell, this follows the example on the FsUnit home page (http://fsunit.codeplex.com/), but the compiler tells me that [<Test>] is not a valid attribute on this language element, by which I assume it means member.

Any tips on what I'm doing wrong here?

Thanks!

Was it helpful?

Solution

You probably need to use a method rather than a property. Just add a () argument of type unit.

OTHER TIPS

As a side-note, if you don't need any custom initialization before tests are run and if you're using a recent version of NUnit, then you should be able to use module with let bound functions for your tests.

This gives you a bit more lightweight syntax. Some people also like to use double-tick syntax that makes it possible to write the test name as an ordinary English sentence (which shows nicely in test runner):

module DatabaseReaderTest =

  [<Test>]
  let ``Results are returned from database`` () =
    DatabaseReader.result.GetSqlString(1) |> should equal "first"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top