-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExpectingExceptions.fs
More file actions
97 lines (74 loc) · 3.31 KB
/
Copy pathExpectingExceptions.fs
File metadata and controls
97 lines (74 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
module ExpectingExceptions
module exceptionThrowingCode =
// A function that throws an exception
let f1 () = failwith "Oops!"
// A function that does not throw an exception
let f2 () = "OK"
open NUnit.Framework
// This example uses just NUnit framework for checking if exception gets thrown.
// It is equivalent to either have 2 attributes of a single with a list of them.
// Note that it is important to ignore any output of the functions if output
// is produced.
// For this, only NUnit.Framework is required.
[<TestFixture>]
type NUnitExample () =
[<Test; ExpectedException(typeof<System.Exception>)>]
member x.``test if exception is thrown when it actually is``() =
exceptionThrowingCode.f1 () |> ignore
[<Test>]
[<ExpectedException(typeof<System.Exception>)>]
member x.``test if exception is thrown when it actually is not``() =
exceptionThrowingCode.f2 () |> ignore
[<Test>]
member x.``test that f2 outputs "OK"``() =
Assert.AreEqual(exceptionThrowingCode.f2 (), "OK")
open FsUnit
// This example uses FsUnit apporach to detecting exceptions. Note that the code throwing exception
// is included in the body of a lambda function.
// Requires both NUnit.Framework and FsUnit
[<TestFixture>]
type FsUnitExample () =
[<Test>]
member x.``test if exception is thrown when it actually is``() =
(fun () -> exceptionThrowingCode.f1 () |> ignore) |> should throw typeof<System.Exception>
[<Test>]
member x.``test if exception is thrown when it actually is not``() =
(fun () -> exceptionThrowingCode.f2 () |> ignore) |> should throw typeof<System.Exception>
[<Test>]
member x.``test that f2 outputs "OK"``() =
exceptionThrowingCode.f2 () |> should equal "OK"
open FsCheck
open FsCheck.NUnit
// In FsCheck it is also possible to check for exceptions. There is a property building combinator
// for that in the Prop module.
[<TestFixture>]
type FsCheckExample () =
[<Property>]
member x.``test if exception is thrown when it actually is``() =
Prop.throws(lazy (exceptionThrowingCode.f1()|> ignore))
[<Property>]
member x.``test if exception is thrown when it actually is not``() =
Prop.throws(lazy (exceptionThrowingCode.f2()|> ignore))
// A simple property that checks output in the case of particular input (unit in this case)
[<Property>]
member x.``test that f2 outputs "OK"``() =
exceptionThrowingCode.f2 () = "OK"
// In case you have trouble getting the FsCheck examples to work,
// Uncomment the following code. It should appear in your project
// as FsCheckAddin.fs, but if it does not, uncomment the code.
// You may need to install NUnit Runtime package under Monodevelop
// to make NUnit.Core namespace accessible.
// Also, accessing FsCheck.NUnit and FsCheck.Nunit.Addin dll-s seems
// to require some manual fixing in references in MonoDevelop.
(*
open NUnit.Core.Extensibility
open FsCheck.NUnit
open FsCheck.NUnit.Addin
[<NUnitAddin(Description = "FsCheck addin")>]
type FsCheckAddin() =
interface IAddin with
override x.Install host =
let tcBuilder = new FsCheckTestCaseBuilder()
host.GetExtensionPoint("TestCaseBuilders").Install(tcBuilder)
true
*)