Reputation: 185
I am making tests for three functions that make same thing using different ways (with NUnit and FsUnit). I want to use TestCase so I would not have to copy/paste a lot of code. I have this as compilation error message: "This is not a valid constant expression or custom attribute value". Is there any ways to solve the problem?
Program.fs
let calculateEvenNumbersWithFilter = ...
let calculateEvenNumbersWithMap = ...
let calculateEvenNumbersWithFold = ...
Tests.fs
open FsUnit
open NUnit.Framework
open Program
[<TestCase(calculateEvenNumbersWithFilter)>]
[<TestCase(calculateEvenNumbersWithFold)>]
[<TestCase(calculateEvenNumbersWithMap)>]
let ``Smoke test`` (func) =
func [1;2;3;4] |> should equal 2
Upvotes: 0
Views: 46
Reputation: 13726
This is a limitation of .NET. Only constants and certain restricted data types may be used as an argument to any attribute. It's not specific to NUnit.
Of course, in using attributes this way, we made ourselves subject to this limitation, but that's an issue for the next framework we design.
What you are trying to do is very elegant and (of course) functional. NUnit normally expects data arguments rather than functional arguments to test cases. However, this would work if you could get the argument passed.
The way to do that would be through use of the TestCaseSource
attribute. Give it the name of a static array containing your three functions and it ought to (eventually) work.
I regret that I can't translate the answer into working F# code for you, but since you've gotten this far I bet you can figure it out. :-)
Upvotes: 0