You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-user@jakarta.apache.org by Jason Novotny <no...@aei.mpg.de> on 2003/09/09 17:41:30 UTC

testing and subclassing

Hi,

    I'm trying to use Cactus/Junit to test my scenario. I have one test 
that simply just checks that it can instantiate and init my servlet 
object. The next test then would use this method in its setUp() method 
before testing methods for trying to log a root user in from the 
database. Next I need a TestSetupUsers class that woudl then subclass to 
use the initialization of the base test and the testLoginRootUser test 
of the SetupRootUserTest. So what I tried doing initially was something 
like this:

    MyServletTest
      
          testInitServlet()

    TestSetupRoot extends MyServletTest
      
          setUp() {
             // uses super.testInitServlet()
          }
   
          testLoginRotUser()

    TestSetupUsers extends TestSetupRoot

       setUp() {
          // uses super.setUp()and super.testInitServlet()
       }
   
       testCreateUsers()

Now, I look thru and I see these tests are no good because of the 
subclassing. The TestSetupUsers is also testing all the test* methods of 
the superclasses. I feel like what I'm doing must be somewhat common to 
other projects-- I would be very interested in any discussions of how to 
make unit testing dependencies manageable.

    Thanks, Jason

      


RE: testing and subclassing

Posted by Vincent Massol <vm...@pivolis.com>.
Short answer: you shouldn't :-)

It's either a fixture or it's a test but it should not be both. Fixtures
go in setUp() and test in testXXX() methods.

Let's imagine you have:

testXXX()
{
   A
   B
}

testYYY()
{
   A
   C
}

A is common to both. So you would refactor to:

setUp()
{
   A
}

testXXX()
{
   B
}

testYYY()
{
   C
}

AFAIK, this is the canonical way to share common code between test.

-Vincent

> -----Original Message-----
> From: Jason Novotny [mailto:novotny@aei.mpg.de]
> Sent: 09 September 2003 18:34
> To: Cactus Users List
> Subject: Re: testing and subclassing
> 
> 
> Hi Vinent,
> 
>     Thanks for the response-- I guess what I'm asking is how can I
reuse
> test code from one test as a fixture for another test in my test
suite?
> 
>     Thanks, Jason
> 
> Vincent Massol wrote:
> 
> >Jason,
> >
> >I don't think I quite understand. In any case:
> >
> >- if you want to group test together, you should use a junit test
suite
> >(see the junit website)
> >- each test must be independent of the other. Only the fixture can be
> >shared through setUp. Tests that share the same fixture are grouped
> >together in a TestCase
> >
> >-Vincent
> >
> >
> >
> >>-----Original Message-----
> >>From: Jason Novotny [mailto:novotny@aei.mpg.de]
> >>Sent: 09 September 2003 17:42
> >>To: cactus-user@jakarta.apache.org
> >>Subject: testing and subclassing
> >>
> >>
> >>Hi,
> >>
> >>    I'm trying to use Cactus/Junit to test my scenario. I have one
> >>
> >>
> >test
> >
> >
> >>that simply just checks that it can instantiate and init my servlet
> >>object. The next test then would use this method in its setUp()
method
> >>before testing methods for trying to log a root user in from the
> >>database. Next I need a TestSetupUsers class that woudl then
subclass
> >>
> >>
> >to
> >
> >
> >>use the initialization of the base test and the testLoginRootUser
test
> >>of the SetupRootUserTest. So what I tried doing initially was
> >>
> >>
> >something
> >
> >
> >>like this:
> >>
> >>    MyServletTest
> >>
> >>          testInitServlet()
> >>
> >>    TestSetupRoot extends MyServletTest
> >>
> >>          setUp() {
> >>             // uses super.testInitServlet()
> >>          }
> >>
> >>          testLoginRotUser()
> >>
> >>    TestSetupUsers extends TestSetupRoot
> >>
> >>       setUp() {
> >>          // uses super.setUp()and super.testInitServlet()
> >>       }
> >>
> >>       testCreateUsers()
> >>
> >>Now, I look thru and I see these tests are no good because of the
> >>subclassing. The TestSetupUsers is also testing all the test*
methods
> >>
> >>
> >of
> >
> >
> >>the superclasses. I feel like what I'm doing must be somewhat common
> >>
> >>
> >to
> >
> >
> >>other projects-- I would be very interested in any discussions of
how
> >>
> >>
> >to
> >
> >
> >>make unit testing dependencies manageable.
> >>
> >>    Thanks, Jason
> >>
> >>
> >>
> >>
>
>>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> >>For additional commands, e-mail: cactus-user-help@jakarta.apache.org
> >>
> >>
> >
> >
> >
> >---------------------------------------------------------------------
> >To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> >For additional commands, e-mail: cactus-user-help@jakarta.apache.org
> >
> >
> >
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org



