Constructor vs. [Setup]

2,660 views
Skip to first unread message

Mark Levison

unread,
Mar 26, 2009, 2:31:08 PM3/26/09
to nunit-...@googlegroups.com
I searched the documentation and also the mailing list - I seem to have a mental block today.

Is it better to use a [Setup] attribute over a constructor?

If so why? Better exception reporting/handling? Symmetry with the Teardown?

I ask because I find the use of initializers so much more compact than the typical setup block and before I start introducing a new team to my habits I thought I would check my assumptions.

Cheers
Mark Levison

Blog: http://www.notesfromatooluser.com/
Recent Entries: Agile/Scrum Smells:  http://www.notesfromatooluser.com/2008/06/agilescrum-smells.html
Agile Games for Making Retrospectives Interesting: http://www.notesfromatooluser.com/2008/10/agile-games-for-making-retrospectives-interesting.html

Chris Wright

unread,
Mar 26, 2009, 2:45:55 PM3/26/09
to nunit-...@googlegroups.com
2009/3/26 Mark Levison <ma...@mlevison.com>:

> Is it better to use a [Setup] attribute over a constructor?

Yes. There are no guarantees about how often or when a constructor for
a test fixture will be invoked; there are guarantees for how often and
when a setup method will be invoked. Using the constructor leaves the
possibility that your tests will share state. Most people think that
this is a bad thing.

If your tests are guaranteed not to modify that data, it doesn't
matter, but in C#, your only guarantees of that are readonly
primitives.

I believe I saw this mentioned on the list a few days back, but my
memory may be failing me. At any rate, it would have been buried in a
post on a different topic.

Fabio Maulo

unread,
Mar 26, 2009, 2:48:00 PM3/26/09
to NUnit-Discuss
For SetUp/Teardown I prefer to call a specific method in each test.
What I saw is that, in many cases, people are using [SetUp] but each
test are using only a part of that context.
The SetUp should be used to define the real common-context of each
test.

Mark Levison ha escrito:

Charlie Poole

unread,
Mar 26, 2009, 2:49:19 PM3/26/09
to nunit-...@googlegroups.com
Hi Mark,

> Is it better to use a [Setup] attribute over a constructor?

Yes. Or more precisely to use [SetUp] or [TestFixtureSetUp],
depending on the desired semantics.

> If so why? Better exception reporting/handling? Symmetry with
> the Teardown?

Those are two reasons, but not the most important:

* NUnit may need to construct your class more than once.
For example, this happens when you use a method of the
same class to generate the tests.

* NUnit provides no special guarantee of the lifetime
of objects of your fixture type. It could (in future)
cache them and reuse them at will.

* Currently, NUnit only constructs one fixture object for
use with each test method, so the proper comparison is
really between constructor and [TestFixtureSetUp]

> I ask because I find the use of initializers so much more
> compact than the typical setup block and before I start
> introducing a new team to my habits I thought I would check
> my assumptions.

I agree, and I would like to work toward a version where
this very natural approach doesn't cause problems. But
that's not so at present. :-(

My own experience is that the problems, while rare, are
incredibly time-consuming when they occur in a team. I
guess that's precisely because it "seems" that the
constructor could do the job.

If you wanted to teach use of the constructor for
initialization, you would also have to provide the
team with a set of exception cases, for which they
must not use it. I have learned that this approach
confuses them even more. :-)

Charlie

Fabio Maulo

unread,
Mar 26, 2009, 2:49:50 PM3/26/09
to NUnit-Discuss
Constructor ?
Mark, are you talking about [Setup] or [TestFixtureSetUp] ?

Chris Wright ha escrito:
Reply all
Reply to author
Forward
0 new messages