mercredi 25 mars 2015

Redirecting Console.Out within test Setup and Teardown

This is a follow up from Grabbing the output sent to Console.Out from within a unit test? and the referenced article in the accepted answer by Mark Seemann.


I would like to use Console.Out and Console.In to redirect the streams while testing. Every test in the class needs to use the redirects.


In order to keep the tests clean I would like to do this in the test SetUp and TearDown.


This is what I was thinking:



private StringWriter _sw;
private StringReader _sr;
[SetUp]
public void SetUp()
{
_sw = new StringWriter();
Console.SetOut(_sw);
_sr = new StringReader("100");
Console.SetIn(_sr);
}

[TearDown]
public void TearDown()
{
var standardOut = new StreamWriter(Console.OpenStandardOutput());
standardOut.AutoFlush = true;
Console.SetOut(standardOut);
Console.SetIn(new StreamReader(Console.OpenStandardInput()));
}


Then I would use '_sw.ToString()' within the tests to verify what was written to the stream.


Are there any major drawbacks of using setup or teardown methods for this? Would this similar to redirecting with a using statement? E.g.



using (StringWriter sw = new StringWriter())
{
...
}

Aucun commentaire:

Enregistrer un commentaire