Special NUnit exception types (#38)

This commit is contained in:
Patrick Stevens
2024-06-06 21:23:47 +01:00
committed by GitHub
parent 00e2b027c7
commit 9a0eb1d8fc
6 changed files with 52 additions and 8 deletions

View File

@@ -9,6 +9,7 @@
<ItemGroup>
<Compile Include="NoAttribute.fs" />
<Compile Include="Inconclusive.fs" />
<Compile Include="TestSetUp.fs" />
<Compile Include="TestValues.fs" />
<None Include="some-config.json">

19
Consumer/Inconclusive.fs Normal file
View File

@@ -0,0 +1,19 @@
namespace Consumer
open NUnit.Framework
[<TestFixture>]
module Inconclusive =
[<Test>]
let ``Inconclusive test`` () =
Assert.Inconclusive "I was inconclusive"
[<Test>]
let ``Ignore test`` () = Assert.Ignore "I am ignored"
[<Test>]
let ``Pass test`` () = Assert.Pass "ohhhh yeaahhhh"
[<Test>]
let ``Warning`` () = Assert.Warn "warning"

View File

@@ -151,6 +151,8 @@ type TestMemberSuccess =
| Ignored of reason : string option
/// We didn't run the test, because it's [<Explicit>].
| Explicit of reason : string option
/// We ran the test, and it performed Assert.Inconclusive.
| Inconclusive of reason : string option
/// Represents the failure of a test.
[<RequireQualifiedAccess>]

View File

@@ -221,27 +221,34 @@ TestRunner.TestMemberFailure.IsMalformed [property]: [read-only] bool
TestRunner.TestMemberFailure.NewFailed [static method]: TestRunner.TestFailure list -> TestRunner.TestMemberFailure
TestRunner.TestMemberFailure.NewMalformed [static method]: string list -> TestRunner.TestMemberFailure
TestRunner.TestMemberFailure.Tag [property]: [read-only] int
TestRunner.TestMemberSuccess inherit obj, implements TestRunner.TestMemberSuccess System.IEquatable, System.Collections.IStructuralEquatable, TestRunner.TestMemberSuccess System.IComparable, System.IComparable, System.Collections.IStructuralComparable - union type with 3 cases
TestRunner.TestMemberSuccess inherit obj, implements TestRunner.TestMemberSuccess System.IEquatable, System.Collections.IStructuralEquatable, TestRunner.TestMemberSuccess System.IComparable, System.IComparable, System.Collections.IStructuralComparable - union type with 4 cases
TestRunner.TestMemberSuccess+Explicit inherit TestRunner.TestMemberSuccess
TestRunner.TestMemberSuccess+Explicit.get_reason [method]: unit -> string option
TestRunner.TestMemberSuccess+Explicit.reason [property]: [read-only] string option
TestRunner.TestMemberSuccess+Ignored inherit TestRunner.TestMemberSuccess
TestRunner.TestMemberSuccess+Ignored.get_reason [method]: unit -> string option
TestRunner.TestMemberSuccess+Ignored.reason [property]: [read-only] string option
TestRunner.TestMemberSuccess+Inconclusive inherit TestRunner.TestMemberSuccess
TestRunner.TestMemberSuccess+Inconclusive.get_reason [method]: unit -> string option
TestRunner.TestMemberSuccess+Inconclusive.reason [property]: [read-only] string option
TestRunner.TestMemberSuccess+Tags inherit obj
TestRunner.TestMemberSuccess+Tags.Explicit [static field]: int = 2
TestRunner.TestMemberSuccess+Tags.Ignored [static field]: int = 1
TestRunner.TestMemberSuccess+Tags.Inconclusive [static field]: int = 3
TestRunner.TestMemberSuccess+Tags.Ok [static field]: int = 0
TestRunner.TestMemberSuccess.get_IsExplicit [method]: unit -> bool
TestRunner.TestMemberSuccess.get_IsIgnored [method]: unit -> bool
TestRunner.TestMemberSuccess.get_IsInconclusive [method]: unit -> bool
TestRunner.TestMemberSuccess.get_IsOk [method]: unit -> bool
TestRunner.TestMemberSuccess.get_Ok [static method]: unit -> TestRunner.TestMemberSuccess
TestRunner.TestMemberSuccess.get_Tag [method]: unit -> int
TestRunner.TestMemberSuccess.IsExplicit [property]: [read-only] bool
TestRunner.TestMemberSuccess.IsIgnored [property]: [read-only] bool
TestRunner.TestMemberSuccess.IsInconclusive [property]: [read-only] bool
TestRunner.TestMemberSuccess.IsOk [property]: [read-only] bool
TestRunner.TestMemberSuccess.NewExplicit [static method]: string option -> TestRunner.TestMemberSuccess
TestRunner.TestMemberSuccess.NewIgnored [static method]: string option -> TestRunner.TestMemberSuccess
TestRunner.TestMemberSuccess.NewInconclusive [static method]: string option -> TestRunner.TestMemberSuccess
TestRunner.TestMemberSuccess.Ok [static property]: [read-only] TestRunner.TestMemberSuccess
TestRunner.TestMemberSuccess.Tag [property]: [read-only] int
TestRunner.TestProgress inherit obj

View File

@@ -31,7 +31,7 @@ module TestFixture =
(test : MethodInfo)
(containingObject : obj)
(args : obj[])
: Result<unit, TestFailure list>
: Result<TestMemberSuccess, TestFailure list>
=
let rec runMethods
(wrap : UserMethodFailure -> TestFailure)
@@ -59,15 +59,30 @@ module TestFixture =
| Error e -> Error [ e ]
| Ok () ->
let result =
let result = runMethods TestFailure.TestFailed [ test ] args
match result with
| Ok () -> Ok None
| Error (TestFailure.TestFailed (UserMethodFailure.Threw (_, exc)) as orig) ->
match exc.GetType().FullName with
| "NUnit.Framework.SuccessException" -> Ok None
| "NUnit.Framework.IgnoreException" -> Ok (Some (TestMemberSuccess.Ignored (Option.ofObj exc.Message)))
| "NUnit.Framework.InconclusiveException" ->
Ok (Some (TestMemberSuccess.Inconclusive (Option.ofObj exc.Message)))
| s when s.StartsWith ("NUnit.Framework.", StringComparison.Ordinal) ->
failwith $"Unrecognised special exception: %s{s}"
| _ -> Error orig
| Error orig -> Error orig
// Unconditionally run TearDown after tests, even if tests failed.
let tearDownResult = runMethods TestFailure.TearDownFailed tearDown [||]
match result, tearDownResult with
| Ok (), Ok () -> Ok ()
| Ok None, Ok () -> Ok TestMemberSuccess.Ok
| Ok (Some s), Ok () -> Ok s
| Error e, Ok ()
| Ok (), Error e -> Error [ e ]
| Ok _, Error e -> Error [ e ]
| Error e1, Error e2 -> Error [ e1 ; e2 ]
let private getValues (test : SingleTestMethod) =
@@ -140,11 +155,11 @@ module TestFixture =
| Ok values ->
let inline normaliseError
(e : Result<unit, TestFailure list>)
(e : Result<TestMemberSuccess, TestFailure list>)
: Result<TestMemberSuccess, TestMemberFailure>
=
match e with
| Ok () -> Ok TestMemberSuccess.Ok
| Ok s -> Ok s
| Error e -> Error (e |> TestMemberFailure.Failed)
match test.Kind, values with

View File

@@ -1,5 +1,5 @@
{
"version": "0.3",
"version": "0.4",
"publicReleaseRefSpec": null,
"pathFilters": [
"./",