Request for comments : @BeforeClass and @Factory

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Request for comments : @BeforeClass and @Factory

Cédric Beust ♔
Hi everyone,

Alexandru recently came across a case we can't really find a good behavior for.

Consider:

class AA {
  @BeforeClass
  public void beforeClass() {}
  
  @Test
  public void f() {}
  
  @AfterClass
  public void afterClass() {}
}

public class MultipleFactoryTest {
  
  @Factory
  public Object[] create() {
    return new Object[] {
        new AA(),
        new AA(),
    };
  }

}

Questions:
  1. Should the @BeforeClass/@AfterClass methods be invoked once or twice?
  2. On which instance(s) should these methods be invoked?

--
Cédric

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/testng-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Request for comments : @BeforeClass and @Factory

Alexandru Popescu ☀
On 2/13/07, Cédric Beust ♔ <[hidden email]> wrote:

> Hi everyone,
>
> Alexandru recently came across a case we can't really find a good behavior
> for.
>
> Consider:
>
> class AA {
>   @BeforeClass
>   public void beforeClass() {}
>
>   @Test
>   public void f() {}
>
>   @AfterClass
>   public void afterClass() {}
> }
>
> public class MultipleFactoryTest {
>
>   @Factory
>   public Object[] create() {
>     return new Object[] {
>         new AA(),
>         new AA(),
>     };
>   }
>
> }
>
> Questions:
>
> Should the @BeforeClass/@AfterClass methods be invoked once or twice?
> On which instance(s) should these methods be invoked?


As a small clarification: these question have a root in a possible bug
report posted on our ml by Bogdan Calmac.

Now regarding the real question: the scenario seems tricky mainly
because of the name of the annotations: @BeforeClass and @AfterClass
which may lead you to think that those classes should be invoked once
per Class. But this is not true in TestNG behavior: the @BeforeClass
are methods that are guaranteed to be invoked before all test methods
in the instance.

Imagine the following scenario:

class A {
  @BeforeClass
  public void parentBeforeClass() {}
}

class B extends A {}

class C extends A {}

if we are running both B and C then parentBeforeClass will be invoked
twice, once for B and once for C test instances.

IMO the behavior for @Factory should be the same: the @BeforeClass
methods should be invoked for each instance.

./alex
--
.w( the_mindstorm )p.
  TestNG co-founder
EclipseTestNG Creator


> --
> Cédric
>
>  >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/testng-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: Request for comments : @BeforeClass and @Factory

Cédric Beust ♔
Yup, confirmed. 

I thought I had implemented the same safeguard for @BeforeClass that I did for @BeforeSuite (which are only invoked once even if they appear several times because of inheritance), but it appears not.

--
Cedric

On 2/13/07, Alexandru Popescu <[hidden email]> wrote:
> On 2/13/07, Cédric Beust ♔ <[hidden email]> wrote:
> > Hi everyone,
> >
> > Alexandru recently came across a case we can't really find a good behavior
> > for.
> >
> > Consider:
> >

> > class AA {
> >   @BeforeClass
> >   public void beforeClass() {}
> >
> >   @Test
> >   public void f() {}
> >
> >   @AfterClass
> >   public void afterClass() {}
> > }
> >
> > public class MultipleFactoryTest {
> >
> >   @Factory
> >   public Object[] create() {
> >     return new Object[] {
> >         new AA(),
> >         new AA(),
> >     };
> >   }
> >
> > }
> >
> > Questions:
> >
> > Should the @BeforeClass/@AfterClass methods be invoked once or twice?
> > On which instance(s) should these methods be invoked?
>
>
> As a small clarification: these question have a root in a possible bug
> report posted on our ml by Bogdan Calmac.
>
> Now regarding the real question: the scenario seems tricky mainly
> because of the name of the annotations: @BeforeClass and @AfterClass
> which may lead you to think that those classes should be invoked once
> per Class. But this is not true in TestNG behavior: the @BeforeClass
> are methods that are guaranteed to be invoked before all test methods
> in the instance.
>
> Imagine the following scenario:
>
> class A {
>   @BeforeClass
>   public void parentBeforeClass() {}
> }
>
> class B extends A {}
>
> class C extends A {}
>
> if we are running both B and C then parentBeforeClass will be invoked
> twice, once for B and once for C test instances.
>
> IMO the behavior for @Factory should be the same: the @BeforeClass
> methods should be invoked for each instance.
>
> ./alex
> --

> .w( the_mindstorm )p.
>   TestNG co-founder
> EclipseTestNG Creator
>
>
> > --
> > Cédric
> >
> >  >
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-dev" group.
To post to this group, send email to [hidden email]
To unsubscribe from this group, send email to [hidden email]
For more options, visit this group at http://groups.google.com/group/testng-dev?hl=en
-~----------~----~----~----~------~----~------~--~---