【问题标题】:NUnit tests: Is IDisposable guaranteed if a non-expected exception is thrown?NUnit 测试:如果抛出非预期的异常,是否保证 IDisposable?
【发布时间】:2010-06-25 12:22:54
【问题描述】:

如果我在 NUnit 测试的 TestFixtureSetup 期间创建了 IDisposable,并且测试引发了意外异常(例如外部资源失败),是否会调用 IDisposable 的 Dispose()?

添加>>
如果没有,NUnit 是否提供保证执行 TestFixtureTearDown 或其他可用于清理的地方?

【问题讨论】:

    标签: exception nunit idisposable


    【解决方案1】:

    不,它不会被调用。 IDisposable 用于确定性终结,通常与using 语句一起使用。不过,您可以在 TestFixtureTearDown 中调用 Dispose 方法。

    【讨论】:

    • 请注意,如果TestFixtureSetUp 失败或抛出异常,TestFixtureTearDown 不会被调用:“只要任何 TestFixtureSetUp 方法运行没有错误,TestFixtureTearDown 方法就是保证运行。如果 TestFixtureSetUp 方法失败或抛出异常,它将不会运行。 NUnit TestFixtureTearDownAttribute Documentation
    • 从 NUnit 2.4 开始,Dispose() 将在您的 TestFixture 上被调用,这样您就可以在另一个地方放置清理代码。 Release notes on 2.4 Alpha: "如果对象实现了 IDisposable,则在销毁它之前调用 Dispose"
    猜你喜欢
    • 2013-10-08
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-26
    • 1970-01-01
    • 2011-03-25
    相关资源
    最近更新 更多