NUnit 3.0 and Assert.Throws

C#Unit TestingNunitNunit 3.0

C# Problem Overview


I am writing some unit tests with NUnit 3.0 and, unlike v2.x, ExpectedException() has been removed from the library.

Based on this answer, I can definitely see the logic in trying to catch specifically where in the test one expects their system to throw an exception (rather than just saying 'anywhere in the test').

However, I tend to be very explicit about my Arrange, Act, and Assert steps and this makes it a challenge.

I used to do something like:

[Test, ExpectedException(typeof(FormatException))]
public void Should_not_convert_from_prinergy_date_time_sample1()
{
	//Arrange
	string testDate = "20121123120122";

	//Act
	testDate.FromPrinergyDateTime();

	//Assert
	Assert.Fail("FromPrinergyDateTime should throw an exception parsing invalid input.");
}

Now I need to do something like:

[Test]
public void Should_not_convert_from_prinergy_date_time_sample2()
{
	//Arrange
	string testDate = "20121123120122";

	//Act/Assert
	Assert.Throws<FormatException>(() => testDate.FromPrinergyDateTime());
}

This isn't terrible, but muddies the Act and Assert, in my opinion. (Obviously, for this simple test, it's not hard to follow, but might be more challenging in larger tests).

I've had a colleague suggest I get rid of Assert.Throws altogether and just do something like:

[Test]
public void Should_not_convert_from_prinergy_date_time_sample3()
{
	//Arrange
	int exceptions = 0;
	string testDate = "20121123120122";

	//Act
	try
	{
		testDate.FromPrinergyDateTime();
	}
	catch (FormatException) { exceptions++;}

	//Assert
	Assert.AreEqual(1, exceptions);
}

Here, I stick with the strict AAA format, but at the expense of even more bloat.

So my question goes out to AAA-style testers: How would you do some sort of exception validation testing like I am trying to do here?

C# Solutions


Solution 1 - C#

I see where you're coming from, even though I don't mind combining Act/Assert steps in this case.

The only thing I can think of is to store the actual delegate (here to FromPrinergyDateTime) into a variable as the "act" step and then assert it:

[Test]
public void Should_not_convert_from_prinergy_date_time_sample2()
{
    //Arrange
    string testDate = "20121123120122";

    //Act
    ActualValueDelegate<object> testDelegate = () => testDate.FromPrinergyDateTime();

    //Assert
    Assert.That(testDelegate, Throws.TypeOf<FormatException>());
}

I get that the "act" step isn't really acting, but rather defining what the action is. However, it does clearly delineate what action is being tested.

Solution 2 - C#

In C# 7, there is another option (albeit very similar to the existing answers):

[Test]
public void Should_not_convert_from_prinergy_date_time_sample2()
{
    void CheckFunction()
    {
        //Arrange
        string testDate = "20121123120122";

        //Act
        testDate.FromPrinergyDateTime();
    }

    //Assert
    Assert.Throws(typeof(Exception), CheckFunction);
}

Blog post on the subject

Solution 3 - C#

You can create a custom Attribute in NUnit 3. Here is the sample code how to create [ExpectedException] Attribute.(ExpectedExceptionExample Shows how to implement a custom attribute for NUnit) https://github.com/nunit/nunit-csharp-samples

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKillnineView Question on Stackoverflow
Solution 1 - C#Patrick QuirkView Answer on Stackoverflow
Solution 2 - C#Paul MichaelsView Answer on Stackoverflow
Solution 3 - C#Matt AllenView Answer on Stackoverflow