Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a ProgressTraceListener to redirect Trace output to the NUnit Progress output #4686

Open
maettu-this opened this issue Apr 9, 2024 · 1 comment

Comments

@maettu-this
Copy link

This issue is based on discussions at TestCentric/testcentric-gui#1043 and nunit/nunit3-vs-adapter#718 but more belongs to NUnit itself.

Similar to VS Test Adapter documentation which describes a workaround to activate Trace and Debug output, @CharliePoole has hinted "possible to create a listener which would capture the output and send it as a Progress event or write it to stderr, but I don't believe such a thing exists at this time" which I implemented at TestCentric/testcentric-gui#1043 (comment). It is simple and works.

Same as the ConsoleTraceListener class is provided by the .NET Framework, I propose the NUnit framework provides this NUnit specific ProgressTraceListener ready for use by the NUnit users. I don't ask NUnit to always have this listener active, it shall only provide the class and document its purpose and how to use it.

@maettu-this
Copy link
Author

maettu-this commented May 8, 2024

For completeness and those who would like to already use the resulting proposal, here's the code:

	/// <summary>
	/// Allows directing tracing or debugging output to <see cref="TestContext.Progress"/>.
	/// </summary>
	/// <remarks>
	/// Implemented following the <see cref="ConsoleTraceListener"/> implementation.
	/// </remarks>
	[HostProtection(Synchronization = true)]
	public class ProgressTraceListener : TextWriterTraceListener
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="ProgressTraceListener"/> class with trace
		/// output written to <see cref="TestContext.Progress"/>.
		/// </summary>
		public ProgressTraceListener()
			: base(TestContext.Progress)
		{
		}
	}

To activate, place the following snippet into the one-time set-up method of either a test's fixture or the set-up fixture of a project:

System.Trace.Listeners.Add(new ProgressTraceListener());

Make sure to only add a listener once, e.g.:

if (!System.Trace.Listeners.OfType<ProgressTraceListener>().Any())
	System.Trace.Listeners.Add(new ProgressTraceListener());

Or again remove and dispose in the one-time tear-down, e.g.:

System.Trace.Listeners.Remove(_progressTraceListener);
_progressTraceListener.Dispose();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant