因此,我希望能够在TestCase中指定不同的异常消息,但不知道是如何做到的。
这是原版
[Test]
[ExpectedException(typeof(SystemException), ExpectedMessage = "Holiday cannot start or end on a weekend or non-working day")]
public void AddHolidays_StartsInvlaid()
{}这是和TestCase一起的
[TestCase("27/04/2025", "28/05/2025", "FullDay", "FullDay", ExpectedMessage = "Holiday cannot start or end on a weekend or non-working day")]
[ExpectedException(typeof(SystemException), ExpectedMessage)]
public void AddHolidays_Exceptions(string dateFrom, string dateTo, string fromPeriod, string toPeriod)
{}该方法运行良好,但我只希望能够使用NUnit TestCase指定异常消息。
发布于 2016-05-05 13:31:54
如果可以的话,我建议您离开ExpectedException。这被认为是一种糟糕的做法,因为如果测试中的代码抛出了您没有预料到的相同异常,则可能会导致错误。因此,ExpectedException已经从NUnit 3中删除了。而且,正如您所发现的,NUnit中的所有数据驱动属性都不完全支持ExpectedException。
将代码移动到Assert.Throws将解决您的问题。您可以将来自TestCase的预期消息作为常规参数传递。我将简化为可读性;
[TestCase("27/04/2025", "Holiday cannot start or end on a weekend or non-working day")]
public void AddHolidays_Exceptions(string date, string expectedMessage)
{
Assert.That(() => ParseDate(date), Throws.ArgumentException.With.Message.EqualTo(expectedMessage));
}发布于 2016-05-05 13:30:55
假设您继续使用NUnit 2.x,只需使用TestCaseAttribute的ExpectedException属性即可。
https://stackoverflow.com/questions/37045031
复制相似问题