简体   繁体   English

测试过程。开始?

[英]Testing Process.Start?

I am creating an application that manages multiple instances of an external utility, supplying each with data and fetching results.我正在创建一个管理外部实用程序的多个实例的应用程序,为每个实例提供数据并获取结果。

But I'm facing a problem writing unit tests.但是我在编写单元测试时遇到了问题。

How to test if the target method actually starts a process (set via a property) when called?如何测试目标方法在调用时是否真的启动了一个进程(通过属性设置)?

I have tried:我努力了:

  • Make the class execute an external process and then use GetProcessesByName() to check if it has started.让该类执行一个外部进程,然后使用GetProcessesByName()检查它是否已启动。
  • Use output redirection, eg using the greater-than sign to echo something to a file and test its existence使用输出重定向,例如使用大于号将某些内容回显到文件并测试其存在

I feel like creating yet another .exe to test it is overkill.我觉得创建另一个.exe来测试它是矫枉过正的。

Code:代码:

public void Start() 
{
    if (!_isRunning) 
    {
        var startInfo = new ProcessStartInfo() {
          CreateNoWindow = true,
            UseShellExecute = true,

            FileName = _cmdLine,
            Arguments = _args
        };

        _process = Process.Start(startInfo);
        _isRunning = true;
    } 
    else 
    {
        throw new InvalidOperationException("Process already started");
    }
}

I want to unit-test it so that a new process should start if nothing is running ( _isRunning == false ).我想对其进行单元测试,以便在没有任何运行时启动一个新进程( _isRunning == false )。

I would approach this using dependency injection and using a mock or fake class.我会使用依赖注入并使用模拟或假类来解决这个问题。 Note I'm using the instance method for start instead of the class method.注意我使用的是实例方法而不是类方法。 In your regular code, you can use the default constructor and it will create a process for you.在您的常规代码中,您可以使用默认构造函数,它将为您创建一个进程。 For testing you can inject a mock or fake process and simply check that the proper methods are called on your mock object and never have to actually start a process at all.对于测试,您可以注入一个模拟或假进程,并简单地检查在您的模拟对象上调用了正确的方法,并且根本不需要实际启动一个进程。 You'll need to adjust this to take account of the properties I've omitted.您需要调整它以考虑我省略的属性。 Ex.前任。 below:以下:

 public class UtilityManager
 {
      public Process UtilityProcess { get; private set; }

      private bool _isRunning;

      public UtilityManager() : this(null) {}

      public UtilityManager( Process process )
      {
          this. UtilityProcess = process ?? new Process();
          this._isRunning = false;
      }

      public void Start()
      {
          if (!_isRunning) {
          var startInfo = new ProcessStartInfo() {
              CreateNoWindow = true,
              UseShellExecute = true,

              FileName = _cmdLine,
              Arguments = _args
          };

          this.UtilityProcess.Start(startInfo);
          _isRunning = true;

      } else {
          throw new InvalidOperationException("Process already started");
      }
 }

Test code...测试代码...

 [TestMethod]
 public void StartTest()
 {
      Process proc = new FakeProcess();  // May need to use a wrapper class
      UtilityManager manager = new UtilityManager( proc );
      manager.CommandLine = "command";
      ...

      manager.Start();


      Assert.IsTrue( proc.StartCalled );
      Assert.IsNotNull( proc.StartInfo );
      Assert.AreEqual( "command", proc.StartInfo.FileName );
      ...
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM