Skip to content

Commit

Permalink
Allow tests to pass with a message
Browse files Browse the repository at this point in the history
  • Loading branch information
rynoV committed Dec 17, 2024
1 parent f21ca22 commit 8d765df
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Expecto.Tests.CSharp/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Task ITestPrinter.Failed(string value1, string value2, TimeSpan value3)
return Task.CompletedTask;
}

public Task PassedWithMessage(string value1, string value2, TimeSpan value3)
{
return Task.CompletedTask;
}

Task ITestPrinter.Ignored(string value1, string value2)
{
return Task.CompletedTask;
Expand Down
2 changes: 2 additions & 0 deletions Expecto/CSharp.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type ITestPrinter =
abstract member BeforeEach : string -> Task
abstract member Info : string -> Task
abstract member Passed : string * TimeSpan -> Task
abstract member PassedWithMessage : string * string * TimeSpan -> Task
abstract member Ignored : string * string -> Task
abstract member Failed : string * string * TimeSpan -> Task
abstract member Exn : string * exn * TimeSpan -> Task
Expand All @@ -30,6 +31,7 @@ module Runner =
{ beforeRun = fun t -> async { return! i.BeforeRun(t) |> Async.AwaitTask }
beforeEach = fun s -> async { return! i.BeforeEach(s) |> Async.AwaitTask }
passed = fun n d -> async { return! i.Passed(n, d) |> Async.AwaitTask }
passedWithMessage = fun n m d -> async { return! i.PassedWithMessage(n, m, d) |> Async.AwaitTask }
info = fun s -> async { return! i.Info(s) |> Async.AwaitTask }
ignored = fun n m -> async { return! i.Ignored(n, m) |> Async.AwaitTask }
failed = fun n m d -> async { return! i.Failed(n, m, d) |> Async.AwaitTask }
Expand Down
27 changes: 27 additions & 0 deletions Expecto/Expecto.Impl.fs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ module Impl =

type TestResult =
| Passed
| PassedWithMessage of string
| Ignored of string
| Failed of string
| Error of exn
override x.ToString() =
match x with
| Passed -> "Passed"
| PassedWithMessage m -> "Passed: " + m
| Ignored reason -> "Ignored: " + reason
| Failed error -> "Failed: " + error
| Error e -> "Exception: " + exnWithInnerMsg e ""
member x.tag =
match x with
| Passed -> 0
| PassedWithMessage _ -> 0
| Ignored _ -> 1
| Failed _ -> 2
| Error _ -> 3
Expand All @@ -57,11 +60,13 @@ module Impl =
match x with
| Ignored _ -> 0
| Passed -> 1
| PassedWithMessage _ -> 1
| Failed _ -> 2
| Error _ -> 3
member x.isPassed =
match x with
| Passed -> true
| PassedWithMessage _ -> true
| _ -> false
member x.isIgnored =
match x with
Expand Down Expand Up @@ -213,6 +218,8 @@ module Impl =
info: string -> Async<unit>
/// test name -> time taken -> unit
passed: string -> TimeSpan -> Async<unit>
/// test name -> message -> time taken -> unit
passedWithMessage: string -> string -> TimeSpan -> Async<unit>
/// test name -> ignore message -> unit
ignored: string -> string -> Async<unit>
/// test name -> other message -> time taken -> unit
Expand All @@ -226,6 +233,7 @@ module Impl =
let name = config.joinWith.format test.name
match result.result with
| Passed -> config.printer.passed name result.duration
| PassedWithMessage message -> config.printer.passedWithMessage name message result.duration
| Failed message -> config.printer.failed name message result.duration
| Ignored message -> config.printer.ignored name message
| Error e -> config.printer.exn name e result.duration
Expand All @@ -235,6 +243,7 @@ module Impl =
beforeEach = fun _ -> async.Zero()
info = fun _ -> async.Zero()
passed = fun _ _ -> async.Zero()
passedWithMessage = fun _ _ _ -> async.Zero()
ignored = fun _ _ -> async.Zero()
failed = fun _ _ _ -> async.Zero()
exn = fun _ _ _ -> async.Zero()
Expand All @@ -258,6 +267,13 @@ module Impl =
>> setField "testName" n
>> setField "duration" d)

passedWithMessage = fun n m d ->
logger.logWithAck Debug (
eventX "{testName} passed in {duration}. {message}"
>> setField "testName" n
>> setField "message" m
>> setField "duration" d)

ignored = fun n m ->
logger.logWithAck Debug (
eventX "{testName} was ignored. {reason}"
Expand Down Expand Up @@ -417,6 +433,13 @@ module Impl =
"name", formatName n
"duration", d.TotalMilliseconds |> int |> string ] }

