This is a follow up from Grabbing the output sent to Console.Out from within a unit test? and the referenced article in the accepted answer by Mark Seemann.
I would like
Are there any major drawbacks of using setup or teardown methods for this?
Yes, although it may not be measurable.
The way Setup
and TearDown
method is described here, two new disposable objects are created for every test, but they are never disposed of. They'll eventually go out of scope and be finalised when the garbage collector runs, but it may happen in a less deterministic fashion. In theory, it'll use more memory and processor instructions than if they were deterministically disposed of, but as Knuth taught us 40 years ago, we should measure instead of engaging in premature optimisations.
My main problem with using mutable state and Implicit Setup and Teardown is that it's not thread-safe, so if you ever want to run your unit tests in parallel, you can't.