MSTest implement Assert.throws function in similar unit test NUnit

Source: Internet
Author: User
Tags assert static class

We do unit test NUnit, there's an assertion that assert.throws works, but when we use mstest you need to write this:

Copy Code code as follows:

[TestMethod]
[ExpectedException (typeof (ArgumentNullException))]
public void Writetotextfile ()
{
Pdfutility.writetotextfile ("d:\\aca.pdf", null);
}

Now let's expand it and implement a similar function, adding a class with the following code:

Copy Code code as follows:

<summary>
Useful assertions for the actions that is are expected to throw a exception.
</summary>
public static Class Exceptionassert
{
<summary>
Executes an exception, expecting a exception to be thrown.
Like Assert.throws in NUnit.
</summary>
<param name= "Action" >the action to Execute</param>
<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 ' expected exception is not thrown</param>
<returns>the exception thrown by the Action</returns>
public static Exception Throws (Action action, string message)
{
Try
{
Action ();
}
catch (Exception ex)
{
The action method has thrown the expected exception.
Return the exception, in case the "unit" Test wants to perform further assertions on it.
return ex;
}

If we have here, the expected exception is not thrown. fail!
throw new Assertfailedexception (message??) "Expected exception is not thrown.");
}

<summary>
Executes an 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>
<returns>the exception thrown by the Action</returns>
public static T throws<t> (Action action) where t:exception
{
Return throws<t> (action, NULL);
}

<summary>
Executes an 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>
<param name= ' message ' >the error message if ' expected exception is not thrown</param>
<returns>the exception thrown by the Action</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 have 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:
Copy 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 being 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.