Skip to content

Instantly share code, notes, and snippets.

@BlitzkriegSoftware
Created August 24, 2019 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BlitzkriegSoftware/47b8f6658a4c948a67a0e30cc124bc97 to your computer and use it in GitHub Desktop.
Save BlitzkriegSoftware/47b8f6658a4c948a67a0e30cc124bc97 to your computer and use it in GitHub Desktop.
A handy testing method to serialize an object as JSON for MSTEST output
/// <summary>
/// Helper to output as JSON
/// </summary>
public static class TestOutputHelper
{
/// <summary>
/// Emit an object as json
/// </summary>
/// <param name="output">ITestOutputHelper</param>
/// <param name="o">object</param>
/// <param name="title">(optional) Title</param>
public static void AsJson(this TestContext output, object o, string title = null)
{
var json = JsonConvert.SerializeObject(o);
if (string.IsNullOrWhiteSpace(title)) output.WriteLine("{0}", json);
else output.WriteLine("{0}\n{1}", title, json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment