有没有办法在try/catch块中一般地包装任何函数调用?

sco*_*ttm 4 c# integration-testing exception-handling

我正在为一个项目编写一堆集成测试.我想调用包含在try/catch块中的每个单独的集成点方法,这样当它失败时,我会得到某种反馈来显示,而不是仅仅崩溃应用程序.我还希望能够计算调用的时间,并在需要时检查返回值.所以,我有一个IntegrationResult类,其中包含一些基本描述,结果和时间已过去的属性:

class IntegrationResult
{
  private StopWatch _watch;

  public string Description {get;set;}

  public string ResultMessage {get;set;}

  public bool TestPassed {get;set;}

  public string TimeElapsed {get { return _watch == null ? "0" : _watch.Elapsed.TotalMilliseconds.ToString(); } }

  public void Start()
  {
    _watch = StopWatch.StartNew();
  }  

  public void Stop()
  {
    _watch.Stop();
  }
}
Run Code Online (Sandbox Code Playgroud)

我写的代码看起来像这样:

IntegrationResult result = new IntegrationResult();
result.Description = "T-SQL returns expected results";

    try
    {
      result.Start();
      SomeIntegrationPoint("potential arguments"); //This is the line being tested
      result.Stop();

      //do some check that correct data is present

      result.TestPassed = true;
      result.ResultMessage = "Pulled 10 correct rows";
    }
    catch(Exception e)
    {

      result.TestPassed = false;
      result.ResultMessage = String.Format("Error: {0}", e.Message);
    }
Run Code Online (Sandbox Code Playgroud)

我真的希望能够将SomeIntegrationPoint方法作为参数和委托或其他东西来检查结果,但我无法弄清楚是否可能.是否有任何框架来处理这种类型的测试,或者您对如何简化代码以便更好地重用有任何建议吗?我已经厌倦了输入这个块;)

Ree*_*sey 6

(我假设这是C#,标记为......虽然语法不在问题中.)

你可以这样做.只需将结果类更改为包括:

class IntegrationResult
{
      string Description { get; set; }
      string SuccessResultMessage { get; set; }
      string FailResultMessage { get; set; }

      public IntegrationResult(string desc, string success, string fail)
      {
          this.Description = desc;
          this.SuccessResultMessage = success;
          this.FailResultMessage = fail;
      }

      public bool ExecuteTest(Func<IntegrationResult, bool> test)
      {
          bool success = true;
          try
          {
              this.Start();
              success = test(this);
              this.Stop();
              this.ResultMessage = success ? 
                                      this.SuccessResultMessage : 
                                      this.FailResultMessage;
              this.TestPassed = true;
          }
          catch(Exception e)
          {
               this.TestPassed = false;
               this.ResultMessage = String.Format("Error: {0}", e.Message);
               success = false;
          }
          return success;
      }
       ...
Run Code Online (Sandbox Code Playgroud)

然后,您可以将测试代码更改为:

private void myDoTestMethod(string argumentOne, string argumentTwo)
{
    IntegrationResult result = new IntegrationResult(
                                   "T-SQL returns expected results", 
                                   "Pulled 10 correct rows",
                                   "Wrong number of rows received");
    result.Execute( r=>
    {
         integrationPoint.call(argumentOne, argumentTwo);
         //do some check that correct data is present (return false if not)
         return true;
    });
 }
Run Code Online (Sandbox Code Playgroud)

这可以很容易地扩展到包括你的时间.