问题
There is a command line tool for which we need to write automated tests. Our test framework is written in C# and I was looking for .NET libraries which would let me do the automation (I know I can use the Process class; redirect the IO; do the validation, but I don't want to reinvent the wheel if possible). Ideally I'm looking for something like an expect library. Any suggestions?
回答1:
I realize that this is an old question, but no one has answered.
IMO, I've always had to reinvent the wheel every time I've done this (i.e. Process class, redirect StdIO, StdErr, etc and parse the output). Thought, I've reused an abstract base class for this purpose.
Maybe someone else knows of a library to do this, but i do not.
However I will give a warning that I have seen cases where redirecting the IO is not always properly handled in the .Net framework (depending on how the program writes to stdio or stderr, if you're hiding the window or not, if you're multi-threading, and a few other conditions) leading to a race condition that results in an indefinite hang.
回答2:
http://blog.iwanek.eu/expect-net/
using ExpectNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ExampleApp
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("ExampleApp");
Session spawn = Expect.Spawn(new ProcessSpawnable("cmd.exe"));
spawn.Expect("c:", s => Console.WriteLine("got: " + s));
}
}
}
来源:https://stackoverflow.com/questions/1169943/command-line-automation-expect-equivalent