What's the best way to avoid try...catch...finally... in my unit tests?

Posted by Bruce Li on Stack Overflow See other posts from Stack Overflow or by Bruce Li
Published on 2011-11-23T22:48:14Z Indexed on 2011/11/24 9:53 UTC
Read the original article Hit count: 219

Filed under:
|
|

I'm writing many unit tests in VS 2010 with Microsoft Test. In each test class I have many test methods similar to below:

[TestMethod]
public void This_is_a_Test()
{
  try
  {
    // do some test here
    // assert
  }
  catch (Exception ex)
  {
    // test failed, log error message in my log file and make the test fail
  }
  finally
  {
    // do some cleanup with different parameters
  }
}

When each test method looks like this I fell it's kind of ugly. But so far I haven't found a good solution to make my test code more clean, especially the cleanup code in the finally block. Could someone here give me some advices on this?

Thanks in advance.

© Stack Overflow or respective owner

Related posts about unit-testing

Related posts about coding-style