Home > Mobile >  How to print output in Visual Studio Test Explorer from F# and xUnit on success?
How to print output in Visual Studio Test Explorer from F# and xUnit on success?

Time:07-08

I am testing a client application that is requesting data from a database across the network. I have studied similar questions on SO, but they seem to apply several years ago before the really good nuget packages of Hedgehog and Swensen.Unquote were available. ( I am a complete newbie to unit testing).

Is there currently a good way, without creating an F# Class object, of forcing xUnit to print output to the Visual Studio Test Explorer without forcing it as an error?

The below code works to print the visits returned from the server, but only if it is "in error".

How can I get it to print the returned values without it being marked as an error?

TIA

module Tests

open Xunit
open Hedgehog
open Swensen.Unquote
open System
open Stargate.XI.Client

[<Fact>]
let ``Get Visits`` () =
   
   let visits = Scheduling.getVisits (new DateTime(2022,04,22))

   test <@ printfn "%O" visits = () @>

Edit#1 For others who want to do this, the below suggestion worked great for me with the following changes:

[<Fact>]
    let ``Get Visits`` () =
       let visits = Scheduling.getVisits (new DateTime(2022,04,22))
       output.WriteLine(sprintf "%A" visits)      
       Assert.True(true)

CodePudding user response:

XUnit supports writing of additional output from the tests. I agree, additional output can be useful to diagnose potential failures, data conditions (for integration tests) and can provide some additional instrumentation in the CI.

Is there a reason why you don't want to create additional class? With such class, all that you need to do is to create a constructor that accepts an ITestOutputHelper parameter:

module Tests

open System
open Xunit
open Xunit.Abstractions

type GeneralTests(output : ITestOutputHelper) =

    [<Fact>]
    let ``My test`` () =
        output.WriteLine("Here I am!")
        Assert.True(true)

More information: https://xunit.net/docs/capturing-output

I would say that having a separate class would give you additional control over the lifecycle of the resources you utilize in your tests.

  • Related