Mstest implements assert. throws in a unit test like nunit.

Source: Internet
Author: User

In the unit test NUnit, we have an asserted Assert. Throws which is very useful, but when we use MsTest, you need to write it like this:
Copy codeThe Code is as follows:
[TestMethod]
[ExpectedException (typeof (ArgumentNullException)]
Public void WriteToTextFile ()
{
PDFUtility. WriteToTextFile ("D :\\ ACA.pdf", null );
}

Now let's extend the implementation to a similar success, and add a class. The Code is as follows:
Copy codeThe Code is as follows:
/// <Summary>
/// Useful assertions for actions that are expected to throw an exception.
/// </Summary>
Public static class ExceptionAssert
{
/// <Summary>
/// Executes an exception, expecting an 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 an 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 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 end up here, the expected exception was not thrown. Fail!
Throw new AssertFailedException (message ?? "Expected exception was not thrown .");
}

/// <Summary>
/// Executes an exception, expecting an exception of a specific type 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 T Throws <T> (Action action) where T: Exception
{
Return Throws <T> (action, null );
}

/// <Summary>
/// Executes an exception, expecting an exception of a specific type 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 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 end up here, the expected exception of type 't'was not thrown. Fail!
Throw new AssertFailedException (message ?? String. Format ("Expected exception of type {0} not thrown.", typeof (T )));
}
}

Now we can use the following code in MsTest:
Copy codeThe Code is as follows:
[TestMethod]
Public void WriteToTextFile2 ()
{
// Implement Assert. Throws in MSTest
ExceptionAssert. Throws <ArgumentNullException> () => PDFUtility. WriteToTextFile ("D: \ ACA.pdf", null)
, "Output file path shocould not be null ");
}
 

Related Article

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.