2 people like it.

Using Theories with FSUnit, XUnit and NUnit

Here's how to use the [] and [] attributes to throw a set of data at a single test.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
open NUnit.Framework
open Xunit
open Xunit.Extensions
open FsUnit

let PresentValueOf amt time rate =
    amt * System.Math.Pow(1.0 + rate, -time)

[<TestFixture>]
type ``Given a future amount f at time t at rate i``() =

    [<Theory>]
    [<InlineData(0.0, 0.0, 0.0, 0.0)>]
    [<InlineData(0.0, 0.0, 0.1, 0.0)>]
    [<InlineData(0.0, 1.0, 0.0, 0.0)>]
    [<InlineData(1.0, 0.0, 0.0, 1.0)>]
    [<InlineData(1.0, 0.0, 0.1, 1.0)>]
    [<InlineData(1.1, 1.0, 0.1, 1.0)>]
    [<InlineData(1.2, 1.0, 0.2, 1.0)>]
    [<InlineData(1.21, 2.0, 0.1, 1.0)>]
    member x.``the present value is correct``(f, t, i, expected) =
        let actual = PresentValueOf f t i
        actual |> should (equalWithin 1.0e-9) expected 
namespace NUnit
namespace NUnit.Framework
namespace FsUnit
val PresentValueOf : amt:float -> time:float -> rate:float -> float

Full name: Script.PresentValueOf
val amt : float
val time : float
val rate : float
namespace System
type Math =
  static val PI : float
  static val E : float
  static member Abs : value:sbyte -> sbyte + 6 overloads
  static member Acos : d:float -> float
  static member Asin : d:float -> float
  static member Atan : d:float -> float
  static member Atan2 : y:float * x:float -> float
  static member BigMul : a:int * b:int -> int64
  static member Ceiling : d:decimal -> decimal + 1 overload
  static member Cos : d:float -> float
  ...

Full name: System.Math
System.Math.Pow(x: float, y: float) : float
Multiple items
type TestFixtureAttribute =
  inherit Attribute
  new : unit -> TestFixtureAttribute + 1 overload
  member Arguments : obj[]
  member Categories : IList
  member Category : string with get, set
  member Description : string with get, set
  member Ignore : bool with get, set
  member IgnoreReason : string with get, set
  member TypeArgs : Type[] with get, set

Full name: NUnit.Framework.TestFixtureAttribute

--------------------
TestFixtureAttribute() : unit
TestFixtureAttribute([<System.ParamArray>] arguments: obj []) : unit
Multiple items
type TheoryAttribute =
  inherit Attribute
  new : unit -> TheoryAttribute

Full name: NUnit.Framework.TheoryAttribute

--------------------
TheoryAttribute() : unit
val x : Given a future amount f at time t at rate i
member Given a future amount f at time t at rate i.( the present value is correct ) : f:float * t:float * i:float * expected:'a -> unit

Full name: Script.Given a future amount f at time t at rate i.( the present value is correct )
val f : float
val t : float
val i : float
val expected : 'a
val actual : float
val should : f:('a -> #Constraints.Constraint) -> x:'a -> y:obj -> unit

Full name: FsUnit.TopLevelOperators.should
val equalWithin : tolerance:'a -> x:'b -> Constraints.EqualConstraint

Full name: FsUnit.TopLevelOperators.equalWithin
Raw view New version

More information

Link:http://fssnip.net/dE
Posted:4 years ago
Author:Kit Eason
Tags: theories , unit testing , xunit , nunit , fsunit