获取 NUnit 断言失败消息

Get NUnit assertion failure message

我是测试新手。在使用 Visual Studio 进行一些简单练习后,我转向了 Nunit 测试框架。我正在寻找的是 属性 或在 [TearDown] 测试方法中使用的方法,它为我提供了一个字符串,在失败的情况下,该字符串包含错误消息。 例如,如果我编写此断言:

Assert.That(true, Is.False);

然后框架收到此错误消息:

Message: Expected: False But was: True

我需要的是上面的字符串。 谢谢。

如果我理解你的问题,那么你可能正在寻找 Assert.IsTrue()。类似于 Assert.IsTrue(true, "Test Passed")

Assert.IsTrue() 提供多重重载,并让您可以选择在 failure.See this

情况下传递错误消息

其次,您不想在拆解中使用断言。您可能想在单独的测试中使用断言。 TearDown 属性主要用于在测试后进行处理或做一些清理工作。注意:不要与 TestFixtureTearDown 混淆。查看所有断言 here

只有一种方法可以获得断言失败消息或异常消息...解决方案是实现扩展 class! 关于这个主题,我建议您访问这些链接:

这是我实现最重要步骤的代码:

namespace UTExtension
{
  [ NUnitAddinAttribute(Type = ExtensionType.Core)]
  public class ExtensionToLog: IAddin, EventListener
  {

#region IAddin Members

public bool Install(IExtensionHost host)
{
  if (host == null)
    throw new ArgumentNullException("MyExtension.Install() method has failed beacuse 'host' is NULL");

  IExtensionPoint listeners = host.GetExtensionPoint("EventListeners");
  if (listeners == null)
    return false;

  listeners.Install(this);
  return true;
}

#endregion

#region Event Listener Members

/// <summary>
/// This method is executed before the TearDown test method.
/// When the test method will fail then in order to get the failure message we will use the method below. 
/// </summary>
public void TestFinished(TestResult result)
{
  if ((result.Message == null) && result.Executed && (result.ResultState == ResultState.Success))
  {
    // the single test METHOD has passed
    Console.Out.WriteLine("\t\tPassed\n");
  }
  else if(result.Message != null)
  {
    // something goes wrong
    string errorMsg = null;
    errorMsg = result.Message;
    errorMsg = Regex.Replace(errorMsg, @"\s+", " ");

    // an assert fails during the single test method
    bool assertFails= result.Executed && (result.ResultState == ResultState.Failure);
    // an exception occurrs during the single test method
    bool exeptionOccurrs = result.Executed && (result.ResultState == ResultState.Error);

    if (assertFails || exeptionOccurrs)
    {

      Console.Out.WriteLine("\t\tFailed\n");
    }
    else
    {
      // if an exception occurred during the load of the test environment
      bool loadingExeptionOccurrs = result.Executed && (result.ResultState == ResultState.Error);
      if (loadingExeptionOccurrs)
      {
          Console.Out.WriteLine("\nException --> " + errorMsg);
      }
    }

  }
}

private static bool TestMethodError = false;

/// <summary>
/// This method is executed before the FixtureTearDown test method. 
/// </summary>
public void SuiteFinished(TestResult result)
{
  // an exception occurred during the load of the test environment
  bool loadingExeptionOccurrs = result.Executed && (result.ResultState == ResultState.Failure);

  if (!loadingExeptionOccurrs || TestMethodError)
  {

  }

  TestMethodError = false; // reset the variable

}


public void RunStarted(string name, int testCount)
{
  Console.Out.WriteLine("Run has started: name= " + name + "; test count= " + testCount);
}

public void RunFinished(TestResult result)
{
  Console.Out.WriteLine("\n\nRUN HAS FINISHED: " + result.Name + " ; message: " + result.Message);
}

public void RunFinished(Exception exception)
{
  Console.Out.WriteLine("\n\nRUN HAS FINISHED: " + exception.Message);
}

public void TestStarted(TestName testName)
{
  Console.Out.WriteLine("\t\tTest method: " + testName.Name);
}
// This methos will be executed before the 'TestFixtureSetUp' method
public void SuiteStarted(TestName testName)
{
  Console.Out.WriteLine("Test Class: " + testName.Name);
}

public void UnhandledException(Exception exception)
{
  Console.Out.WriteLine("Suite has finished: " + exception.Message);
}

public void TestOutput(TestOutput testOutput)
{
}

#endregion

 }
 }

请注意,如果您只需要一个扩展 class,那么您可以将其添加到您的测试程序集中,而无需在 NUnit 插件文件夹中添加相关的 dll。