获取NUnit断言失败消息

时间:2015-02-09 09:40:33

标签: c# unit-testing nunit testng

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

Assert.That(true, Is.False);

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

Message: Expected: False But was: True

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

2 个答案:

答案 0 :(得分:2)

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

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

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

 }
 }

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

答案 1 :(得分:0)

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

这样的东西

Assert.IsTrue()提供了多次重载,并为您提供了在失败时传递错误消息的选项。请参阅this

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