views:

1068

answers:

4

This question regards unit testing in Visual Studio using MSTest (this is important, because of MSTest's execution order). Both the method marked [TestInitialize] and the test class constructor will run before each test method.

So, the question is, what do you tend to do in each of these areas? Do you avoid performing certain activities in either? What is your reason: style, technical, superstition?

+1  A: 

The object you test doesn't need to be instancied in the [TestInitialize] method. You can test the constructor of your object in a test method [Test].

Object in the [TestInitialize] can be to setup your persistance storage or to prepare value that the object tested will used in the tests.

Daok
I know this. You can instantiate the object inline with the declaration if possible. My question is what do _you_ do in either of those places. And why?
Anthony Mastrean
I answered that question in my answer... read again.
Daok
A: 

I prefer to use the [TestInitialize] method to perform instantiation of the object being tested and it's parameters. I only perform work in the constructor if it is necessary to instantiate a testing base class (which is usually where I create or refresh repositories, etc). This helps me keep the test framework code and test code separate logically and physically.

Anthony Mastrean
I think if you're going to give a down vote, you should at least comment as to why you gave the down vote.
Seattle Leonard
A: 

Daok's answer addresses testing the construction of the object under test. This is not the question. The original question concerns the test class's constructor, not the constructor of the object under test.

I would add this as a comment to Daok's answer, but I don't have the rep :(

bacar
A: 

This question is also asked (later) at What’s the difference between using the constructor in VS Testing framework vs. TestInitialize() attribute?

FWIW I assume by "class constructor" you mean the instance constructor (not the static constructor).

I believe the same question you are asking could equally be asked about the static constructor vs. ClassInitialize...

bacar