passedWithMessage = fun n m d -> async {
do! innerPrinter.passedWithMessage n m d
tcLog "testFinished" [
"flowId", formatName n
"name", formatName n
"duration", d.TotalMilliseconds |> int |> string ] }

info = fun s ->
innerPrinter.info s

Expand Down Expand Up @@ -463,6 +486,7 @@ module Impl =
beforeEach = fun n -> runTwoAsyncs (first.beforeEach n) (second.beforeEach n)
info = fun s -> runTwoAsyncs (first.info s) (second.info s)
passed = fun n d -> runTwoAsyncs (first.passed n d) (second.passed n d)
passedWithMessage = fun n m d -> runTwoAsyncs (first.passedWithMessage n m d) (second.passedWithMessage n m d)
ignored = fun n m -> runTwoAsyncs (first.ignored n m) (second.ignored n m)
failed = fun n m d -> runTwoAsyncs (first.failed n m d) (second.failed n m d)
exn = fun n e d -> runTwoAsyncs (first.exn n e d) (second.exn n e d)
Expand Down Expand Up @@ -595,6 +619,9 @@ module Impl =
w.Stop()
return TestSummary.single Passed (float w.ElapsedMilliseconds)
with
| :? PassWithMessage as e ->
w.Stop()
return TestSummary.single (PassedWithMessage ("\n"+e.Message)) (float w.ElapsedMilliseconds)
| :? AssertException as e ->
w.Stop()
let msg =
Expand Down
1 change: 1 addition & 0 deletions Expecto/Model.fs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ type Test =
| Sequenced of SequenceMethod * Test

type ExpectoException(msg) = inherit Exception(msg)
type PassWithMessage(msg) = inherit ExpectoException(msg)
type AssertException(msg) = inherit ExpectoException(msg)
type FailedException(msg) = inherit ExpectoException(msg)
type IgnoreException(msg) = inherit ExpectoException(msg)
Expand Down
7 changes: 7 additions & 0 deletions Expecto/TestResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ let writeNUnitSummary file (summary: TestRunSummary) =
|> addAttribute "executed"

match test.result with
| PassedWithMessage _
| Passed -> "Success"
| Error _
| Failed _ -> "Failure"
| Ignored _ -> "Ignored"
|> addAttribute "result"

match test.result with
| PassedWithMessage _
| Passed -> addAttribute "success" "True"
| Error _
| Failed _ -> addAttribute "success" "False"
Expand All @@ -59,11 +61,15 @@ let writeNUnitSummary file (summary: TestRunSummary) =
// TODO: implement it.
addAttribute "asserts" "0"

let passNode = XElement(XName.Get "pass")
let failureNode = XElement(XName.Get "failure")

// Some more details that explain why a test was not executed.
match test.result with
| Passed -> ()
| PassedWithMessage m ->
passNode.Add(XName.Get "message", XCData m)
element.Add passNode
| Error e ->
failureNode.Add(XName.Get "message", XCData e.Message)
failureNode.Add(XName.Get "stack-trace", XCData e.StackTrace)
Expand Down Expand Up @@ -136,6 +142,7 @@ let writeJUnitSummary file (summary: Impl.TestRunSummary) =
XAttribute(XName.Get "message", message))
match test.result with
| Passed -> [||]
| PassedWithMessage msg -> [|makeMessageNode "pass" msg|]
| Error e ->
let message = makeMessageNode "error" e.Message
message.Add(XCData(e.ToString()))
Expand Down

0 comments on commit 8d765df

Please sign in to comment.