使用Assert.ThrowsExactly或Assert.ThrowsExactlyAsync而不是Assert.ThrowsException或Assert.ThrowsExceptionAsync。 何时禁止显示警告 不禁止显示此规则发出的警告。 强烈建议从旧 API 迁移到新 API。 在GitHub 上与我们协作 可以在 GitHub 上找到此内容的源,还可以在其中创建和查看问题和拉取请求。 有关详细信息,请参...
MSTestで例外をテストする(Assert.ThrowsException) 最新のMSTestのバージョンではAssert.ThrowsExceptionを使うのが良いようです。 [TestMethod]publicvoidTestInvalidUserNameLengthMin(){varuserFactory=newInMemoryUserFactory();varuserRepository=newInMemoryUserRepository();varuserService=newUserService(userRepos...
/// Like Assert.Throws in NUnit. /// /// The action to execute /// The error message if the expected exception is not thrown /// <returns>The exception thrown by the action</returns> publicstaticT Throws<T>(Action action,stringmessage)whereT : Exception { try { action(); } catc...
public static T Throws<T>(Action action) where T : Exception { return Throws<T>(action, null); } /// /// Executes an exception, expecting an exception of a specific type to be thrown. /// Like Assert.Throws in NUnit. /// /// The action to execute /// The error message if...
MSTEST0004 PublicTypeShouldBeTestClassAnalyzer 在测试项目中最好只将测试类标记为公共。 MSTEST0006 AvoidExpectedExceptionAttributeAnalyzer 首选Assert.ThrowsException 或Assert.ThrowsExceptionAsync 而非[ExpectedException],这样可确保只有预期的调用引发预期的异常。 断言 API 还提供更大的灵活性,且允许断言异常的额外...
模拟StackOverflowException:在测试方法中,通过模拟递归调用或无限循环的情况,触发StackOverflowException。 捕获异常并断言:使用MSTest的断言方法,捕获并验证是否抛出了StackOverflowException。例如,可以使用Assert.ThrowsException方法来断言是否抛出了指定类型的异常。 执行测试:在Visual Studio中执行测试,查看测试结果是否...
ExpectedException属性定义测试方法应引发的异常。 如果引发预期的异常并且异常消息与预期消息匹配,则测试通过。 警告 保留此属性是为了后向兼容,不建议用于新测试。 请改用Assert.ThrowsException方法(如果使用 MSTest 3.8 及更高版本,请改用Assert.ThrowsExactly)。
/// Like Assert.Throws in NUnit. /// /// The action to execute /// <returns>The exception thrown by the action</returns> public static Exception Throws(Action action) { return Throws(action, null); } /// /// Executes an exception, expecting...
Assert.AreEqual(sum, n1+n2); } 也可以指定多组参数,测试就会执行多次用例。在MS TEST V2的版本中,还可以通过ITestDataSource接口实现自定义数据源。 异常测试: 异常下现在也和xunit一样采用断言的方式捕获了。 publicvoidTestException() { Assert.ThrowsException<InvalidOperationException>(() =>foo());void...
public void TestException() { Assert.ThrowsException<InvalidOperationException>(() => foo()); void foo() { throw new InvalidOperationException(); } } 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 扩展: 在MS TEST V2中,微软提供了一定的扩展支持,如下图所示(这些扩展也大部分支持MS TEST V1) ...