测试过程.开始? [英] Testing Process.Start?

查看:49
本文介绍了测试过程.开始?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,它将管理一个外部实用程序的多个实例,为每个实例提供数据并获取结果.

I am creating an application that will manage multiple instances of an external utility, supplying each with data and fetching results.

但是当我为班级编写单元测试时,我遇到了一个问题.

But as I am writing unit tests for the class I came upon a problem.

如何测试目标方法在调用时是否实际启动了一个进程(通过属性设置)?

我试过了:

  • 让类执行一个外部进程,然后使用 GetProcessesByName 检查它是否已启动.
  • 使用输出重定向,例如使用大于号将某些内容回显到文件中并测试其存在

我觉得发布代码和/或创建另一个要测试的 exe 是多余的.

I feel like emitting code and/or creating yet another exe to test is overkill.

这是确切的方法:

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");

    }
}

我想对其进行单元测试,以便如果没有任何东西在运行(_isRunning == false),则应该生成一个新进程.

I want to unit-test it so that if nothing is running (_isRunning == false), a new process should be spawned.

我感到很困惑,有没有一种优雅的方法来对外部进程实际启动进行单元测试?

I feel stumped, is there an elegant way to unit-test that an external process actually starts?

推荐答案

我会使用依赖注入和模拟或假类来解决这个问题.注意我使用实例方法来启动而不是类方法.在您的常规代码中,您可以使用默认构造函数,它会为您创建一个进程.对于测试,您可以注入一个模拟或假进程,只需检查是否在模拟对象上调用了正确的方法,而根本不必实际启动进程.您需要调整它以考虑我省略的属性.前任.下面:

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");
      }
 }

测试代码...

 [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 );
      ...
 }

这篇关于测试过程.开始?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