【问题标题】:Handle expected exception in unit test in MSTest在 MSTest 中处理单元测试中的预期异常
【发布时间】:2017-06-28 16:20:18
【问题描述】:

我正在使用以下测试方法测试DoingSomething() 方法-

[TestMethod()]
[ExpectedException(typeof(ArgumentException),"Invalid currency.")]
public void ConvertCurrencyTest_ExhangeRate()
{
    try
    {
        DoingSomething();
    }
    catch (ArgumentException Ex)
    {
    }
    catch (Exception Ex)
    {
        Assert.Fail();
    }
}

测试结果显示DoingSomething() 没有抛出异常。但它确实引发了异常。

我错过了什么?

【问题讨论】:

  • 您正在使用 try/catch 中的异常,因此它不会冒泡被测试捕获。

标签: c# unit-testing exception mstest


【解决方案1】:

您正在使用 try/catch 中的异常,因此它不会冒泡被测试捕获。

删除try/catch 并让测试工具处理异常。任何其他异常自然会导致测试失败。

[TestMethod()]
[ExpectedException(typeof(ArgumentException),"Invalid currency.")]
public void ConvertCurrencyTest_ExhangeRate() {       
    DoingSomething();        
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    相关资源
    最近更新 更多