setup and teardown

56 views
Skip to first unread message

Shannon

unread,
Aug 11, 2009, 7:37:23 PM8/11/09
to mxunit
Is there any documentation or samples on how to use these?

In reviewing the source code, it appears that each test within a test
case calls setup and teardown and you can override these on a per test
case basis.

Does mxUnit provide a way to factor out setup and teardown code for an
entire suite?

bill shelton

unread,
Aug 12, 2009, 6:21:20 AM8/12/09
to mxunit
Hi Shannon,

We don't have any explicit documentation regarding setUp() and tearDown
(). This is probably an assumption and omission on our part, as setUp
() and tearDown() are defacto xUnit methods and behave the same way
(basically) from JUnit to pyUnit and others.

Your understanding is correct with resepect to how setUp() and tearDown
() are called. Personally, I view these more as events associated with
running a test. So, MXUnit first instantiates a TestCase, then setUp()
is called, your test method, then tearDown(). This happens for each
test in your test case. setUp() is empty in TestCase so you are
expected to override these methods if you intend to use them.
Otherwise they can safely be omitted and have no impact on your test
(other than a nominal performance hit, adding an empty operation).

The whole idea behind this is that you want your object under test to
be in a known state when testing it. So, setUp() will typically
initialize the object and tearDown() will clean it up, as the test may
have altered the object's state. Example:

function setUp(){
bill = createObject('component','User').init();
bill.setName('Bill');
bill.setEmail('bi...@mxunit.org');
}

... run test

function tearDown(){
bill = '';
}


I'm not sure what you mean by factor out setUp() and tearDown() for a
test suite. Do you want setUp and tearDown not to run at certain
times? Or maybe you want more control on how your object's state is
maintained at the suite level?

best,
bill

Shannon

unread,
Aug 18, 2009, 4:13:10 PM8/18/09
to mxunit
Bill,

Thanks for getting back to me! (sorry I'm so slow to get back to this
post ... I got tied up on another project)

The latter ... as in, I want to run certain code only once when I
execute a test suite. For example, I may have broken related test
cases into into several smaller ones for manageability, but I may need
to run some shared setup for all. I only want to run it once because
it eats a lot of resources and takes a lot of time to execute the
setup and the teardown so I definitely don't want to do it for every
case. It's been awhile since I've used JUnit, but I think you could
do this in JUnit.

I think I've figured out the simple answer. I can just code it
directly into the test suite to execute before and after the call to
run(). I'm still a CF newbie and a bit code rusty, but once I grasp
scoping of variables, I should be OK.

But the better answer may be mocking. I've never used mocking, but
I'm thinking that maybe the things I could think of to do for an
entire suite may be best handled by mocks. I'm reading up on it today
and hope to have some examples together by COB.

I understand the assumption/omission part ... My shop has never
touched unit testing before and most are not versed in Java or python,
so I'm hoping to find existing ColdFusion examples to give them so as
to make the adoption of unit testing as easy as possible! Where they
don't exist or don't seem to be well enough explained, I'm creating
them ... so let's hope I get it right!

Marc Esher

unread,
Aug 18, 2009, 8:05:02 PM8/18/09
to mxu...@googlegroups.com
Shannon,
  I typically add once-and-done code into my TestCase itself, but I think what you're referring to is something we do intend to add into MXUnit: a beforeClass() function that executes only once for the entire testcase.

Bill..... can you have that in by tomorrow?  :-)

Marc

bill shelton

unread,
Aug 19, 2009, 10:32:12 AM8/19/09
to mxunit
Yeah, we've been talking for some time about TestSuite level
annotations. It would be nice to have before and after suite event
handlers ... we'll get there. In the meantime, as Marc suggests, you
can just write some initialization code in the suite.

bill

On Aug 18, 8:05 pm, Marc Esher <marc.es...@gmail.com> wrote:
> Shannon,
>   I typically add once-and-done code into my TestCase itself, but I think
> what you're referring to is something we do intend to add into MXUnit: a
> beforeClass() function that executes only once for the entire testcase.
>
> Bill..... can you have that in by tomorrow?  :-)
>
> Marc
>

Shannon

unread,
Aug 20, 2009, 5:09:44 PM8/20/09
to mxunit
Bill and Marc,

Thanks. Will do! :)
Reply all
Reply to author
Forward
0 new messages