thomhurst / TUnit

A modern, fast and flexible .NET testing framework
MIT License
1.02k stars 8 forks source link

Output Messages #525

Open biqas opened 3 days ago

biqas commented 3 days ago

Hi,

How to inject logging into tests? or write to output window or any output sink?

thomhurst commented 3 days ago

Hi @biqas - If you're executing using dotnet run then Console output should display normally.

If you're using Visual Studio, I think they're still working on showing the Console output in each test results window.

If you want to dynamically retrieve a tests output, you could register a global [AfterEvery(Test)] method.

E.g.

    [AfterEvery(Test)]
    public static async Task TestOutput(TestContext context)
    {
        var output = context.GetTestOutput();
        var errorOutput = context.GetTestErrorOutput();

        // Do something with `output` or `errorOutput` 
    }

Does that solve your problem?

biqas commented 2 days ago

Hi,

to get the output yes, but i would like to redirect the logger output to the test output. This is in some complex cases needed to see how far a test came along to look at the logs in the test output.

thomhurst commented 2 days ago

I don't quite understand what you mean by "logger output to test output"? Are you talking about the visual studio results window?

If not, can you give an equivalent example in xUnit or NUnit?

biqas commented 2 days ago

Something like this:

public MyTest(ITestOutputHelper output, LogLevel logLevel = LogLevel.Debug)
{
    Logger = output.BuildLogger(logLevel, GetType().Name);
}

ITestOutputHelper is from xUnit. This redirects all ILogger messages to the output of the unit test. To achieve this there is an extension needed (Neovolve.Logging.Xunit).

thomhurst commented 1 day ago

@biqas You can do Logger = new DefaultLogger() for a logger that writes to the console. Otherwise you can define your own class that inherits from TUnitLogger and then define your own logging implementation.

Does that help?

david-driscoll commented 4 hours ago

I think the big thing that @biqas is after (and myself as well) is the ability to log and capture logs for a given test and provide that as output similar to how xunit has the ITestOutputHelper.

It's very close to how OutputWriter and ErrorOutputWriter are working with the test context. The only issue is that the log information is not available in the the IDE when you're inspecting a given test.

thomhurst commented 4 hours ago

I think the big thing that @biqas is after (and myself as well) is the ability to log and capture logs for a given test and provide that as output similar to how xunit has the ITestOutputHelper.

It's very close to how OutputWriter and ErrorOutputWriter are working with the test context. The only issue is that the log information is not available in the the IDE when you're inspecting a given test.

When TUnit finishes a test it sends all the relevant error / standard output to the runner. So I believe not seeing the output is a current limitation of the new Microsoft testing framework (correct me if I'm wrong) and I believe they're working on it over at the testfx repo

thomhurst commented 4 hours ago

Also it might be a limit or the ide. As this is all quite new there's multiple parts here that all need to work together. I think TUnit is fulfilling it's part and that the IDEs simply need to improve support.

Xor-el commented 4 hours ago

These links below might provide some hints as to what @biqas is inquiring about.

https://blog.martincostello.com/writing-logs-to-xunit-test-output/

and

https://www.meziantou.net/how-to-view-logs-from-ilogger-in-xunitdotnet.htm

thomhurst commented 3 hours ago

These links below might provide some hints as to what @biqas is inquiring about.

https://blog.martincostello.com/writing-logs-to-xunit-test-output/

and

https://www.meziantou.net/how-to-view-logs-from-ilogger-in-xunitdotnet.htm

I'll take a look but this is referring to the old VSTest runner. Tunit runs on the new runner. I don't think you can compare them like for like