I am writing integration tests using the MSTest framework. Tests and test code have a significant log built into them.
I am trying to find a way to connect to the Assert output so that I can write it to the log files along with the rest of the log.
For example, if I have a testing method, for example
[TestMethod]
SomeRandomIntegrationTest()
{
Assert.AreEqual(true, false, "This error message should also appear in the log");
}
I would get
Message: Assert.AreEqual failed. Expected. Got a lie. This error message should also appear in the log.
in my log file.
I tried to do
private StringBuilder testOutputBuilder;
private StringWriter testOutputWriter;
private TextWriter originalWriter;
[TestInitialize]
public virtual void Initialize()
{
testOutputBuilder = new StringBuilder();
testOutputWriter = new StringWriter(testOutputBuilder);
originalWriter = Console.Out;
Console.SetOut(testOutputWriter);
}
[TestCleanup]
public virtual void TestCleanup()
{
if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
{
}
Console.SetOut(originalWriter);
testOutputWriter.Dispose();
}
but testOutputBuilderreturns an empty string.
How can I capture string output from assert methods in MSTest?
source
share