断言一个例外或另一个例外

时间:2013-08-20 10:25:29

标签: c# nunit

假设我有测试方法A,B,C。 当我全部启动时,测试方法B抛出SQLiteException,一切都是绿色的,没问题。

Assert.Throws<SQLiteException>(() => sql.Select(selectFrom + "table_name"));

但是,当我只启动测试方法B时,它会在SQLiteException之前抛出ArgumentException并且测试失败。

问题是:如何断言抛出一个或另一个异常?

我在说这样的事情

Assert.Throws<SQLiteException>(() => sql.Select(selectFrom + "table_name")).OR.Throws<ArgumentException>()

1 个答案:

答案 0 :(得分:1)

try {
    somethingThatShouldThrowAnAcception();
    Assert.Fail(); // If it gets to this line, no exception was thrown
} catch (GoodException) { }

您应该能够根据自己喜欢的方式调整此方法,包括您想要捕获的特定异常。如果您只期望某些类型,请使用以下命令关闭catch块:

} catch (GoodException) {
} catch (Exception) {
    // don't want this exception
    Assert.Fail();
}

请记住,你不能这样做

try {
    somethingThatShouldThrowAnAcception();
    Assert.Fail();
} catch (Exception) { }

因为Assert.Fail()通过抛出AssertionException来工作。

你也可以这样做

try {
    somethingThatShouldThrowAnAcception();
    Assert.Fail("no exception thrown");
} catch (Exception ex) {
    Assert.IsTrue(ex is SpecificExceptionType);
}
相关问题