0

I am writing unit tests on each of several methods MyMethod1, MyMethod2, ... For each method, I would like to test a case when it raises a particular exception and a case when it doesn't.

What I do now is to group the two cases for each method into a test method:

[TestMethod]
[TestCategory("Unit")]
public void MyTest()
{     
    SetupExceptionCase();
    try
    {
        MyMethod1();
        Assert.Fail();
    }
    catch (InvalidDataException) { }

    SetupNonExceptionCase();
    try
    {
        MyMethod1();
    }
    catch (InvalidDataException)
    {
        Assert.Fail();
    }
}

If I would like to use ExpectedException attribute to replace the more lengthy try...catch... control, as suggested in https://stackoverflow.com/a/933627/156458,

  • do I need to split my test method into two test methods, each for a case?

  • If yes, how can I use the attribute for the case where I don't expect my method to raise an exception?

  • Is there any way that I don't have to split it, because grouping two cases for the same method to be tested in a test method separates nicely the tests for different methods to be tested?

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590

2 Answers2

2

Your unit test is actually testing two different cases, so logically it should be split out anyway. But in regards to using the out of the box attribute, it is applied to the method so yes, you would need to split them out (I'd recommend that anyway).

If yes, how can I use the attribute for the case where I don't expect my method to raise an exception?

If you are not expecting your method to raise an exception then do not use the attribute.

Is there any way that I don't have to split it, because grouping two cases for the same method to be tested in a test method separates nicely the tests for different methods to be tested?

It's best practice to split your tests out into units anyway, I'm not sure how you think grouping them is nice but it's not something I would recommend.

Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
  • Thanks. If I don't expect exception, then i don't use the attribute, and then how can I apply assert? – Tim Jun 05 '17 at 13:33
  • @Tim by using the Assert class. If no exception is thrown then it succeeded? If you need to check the behavior of the method then you want to assert that certain properties for example have been set. e.g. `Assert.Equals(property1, property2);` – Jamie Rees Jun 06 '17 at 08:02
1

do I need to split my test method into two test methods, each for a case?

Yes.

If yes, how can I use the attribute for the case where I don't expect my method to raise an exception?

Just don't add the attribute to the method!?

Is there any way that I don't have to split it, because grouping two cases for the same method to be tested in a test method separates nicely the tests for different methods to be tested?

I dont think so, because the attributes needs the exception to be thrown. Therefore the test ends with the first exception. However, it is good to split test in single test cases anyway (as atomic as possible) so that you don't have to waste time with debugging / analyzing what went wrong if a test fails.

JanDotNet
  • 3,746
  • 21
  • 30
  • Thanks. If I don't expect exception, then i don't use the attribute, and then how can I apply assert? – Tim Jun 05 '17 at 13:50