I have a set of NUnit tests executed on a build server. I need to see test logs (results, outputs) in ApplicationInsights in Azure.
- NUnit has
ResultWriter
but it seems difficult to implement. - I can log from my test explicitly to AppInsights but I don't see failed test logs
I've considered few options:
- redirecting outputs from NUnit -> cannot find the right API
- Instrumenting Test application with AppInsights -> doesn't seem possible
- redirecting standard output/error to ILogger -> could not find a way to do it
I tried hooking up Application Insights and overriding some of NUnit TestContext properties but without any luck
CodePudding user response:
The term "log" generally means output produced incrementally as an application is running. NUnit results are output at the end of the run, so I wouldn't call them a log. The results are in an XML file and you can control where they are placed. I assume that AppInsights could accept these files although they are a bit "busy" and require some effort to interpret.
If you want the information from the result XML in a more readable form, that's what the ResultWriter
extension point is for. It can be easy or hard to implement, depending on what you want to see.
NUnit also produces InternalTrace
logs if you ask for them. These are intended for debugging NUnit itself, so I'm guessing this is not what you want.
Finally, the nunit3-console
runner writes to the console as it executes. This could be treated as a log and redirected to a file. Normal command-line redirection will redirect all the output, while the --out
parameter will redirect the text output created by your tests but not NUnit's own summaries.
NUnit has an InternalTrace feature, which produces logs, but it is aimed at debugging NUnit (hence "internal") so you probably don't mean that. It also writes to the console while it runs, which can be considered a log. Finally, it's result XML file is not a strict "log" because it is produced at the end of the run.
CodePudding user response:
Looks like I was looking at this all wrong. This SO answer is the solution to what I was trying to achieve: How to log NUnit Test Error or fail message?
First add App Insights to console app
Log messages can be added directly to AI from the test.
Test results can be captured in TearDown
like this:
[TearDown]
public void TearDown()
{
using var scope = _logger.BeginScope(new Dictionary<string, object> {
{ "TestName", TestContext.CurrentContext.Test.Name },
{ "FullName", TestContext.CurrentContext.Test.FullName }
});
var result = TestContext.CurrentContext.Result;
switch (result.Outcome.Status)
{
case NUnit.Framework.Interfaces.TestStatus.Passed:
_logger.LogInformation("Test Passed");
break;
case NUnit.Framework.Interfaces.TestStatus.Inconclusive:
_logger.LogWarning("Test Result was Inconclusive");
break;
case NUnit.Framework.Interfaces.TestStatus.Skipped:
_logger.LogInformation("Test was skipped");
break;
case NUnit.Framework.Interfaces.TestStatus.Failed:
_logger.LogError(new AssertionException(result.Message), "Test Failed. Error:{error}, StackTrace:{stackTrace}", result.Message, result.StackTrace);
break;
}
}