The Artima Developer Community
Sponsored Link

SuiteRunner Forum
Suite Destructor?

6 replies on 1 page. Most recent reply: Aug 27, 2003 9:59 PM by Bill Venners

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 6 replies on 1 page
John Smith

Posts: 5
Nickname: jeksmith
Registered: Jan, 2003

Suite Destructor? Posted: Aug 11, 2003 9:11 AM
Reply to this message Reply
Advertisement
One of the reasons I chose to use SuiteRunner over JUnit is the ability to setup state once for multiple tests. The constructor of a Suite handles this nicely. But, alas, it seems there is no Suite destructor. The idea is to be able to close a connection (or some other clean up action) after all of the tests have completed or if any of the tests were aborted (due to some unrecoverable error). I realize I can override executeTestMethods to do this, but I thought I'd ask in case I missed the functionality or if it is a bad idea for reasons I haven't thought of.


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Suite Destructor? Posted: Aug 22, 2003 4:48 AM
Reply to this message Reply
I suspect that Bill might argue that your code being tested or your test code itself should handle the exceptions. For example,

public void testSomething() {
  try {
    // call some unit of code to be tested
  }
  catch( Exception e ) {
    // catch the exception
  }
  finally {
    // clean up by closing connections, etc
  }
}


Adam

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Suite Destructor? Posted: Aug 22, 2003 4:50 AM
Reply to this message Reply
Apologies. That should read

public void testSomething() {
  try {
    // call some unit of code to be tested
 
  catch( Exception e ) {
    // catch the exception
    verify( false );
  }
  finally {
    // clean up by closing connections, etc
  }
}


Note the inclusion of the verify( false ); statement.

Adam

John Smith

Posts: 5
Nickname: jeksmith
Registered: Jan, 2003

Re: Suite Destructor? Posted: Aug 25, 2003 2:11 PM
Reply to this message Reply
Adam, I think you misunderstood. In the Suite's constructor, I am creating and connecting to a server under test. So, when the tests are complete (failed or success or aborted), the connection to the server needs to be destroyed.

Here is my code in the overridden suite:

/** invokes destroySuite after executing all of the tests
 *
 * @param reporter
 */
protected void executeTestMethods( Reporter reporter )
{
    boolean exceptionThrownDuringTestMethods = true;
    try
    {
        super.executeTestMethods( reporter );
        exceptionThrownDuringTestMethods = false;
    } finally
    {
        try
        {
            destroySuite();
        } catch ( Exception e )
        {
            // don't want to lose evidence of exception in super.executeTestMethods;
            if ( !exceptionThrownDuringTestMethods )
            {
                // wrap Exception in RTE, since executeTestMethods doesn't throw Exception
                throw new RuntimeException( e );
            }
        }
    }
}
 
/**
 * destroySuite() is invoked after all of the tests have been executed.
 * Override it to clean-up your Suite-level state
 */
protected void destroySuite() throws Exception
{
}
 

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Suite Destructor? Posted: Aug 25, 2003 5:00 PM
Reply to this message Reply
Hi John,

Thanks for your post. I'm not sure I thought of this case. Would it work for you to simply create an outermost Suite whose constructor adds subsuites to itself. In that outermost Suite, override execute by simply grabbing the resource, calling super.execute(), and then freeing the resource? The resource would then be grabbed before each run, and freed after each run.

If this needs to be done on a Suite by Suite basis, I believe you could use the above Suite as a superclass of all your actual Suites.

Would either of these approaches work for you? Is there something else you'd like to see? I suspect you want a method that is called after execute that you can override, is that correct?

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Suite Destructor? Posted: Aug 27, 2003 3:38 AM
Reply to this message Reply
> Adam, I think you misunderstood.

Looks like I did misunderstand John. My apologies. I see Bill is offering to take this into account in a future version of SuiteRunner.

Adam

Bill Venners

Posts: 2284
Nickname: bv
Registered: Jan, 2002

Re: Suite Destructor? Posted: Aug 27, 2003 9:59 PM
Reply to this message Reply
> > Adam, I think you misunderstood.
>
> Looks like I did misunderstand John. My apologies. I see
> Bill is offering to take this into account in a future
> version of SuiteRunner.
>
Well, I'm not sure it rises to the level of needing to be added to the API, because then everyone would have to look at it and understand it, and I don't think most people need it in most cases.

I did notice today that the new NUnit has attributes that mark methods that will be executed at the beginning and end running each TestCase. They mention that some fixtures are too expensive to create before each test. The same functionality could be achieved in SuiteRunner by simply creating the fixture once in the constructor, overriding execute, doing:


try {
super.execute(reporter)
}
finally {
callYourCleanupMethod()
}


Unless most people want to do this, it doesn't carry its weight to be pulled up into the API, since it can be so easily accomplished in a Suite subclass.

Flat View: This topic has 6 replies on 1 page
Topic: VC++ compiler for eclipse Previous Topic   Next Topic Topic: customizing the suiterunner

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use