Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support of shouldFail operator for xUnit, MsTest and MbUnit +… #64

Merged
merged 1 commit into from
Jul 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* FsUnitDepricated module renamed to FsUnitDeprecated https://github.com/fsprojects/FsUnit/pull/46
* Enable substring checks https://github.com/fsprojects/FsUnit/pull/45
* Fixed assertion message for NUnit equal constraint - https://github.com/fsprojects/FsUnit/pull/60
* Added support of `shouldFail` operator for xUnit, MsTest and MbUnit

### 1.3.1.0 - 26 July 2015
* Bump NUnit version up to 2.6.4
Expand Down
1 change: 0 additions & 1 deletion build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ Target "xUnit" (fun _ ->
|> Fake.Testing.XUnit2.xUnit2 (fun p ->
{p with
TimeOut = TimeSpan.FromMinutes 20.
ExcludeTraits = [("Category", "ShouldFail")]
HtmlOutputPath = Some "xunit.html"})
)

Expand Down
5 changes: 5 additions & 0 deletions docs/content/index.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ A function should throw a certain type of exception:
(fun () -> failwith "BOOM!" |> ignore) |> should throw typeof<System.Exception>
(fun () -> failwith "BOOM!" |> ignore) |> should (throwWithMessage "BOOM!") typeof<System.Exception>

(**
A function should fail
*)
shouldFail (fun () -> 5/0 |> ignore)

