TestContext.WriteLine has "System.FormatException : Input string was not in a correct format" when try to output JSON string - by Michael Freidgeim

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 2966651 Comments
Status Closed Workarounds
Type Bug Repros 0
Opened 7/23/2016 5:04:53 AM
Access Restriction Public


In test class I was using TestContext.WriteLine trying to output JSON string, I've got 
System.FormatException : Input string was not in a correct format.
    at System.Text.StringBuilder. AppendFormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.FormatHelper(IFormatProvider provider, String format, ParamsArray args)
   at System.String.Format(IFormatProvider provider, String format, Object[] args)
   at Microsoft.VisualStudio.TestPlatform.MSTestFramework.TestContextImplementation.WriteLine(String format, Object[] args)

Example of code that works for  Debug.WriteLine, but not for TestContext.WriteLine is the following:
             var msg  = "{curly braces}";

I found, that TestContext.WriteLine expects first parameter as a format string, that consider { and } as placeholder delimiters.
I had to write EscapeCurlyBraces extension 
  public static string EscapeCurlyBraces( this string message)
            string ret = message.Replace( "{", "{{" ).Replace("}", "}}");
            return ret;
and call 
     TestContext.WriteLine(msg.EscapeCurlyBraces() );
it will be good to have separate overload TestContext.WriteLine(string msg); that doesn't consider the parameter as format and doesn't expect any additional parameters.
Sign in to post a comment.
Posted by Microsoft on 11/8/2016 at 10:04 PM
Based on this feedback, we have added a new API : TestContext.WriteLine(string message) in latest release of MSTestV2, which takes formatted message as input.
Posted by Microsoft on 7/23/2016 at 9:26 AM
Thank you for your feedback, we are currently reviewing the issue you have submitted. If you require immediate assistance with this issue, please contact product support at http://support.microsoft.com/oas/default.aspx?prid=15825.