Re: testing and subclassing

Posted by Jason Novotny <no...@aei.mpg.de>.
Hi Vinent,

    Thanks for the response-- I guess what I'm asking is how can I reuse 
test code from one test as a fixture for another test in my test suite?
   
    Thanks, Jason

Vincent Massol wrote:

>Jason,
>
>I don't think I quite understand. In any case:
>
>- if you want to group test together, you should use a junit test suite
>(see the junit website)
>- each test must be independent of the other. Only the fixture can be
>shared through setUp. Tests that share the same fixture are grouped
>together in a TestCase
>
>-Vincent
>
>  
>
>>-----Original Message-----
>>From: Jason Novotny [mailto:novotny@aei.mpg.de]
>>Sent: 09 September 2003 17:42
>>To: cactus-user@jakarta.apache.org
>>Subject: testing and subclassing
>>
>>
>>Hi,
>>
>>    I'm trying to use Cactus/Junit to test my scenario. I have one
>>    
>>
>test
>  
>
>>that simply just checks that it can instantiate and init my servlet
>>object. The next test then would use this method in its setUp() method
>>before testing methods for trying to log a root user in from the
>>database. Next I need a TestSetupUsers class that woudl then subclass
>>    
>>
>to
>  
>
>>use the initialization of the base test and the testLoginRootUser test
>>of the SetupRootUserTest. So what I tried doing initially was
>>    
>>
>something
>  
>
>>like this:
>>
>>    MyServletTest
>>
>>          testInitServlet()
>>
>>    TestSetupRoot extends MyServletTest
>>
>>          setUp() {
>>             // uses super.testInitServlet()
>>          }
>>
>>          testLoginRotUser()
>>
>>    TestSetupUsers extends TestSetupRoot
>>
>>       setUp() {
>>          // uses super.setUp()and super.testInitServlet()
>>       }
>>
>>       testCreateUsers()
>>
>>Now, I look thru and I see these tests are no good because of the
>>subclassing. The TestSetupUsers is also testing all the test* methods
>>    
>>
>of
>  
>
>>the superclasses. I feel like what I'm doing must be somewhat common
>>    
>>
>to
>  
>
>>other projects-- I would be very interested in any discussions of how
>>    
>>
>to
>  
>
>>make unit testing dependencies manageable.
>>
>>    Thanks, Jason
>>
>>
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: cactus-user-help@jakarta.apache.org
>>    
>>
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: cactus-user-help@jakarta.apache.org
>
>  
>



RE: testing and subclassing

Posted by Vincent Massol <vm...@pivolis.com>.
Jason,

I don't think I quite understand. In any case:

- if you want to group test together, you should use a junit test suite
(see the junit website)
- each test must be independent of the other. Only the fixture can be
shared through setUp. Tests that share the same fixture are grouped
together in a TestCase

-Vincent

> -----Original Message-----
> From: Jason Novotny [mailto:novotny@aei.mpg.de]
> Sent: 09 September 2003 17:42
> To: cactus-user@jakarta.apache.org
> Subject: testing and subclassing
> 
> 
> Hi,
> 
>     I'm trying to use Cactus/Junit to test my scenario. I have one
test
> that simply just checks that it can instantiate and init my servlet
> object. The next test then would use this method in its setUp() method
> before testing methods for trying to log a root user in from the
> database. Next I need a TestSetupUsers class that woudl then subclass
to
> use the initialization of the base test and the testLoginRootUser test
> of the SetupRootUserTest. So what I tried doing initially was
something
> like this:
> 
>     MyServletTest
> 
>           testInitServlet()
> 
>     TestSetupRoot extends MyServletTest
> 
>           setUp() {
>              // uses super.testInitServlet()
>           }
> 
>           testLoginRotUser()
> 
>     TestSetupUsers extends TestSetupRoot
> 
>        setUp() {
>           // uses super.setUp()and super.testInitServlet()
>        }
> 
>        testCreateUsers()
> 
> Now, I look thru and I see these tests are no good because of the
> subclassing. The TestSetupUsers is also testing all the test* methods
of
> the superclasses. I feel like what I'm doing must be somewhat common
to
> other projects-- I would be very interested in any discussions of how
to
> make unit testing dependencies manageable.
> 
>     Thanks, Jason
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cactus-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: cactus-user-help@jakarta.apache.org