MSTest implements assert.throws functions in similar unit testing NUnit

Source: Internet
Author: User
Tags assert static class

  We do unit test NUnit, there is an assertion assert.throws very good, now we expand the implementation of similar functions, we refer to the use of the bar

We do unit test NUnit, there is an assertion assert.throws very good, but when we use mstest you need to write:   code as follows: [TestMethod] [ExpectedException ( ArgumentNullException)]] public void Writetotextfile () {pdfutility.writetotextfile ("d:aca.pdf", null);}     Now let's extend this to also implement a similar functionality, add a class, the code is as follows:     code as follows:///<summary>///useful assertions for actions that are expected to throw an exception. </summary> public static class Exceptionassert {///<summary>///executes a exception, expecting an exc Eption to be thrown. Like Assert.throws in NUnit. </summary>///<param name= "action" >the action to execute</param>///of <returns>the exception Thrown by the action</returns> public static Exception Throws (Action action) {return Throws (action, null);   <summary>///executes an exception, expecting a exception to be thrown. Like Assert.throws in NUnit. </summary>///<param name= "action" >the action to Execute</param><param name= ' message ' >the error message if the expected exception ' not ' thrown</param>/// ; The exception thrown by the action</returns> public static exception Throws (Action action, string message) {try {a Ction (); The catch (Exception ex) {//The action method has thrown the expected Exception. Test wants to perform further assertions on it. return ex; }  //If We are here, the expected exception is not thrown. fail! throw new Assertfailedexception (message??) "Expected exception is not thrown.");  ///<summary>///executes a exception, expecting a exception of a specific type to is thrown. Like Assert.throws in NUnit. </summary>///<param name= "action" >the action to execute</param>///of <returns>the exception Thrown by the action</returns> public static T throws<t> (Action action) where t:exception {return throws&lt ; T> (Action, NULL);  ///<summary>///executes a exception, expecting a exception of a specific type to is thrown. Like Assert.throws in NUnit. </summary>///<param name= "action" >the action to execute</param>///the <param "message" > The error message if the expected exception isn't thrown</param>///<returns>the exception by the Act ion</returns> public static T throws<t> (Action action, string message) where T:exception {try {action ();} catch (Exception ex) {T actual = ex as T; if (actual = null) {throw new assertfailedexception (message??) String.Format ("expected exception of type {0} not thrown.") Actual exception type was {1}. ", typeof (T), ex. GetType ())); }  //The action method has thrown the expected exception of type ' T '. Return the exception, in case the "unit" Test wants to perform further assertions on it. return actual; }  //If We are here, the expected exception of type ' T ' is not throWn. fail! throw new Assertfailedexception (message??) String.Format ("expected exception of type {0} not thrown.", typeof (T)));   OK, now we can do this in MSTest, look at the following code:   Code as follows: [TestMethod]  public void WriteToTextFile2 () {//implement Assert . Throws in MSTest exceptionassert.throws<argumentnullexception> (() => pdfutility.writetotextfile ("D:ACA.pdf ", null)  ," Output file path should not is null ");  }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.