简体   繁体   中英

Is ExpectedException used for testing more than one cases in one test method?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM