haf / expecto

A smooth testing lib for F#. APIs made for humans! Strong testing methodologies for everyone!
Apache License 2.0
663 stars 96 forks source link

How to show test results in interactive #365

Closed halcwb closed 4 years ago

halcwb commented 4 years ago

How can I print the test results in a nice way in the interactive? I tried something like:

let config =
    { defaultConfig with ``parallel`` = false }

let handler (sum : Impl.TestRunSummary) =
    for (t, r) in sum.results do
        printfn "Test: %A Result: %A" t.name r.result

ValueRange.tests
|> runTests (config.appendSummaryHandler handler)

But this results in garbled output in the interactive. Is there an easy way to accomplish this?

haf commented 4 years ago

You have to create a logger:

open Expecto.Logging
open Expecto.Logging.Message
let logger = Log.create "FSI"
logger.info (eventX "Hi")
halcwb commented 3 years ago

@haf Sorry, but still not get this. For example with the following I was expecting that a verbose print should be visible in the FSI:

runTestsWithCLIArgs [ CLIArguments.Debug; CLIArguments.Sequenced ] [||]

However, whether or not I add the CLIArguments the print out just shows:

[15:43:56 INF] EXPECTO? Running tests... [15:43:56 INF] EXPECTO! 28 tests run in 00:00:00.0477646 for PIM – 28 passed, 0 ignored, 0 failed, 0 errored. Success! val it : int = 0

How can I tell Expecto to use the logger? Or do I need to manually insert a logging message in each test?

I use VSCode on a Macbook.

farlee2121 commented 8 months ago

@halcwb

It's still a bit scrappy, but I wrote these run methods for better interactive support. They capture the logs normally printed to the console and return them as a string from the run method. There's one with ANSI coloration and one without

module Tests =

    let private literateOutputWriter (outputBuilder: StringBuilder) (text: (string*ConsoleColor) list) : unit =
            let colorizeLine (text, color) = ColourText.colouriseText color text
            let sbAppend (builder: StringBuilder) (text: string) =
                builder.Append(text)

            text
            |> List.iter (colorizeLine >> (sbAppend outputBuilder) >> ignore)

    let private colorlessOutputWriter (outputBuilder: StringBuilder) (text: (string*ConsoleColor) list) : unit =
        text
        |> List.iter (fun (text, color) ->
            outputBuilder.Append(text)
            |> ignore)

    let run_ReturnLogs cliArgs args tests =

        let outputBuilder = StringBuilder("")

        Global.initialise 
            { Global.defaultConfig with
                getLogger = fun name ->
                    Expecto.Logging.LiterateConsoleTarget(
                        name = [|"boi"|], 
                        minLevel = Expecto.Logging.LogLevel.Info, 
                        outputWriter = (literateOutputWriter outputBuilder)) :> Expecto.Logging.Logger
            }

        Tests.runTestsWithCLIArgs cliArgs args tests |> ignore
        outputBuilder.ToString()

    let run_ReturnLogs_NoColor cliArgs args tests =

        let outputBuilder = StringBuilder("")

        Global.initialise 
            { Global.defaultConfig with
                getLogger = fun name ->
                    Expecto.Logging.LiterateConsoleTarget(
                        name =[|"boi"|], 
                        minLevel = Expecto.Logging.LogLevel.Info, 
                        outputWriter = (colorlessOutputWriter outputBuilder)) :> Expecto.Logging.Logger
            }

        Tests.runTestsWithCLIArgs cliArgs args tests |> ignore
        outputBuilder.ToString()