我有一个应用程序,可以像这样使用:
type file.txt|app.exe -i
即我的应用程序将从 file.txt 读取数据。
现在我想编写一些测试,以确保应用程序能够很好地处理 file.txt 中的一些特殊数据。
如何组织这个?
我的应用程序读取输入,例如
input = Console.In.ReadToEnd();
在不读取数据的简单测试中,我只是使用 App 类,例如:
using(App app = new App())
{
result = app.Run(args)
}
if (result != 0)
Assert.Fail("Failed");
最佳答案
您可以将控制台输入替换为您自己的对象,例如 StringReader
,并向其提供您想要的任何输入:
var oldIn = Console.In;
try
{
Console.SetIn(new StringReader("some input"));
using (App app = new App())
{
// input = Console.In.ReadToEnd(); happens here
result = app.Run(args);
}
if (result != 0)
{
Assert.Fail("Failed");
}
}
finally
{
Console.SetIn(oldIn);
}
关于c# - 模拟测试中的标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13601175/