(**
A number of assertions can be created using the `be` keyword:
*)
Expand Down
2 changes: 1 addition & 1 deletion docs/content/operators.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Operators comparison across frameworks
| `greaterThanOrEqualTo` | + | + | + | + |
| `lessThan` | + | + | + | + |
| `lessThanOrEqualTo` | + | + | + | + |
| `shouldFail` | + | | | |
| `shouldFail` | + | + | + | + |
| `endWith` | + | + | + | + |
| `startWith` | + | + | + | + |
| `haveSubstring` | + | + | + | + |
Expand Down
10 changes: 10 additions & 0 deletions src/FsUnit.MbUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ let inline should (f : 'a -> ^b) x (y : obj) =
| _ -> y
Assert.That(y, c)

let inline shouldFail (f:unit->unit) =
let failed =
try
f()
false
with
| _ -> true
Assert.That(failed, Is.True(), "Method should fail")


let equal expected = CustomMatchers.equal expected

let equalWithin (tolerance:obj) (expected:obj) = CustomMatchers.equalWithin tolerance expected
Expand Down
11 changes: 11 additions & 0 deletions src/FsUnit.MsTestUnit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ let inline should (f : 'a -> ^b) x (y : obj) =
| _ -> y
Assert.That(y, c)

let inline shouldFail (f:unit->unit) =
let failed =
try
f()
false
with
| _ -> true
if not failed then
raise (new AssertFailedException("Method should fail"))


let equal expected = CustomMatchers.equal expected

let equalWithin (tolerance:obj) (expected:obj) = CustomMatchers.equalWithin tolerance expected
Expand Down
11 changes: 11 additions & 0 deletions src/FsUnit.Xunit/FsUnit.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ let inline should (f : 'a -> ^b) x (y : obj) =
| _ -> y
Assert.That(y, c)

let inline shouldFail (f:unit->unit) =
let failed =
try
f()
false
with
| _ -> true
if not failed then
raise (new MatchException("Method should fail", "No exception raised", null))


let equal expected = CustomMatchers.equal expected

let equalWithin (tolerance:obj) (expected:obj) = CustomMatchers.equalWithin tolerance expected
Expand Down
1 change: 1 addition & 0 deletions tests/FsUnit.MbUnit.Test/FsUnit.MbUnit.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="haveLengthTests.fs" />
<Compile Include="matchListTests.fs" />
<Compile Include="raiseTests.fs" />
<Compile Include="shouldFailTests.fs" />
<Compile Include="shouldEndWithTests.fs" />
<Compile Include="shouldStartWithTests.fs" />
<Compile Include="shouldHaveSubstringTests.fs" />
Expand Down
9 changes: 6 additions & 3 deletions tests/FsUnit.MbUnit.Test/raiseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@ type ``raise tests`` ()=

[<Test>] member test.
``should fail when exception of expected type with unexpected message is thrown`` ()=
(fun () -> raise (ApplicationException "BOOM!") |> ignore) |> should (throwWithMessage "CRASH!") typeof<ApplicationException>
shouldFail (fun () ->
(fun () -> raise (ApplicationException "BOOM!") |> ignore) |> should (throwWithMessage "CRASH!") typeof<ApplicationException>)

[<Test>] member test.
``should fail when exception of unexpected type with expected message is thrown`` ()=
shouldFail (fun () ->
let msg = "BOOM!" in
(fun () -> raise (ApplicationException msg) |> ignore) |> should (throwWithMessage msg) typeof<ArgumentException>
(fun () -> raise (ApplicationException msg) |> ignore) |> should (throwWithMessage msg) typeof<ArgumentException>)

[<Test>] member test.
``should fail when negated and exception of expected type with expected message is thrown`` ()=
shouldFail (fun () ->
let msg = "BOOM!" in
(fun () -> raise (ApplicationException msg) |> ignore) |> should not' ((throwWithMessage msg) typeof<ApplicationException>)
(fun () -> raise (ApplicationException msg) |> ignore) |> should not' ((throwWithMessage msg) typeof<ApplicationException>))

[<Test>] member test.
``should pass when negated and exception of expected type with unexpected message is thrown`` ()=
Expand Down
29 changes: 29 additions & 0 deletions tests/FsUnit.MbUnit.Test/shouldFailTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace FsUnit.Test
open System
open MbUnit.Framework
open FsUnit.MbUnit
open NHamcrest.Core

[<TestFixture>]
type ``shouldFail tests`` ()=
[<Test>] member test.
``empty List should fail to contain item`` ()=
shouldFail (fun () -> [] |> should contain 1)

[<Test>] member test.
``non-null should fail to be Null`` ()=
shouldFail (fun () -> "something" |> should be Null)

[<Test>] member test.
``shouldFail should fail when everything is OK`` ()=
shouldFail (fun () -> shouldFail id)

[<Test>] member test.
``shouldFaild should throw an exception`` ()=
(fun () -> shouldFail id)
|> should throw typeof<Exception> // ???

[<Test>] member test.
``shouldFaild should not throw an exception when fail`` ()=
(fun () -> shouldFail (fun () -> [] |> should contain 1))
|> should not' (throw typeof<Exception>) // ???
1 change: 1 addition & 0 deletions tests/FsUnit.MsTest.Test/Fs30Unit.MsTest.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="haveLengthTests.fs" />
<Compile Include="matchListTests.fs" />
<Compile Include="raiseTests.fs" />
<Compile Include="shouldFailTests.fs" />
<Compile Include="shouldEndWithTests.fs" />
<Compile Include="shouldStartWithTests.fs" />
<Compile Include="shouldHaveSubstringTests.fs" />
Expand Down
15 changes: 9 additions & 6 deletions tests/FsUnit.MsTest.Test/raiseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ type ``raise tests`` ()=
let msg = "BOOM!" in
(fun () -> raise (ApplicationException msg) |> ignore) |> should (throwWithMessage msg) typeof<ApplicationException>

[<TestMethod; Ignore>] member test.
[<TestMethod>] member test.
``should fail when exception of expected type with unexpected message is thrown`` ()=
(fun () -> raise (ApplicationException "BOOM!") |> ignore) |> should (throwWithMessage "CRASH!") typeof<ApplicationException>
shouldFail (fun () ->
(fun () -> raise (ApplicationException "BOOM!") |> ignore) |> should (throwWithMessage "CRASH!") typeof<ApplicationException>)

[<TestMethod; Ignore>] member test.
[<TestMethod>] member test.
``should fail when exception of unexpected type with expected message is thrown`` ()=
shouldFail (fun () ->
let msg = "BOOM!" in
(fun () -> raise (ApplicationException msg) |> ignore) |> should (throwWithMessage msg) typeof<ArgumentException>
(fun () -> raise (ApplicationException msg) |> ignore) |> should (throwWithMessage msg) typeof<ArgumentException>)

[<TestMethod; Ignore>] member test.
[<TestMethod>] member test.
``should fail when negated and exception of expected type with expected message is thrown`` ()=
shouldFail (fun () ->
let msg = "BOOM!" in
(fun () -> raise (ApplicationException msg) |> ignore) |> should not' ((throwWithMessage msg) typeof<ApplicationException>)
(fun () -> raise (ApplicationException msg) |> ignore) |> should not' ((throwWithMessage msg) typeof<ApplicationException>))

[<TestMethod>] member test.
``should pass when negated and exception of expected type with unexpected message is thrown`` ()=
Expand Down
28 changes: 28 additions & 0 deletions tests/FsUnit.MsTest.Test/shouldFailTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace FsUnit.Test
open Microsoft.VisualStudio.TestTools.UnitTesting
open FsUnit.MsTest
open NHamcrest.Core

[<TestClass>]
type ``shouldFail tests`` ()=
[<TestMethod>] member test.
``empty List should fail to contain item`` ()=
shouldFail (fun () -> [] |> should contain 1)

[<TestMethod>] member test.
``non-null should fail to be Null`` ()=
shouldFail (fun () -> "something" |> should be Null)

[<TestMethod>] member test.
``shouldFail should fail when everything is OK`` ()=
shouldFail (fun () -> shouldFail id)

[<TestMethod>] member test.
``shouldFaild should throw an exception`` ()=
(fun () -> shouldFail id)
|> should throw typeof<AssertFailedException>

[<TestMethod>] member test.
``shouldFaild should not throw an exception when fail`` ()=
(fun () -> shouldFail (fun () -> [] |> should contain 1))
|> should not' (throw typeof<AssertFailedException>)
14 changes: 14 additions & 0 deletions tests/FsUnit.NUnit.Test/shouldFailTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,17 @@ type ``shouldFail tests`` ()=
[<Test>] member test.
``non-null should fail to be Null`` ()=
shouldFail (fun () -> "something" |> should be Null)

[<Test>] member test.
``shouldFail should fail when everything is OK`` ()=
shouldFail (fun () -> shouldFail id)

[<Test>] member test.
``shouldFaild should throw an exception`` ()=
(fun () -> shouldFail id)
|> should throw typeof<NUnit.Framework.AssertionException>

[<Test>] member test.
``shouldFaild should not throw an exception when fail`` ()=
(fun () -> shouldFail (fun () -> [] |> should contain 1))
|> should not' (throw typeof<NUnit.Framework.AssertionException>)
1 change: 1 addition & 0 deletions tests/FsUnit.Xunit.Test/FsUnit.Xunit.Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="haveLengthTests.fs" />
<Compile Include="matchListTests.fs" />
<Compile Include="raiseTests.fs" />
<Compile Include="shouldFailTests.fs" />
<Compile Include="shouldEndWithTests.fs" />
<Compile Include="shouldStartWithTests.fs" />
<Compile Include="shouldHaveSubstringTests.fs" />
Expand Down
15 changes: 9 additions & 6 deletions tests/FsUnit.Xunit.Test/raiseTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,22 @@ type ``raise tests`` ()=
let msg = "BOOM!" in
(fun () -> raise (ApplicationException msg) |> ignore) |> should (throwWithMessage msg) typeof<ApplicationException>

[<Fact; Trait("Category", "ShouldFail")>] member test.
[<Fact>] member test.
``should fail when exception of expected type with unexpected message is thrown`` ()=
(fun () -> raise (ApplicationException "BOOM!") |> ignore) |> should (throwWithMessage "CRASH!") typeof<ApplicationException>
shouldFail (fun () ->
(fun () -> raise (ApplicationException "BOOM!") |> ignore) |> should (throwWithMessage "CRASH!") typeof<ApplicationException>)

[<Fact; Trait("Category", "ShouldFail")>] member test.
[<Fact>] member test.
``should fail when exception of unexpected type with expected message is thrown`` ()=
shouldFail (fun () ->
let msg = "BOOM!" in
(fun () -> raise (ApplicationException msg) |> ignore) |> should (throwWithMessage msg) typeof<ArgumentException>
(fun () -> raise (ApplicationException msg) |> ignore) |> should (throwWithMessage msg) typeof<ArgumentException>)

[<Fact; Trait("Category", "ShouldFail")>] member test.
[<Fact>] member test.
``should fail when negated and exception of expected type with expected message is thrown`` ()=
shouldFail (fun () ->
let msg = "BOOM!" in
(fun () -> raise (ApplicationException msg) |> ignore) |> should not' ((throwWithMessage msg) typeof<ApplicationException>)
(fun () -> raise (ApplicationException msg) |> ignore) |> should not' ((throwWithMessage msg) typeof<ApplicationException>))

[<Fact>] member test.
``should pass when negated and exception of expected type with unexpected message is thrown`` ()=
Expand Down
26 changes: 26 additions & 0 deletions tests/FsUnit.Xunit.Test/shouldFailTests.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace FsUnit.Test
open Xunit
open FsUnit.Xunit

type ``shouldFail tests`` ()=
[<Fact>] member test.
``empty List should fail to contain item`` ()=
shouldFail (fun () -> [] |> should contain 1)

[<Fact>] member test.
``non-null should fail to be Null`` ()=
shouldFail (fun () -> "something" |> should be Null)

[<Fact>] member test.
``shouldFail should fail when everything is OK`` ()=
shouldFail (fun () -> shouldFail id)

[<Fact>] member test.
``shouldFaild should throw an exception`` ()=
(fun () -> shouldFail id)
|> should throw typeof<MatchException>

[<Fact>] member test.
``shouldFaild should not throw an exception when fail`` ()=
(fun () -> shouldFail (fun () -> [] |> should contain 1))
|> should not' (throw typeof<MatchException>)