我想要一个TestMethod用于多个异常.问题是Testmethod在第一次抛出异常后停止.
我知道我可以这样做:
try { sAbc.ToInteger(); Assert.Fail(); // If it gets to this line,no exception was thrown } catch (ArgumentException) { }
但我想使用以下代码库:
[TestMethod,ExpectedException(typeof(ArgumentException),"...")] public void StringToIntException() { sAbc.ToInteger(); // throws an exception and stops here sDecimal.ToInteger(); // throws theoretically a exception too... }
而且我不想为每个可能的异常创建一个testmethod:
[TestMethod,"...")] public void StringToIntException() { sAbc.ToInteger(); } [TestMethod,"...")] public void StringToIntException() { sDecimal.ToInteger(); }
编辑2018-11-09:
今天,这将基于Jan David Narkiewicz的建议.但正如我已经提到的那样.从我今天的观点来看,这对于测试来说是一个糟糕的设计.例:
[TestMethod] public void ExceptionTest() { Assert.ThrowsException <FormatException> (() => { int.Parse("abc"); }); Assert.AreEqual(0.1m,decimal.Parse("0.1",CultureInfo.InvariantCulture)); Assert.ThrowsException<FormatException>(() => { decimal.Parse("0.1",new NumberFormatInfo { NumberDecimalSeparator = "," }); }); }