You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TestContext.WriteLine throwing System.FormatException when json string is passed.
Method Signature: public abstract void WriteLine(string format, params object[] args);
Steps to reproduce
Create a unit test project, add MSTest.TestFramework dll and execute the below test method
[TestClass]
public class UnitTest1
{
private TestContext testContextInstance;
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
[TestMethod]
public void TestMethod1()
{
var msg = "{curly braces}";
//Debug.WriteLine(msg);
TestContext.WriteLine(msg);
}
}
Expected behavior
Test sholuld pass, user should be able to write the json in output.
Actual behavior
Test is failing with System.FormatException.
RCA :
This is happening because string.format is used for implementing this API and it expects format which placeholders {0}, {1}, etc. and corresponding arguments.
But when json string is passed, it just throws System.FormatException as string is present inside curly braces.
Workaround:
As a workaround, user has to add one extra curly brace for every curly brace present in string.
string str = "{{mystring}}";
Fix :
Do not use string.format api when count of arguments are null in implementation of API public abstract void WriteLine(string format, params object[] args);
The text was updated successfully, but these errors were encountered:
@harshjain2 : This is actually an API with a format and an args. I think the right fix here is to have another API like so
public abstract void WriteLine(string message)
and the user takes care of formatting in any way he/she chooses.
Description
Method Signature:
public abstract void WriteLine(string format, params object[] args);
Steps to reproduce
Expected behavior
Test sholuld pass, user should be able to write the json in output.
Actual behavior
Test is failing with System.FormatException.
RCA :
This is happening because string.format is used for implementing this API and it expects format which placeholders {0}, {1}, etc. and corresponding arguments.
But when json string is passed, it just throws System.FormatException as string is present inside curly braces.
Workaround:
As a workaround, user has to add one extra curly brace for every curly brace present in string.
string str = "{{mystring}}";
Fix :
Do not use string.format api when count of arguments are null in implementation of API
public abstract void WriteLine(string format, params object[] args);
The text was updated successfully, but these errors were encountered: