简体   繁体   English

C#如何在单元测试中过滤非断言异常

[英]C# How to filter non assert exceptions in Unit Testing

I've a method test that does not have a clear assert expression. 我有一个方法测试,没有明确的断言表达式。 The returned tested value is a very long string that has to be inspected by a programmer to check if it is correct or not. 返回的测试值是一个非常长的字符串,必须由程序员检查以检查其是否正确。 For this reason, if the code executes without exceptions, I'm calling 'Assert.Inconclusive'. 因此,如果代码无例外地执行,则称为“ Assert.Inconclusive”。

However, if some kind of Exception is thrown, I want to call 'Assert.Fail' with the exception message. 但是,如果引发某种异常,我想用异常消息调用“ Assert.Fail”。 Something like this: 像这样:

      [TestMethod()]
      public void Test()
      {
        try {
            string toBeChecked = MethodToBeTested();
            //the string is so particular that no clear
            //assertion can be made about it.

            Console.WriteLine(toBeChecked);             
            Assert.Inconclusive("Check the console output.");            
        } catch(Exception e) {
            Assert.Fail(e.Message);
        }
      }

The problem with this code is that if no regular exception is thrown, the Assert.Inconclusive method also throws an exception that is catched, so Assert.Fail is called, and from the IDE test result pane it seems that the test has failed. 此代码的问题在于,如果未引发任何常规异常,则Assert.Inconclusive方法也会引发Assert.Inconclusive的异常,因此将调用Assert.Fail ,并且从IDE测试结果窗格看,测试似乎已失败。 This is not what I want. 这不是我想要的。

Is there a way to filter the exceptions, such as catching every exception but the Assert-like ones? 有没有一种方法可以过滤异常,例如捕获除类似于Assert的异常之外的所有异常?

(I'm using .NET framework 3.5SP1) (我正在使用.NET Framework 3.5SP1)

The Assert.Inconclusive method should throw a AssertInconclusiveException so you can either mark the Test as ExcpectedException(typeof(AssertInconclusiveExcpetion)) or use something like this: Assert.Inconclusive方法应引发AssertInconclusiveException,因此您可以将测试标记为ExcpectedException(typeof(AssertInconclusiveExcpetion))或使用类似以下内容的方法:

  [TestMethod()]
  public void Test()
  {
    try {
        string toBeChecked = MethodToBeTested();
        //the string is so particular that no clear
        //assertion can be made about it.

        Console.WriteLine(toBeChecked);             
        Assert.Inconclusive("Check the console output.");            
    } catch(AsssertInconclusiveException) {
       /* Do nothing */
    }
    } catch(Exception e) {
        Assert.Fail(e.Message);
    }
  }

Why not just leave out the Assert.Inconclusive()? 为什么不只省去Assert.Inconclusive()? In fact, why catch any exceptions at all - if the code throws an exception the unit test framework will mark it as failed. 实际上,为什么要根本捕获任何异常-如果代码引发异常,则单元测试框架会将其标记为失败。 Less is more: 少即是多:

  [TestMethod()]
  public void Test()
  {
     string toBeChecked = MethodToBeTested();
     Console.WriteLine(toBeChecked);             
  }

But this is a poor unit test if we can not automatically check the result. 但是,如果我们不能自动检查结果,这将是一个糟糕的单元测试。 All we are checking is that no exception is thrown. 我们正在检查的只是没有异常抛出。 Are there no asserts you can make about the resulting string? 您是否可以对结果字符串进行断言?

For example, that is is not null or empty? 例如,这不是空值还是空值? Do we expect it to contain a certain sub-string that we can test? 我们是否期望它包含可以测试的特定子字符串?

At least give the test a helpful name, which includes something like: ManualAssertRequired 至少给测试一个有用的名称,其中包括: ManualAssertRequired

Try to catch specific exception type instead of Exception or add another catch for nunit exception that is caused by Assert.Inconclusive method which is AssertInconclusiveException ... 尝试捕获特定的异常类型,而不是Exception或者添加另一个捕获由Assert.Inconclusive方法导致的Assert.Inconclusive异常,这是AssertInconclusiveException ...

For example modify it like this: 例如,像这样修改它:

  [TestMethod()]
  public void Test()
  {
    try {
        string toBeChecked = MethodToBeTested();
        //the string is so particular that no clear
        //assertion can be made about it.

        Console.WriteLine(toBeChecked);             
        Assert.Inconclusive("Check the console output.");    
    } catch(AssertInconclusiveException e) {
        // do nothing...
    } catch(Exception e) {
        Assert.Fail(e.Message);
    }
  }

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

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