BeforeXxx, AfterXxx, and groups

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

BeforeXxx, AfterXxx, and groups

Matt Welch-2

I'm seeing something that I don't quite understand. I have methods
annotated with:

@BeforeMethod(groups={"group1", "group2"}

and

@BeforeMethod(groups={"group1", "group2"}

I thought that this meant that the methods using these annotations
would only be run when Tests were run int he same groups like:

@Test(groups = {"group1"}
public void group1test(){}

or

@Test(groups = {"group1", "group2"}
public void group1AndGroup2Test(){}

But in reality, the Before and After annotated methods are being run
before every single method and not just the ones in the same group.
Have I misunderstood how groups are supposed to work in this
situation?

-Matt


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: BeforeXxx, AfterXxx, and groups

Cédric Beust ♔
Hi Matt,

To achieve what you are trying to do, you should use @BeforeGroups/@AfterGroups.

As you found out, @BeforeMethod/@AfterMethod are invoked around all test methods, regardless of their group.  The groups @Before/AfterMethod belong to only determine whether they are picked by TestNG or not.

--
Cédric


On 4/13/07, Watter <[hidden email]> wrote:

I'm seeing something that I don't quite understand. I have methods
annotated with:

@BeforeMethod(groups={"group1", "group2"}

and

@BeforeMethod(groups={"group1", "group2"}

I thought that this meant that the methods using these annotations
would only be run when Tests were run int he same groups like:

@Test(groups = {"group1"}
public void group1test(){}

or

@Test(groups = {"group1", "group2"}
public void group1AndGroup2Test(){}

But in reality, the Before and After annotated methods are being run
before every single method and not just the ones in the same group.
Have I misunderstood how groups are supposed to work in this
situation?

-Matt








--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: BeforeXxx, AfterXxx, and groups

Alexandru Popescu ☀
In reply to this post by Matt Welch-2

On 4/13/07, Watter <[hidden email]> wrote:

>
> I'm seeing something that I don't quite understand. I have methods
> annotated with:
>
> @BeforeMethod(groups={"group1", "group2"}
>
> and
>
> @BeforeMethod(groups={"group1", "group2"}
>
> I thought that this meant that the methods using these annotations
> would only be run when Tests were run int he same groups like:
>
> @Test(groups = {"group1"}
> public void group1test(){}
>
> or
>
> @Test(groups = {"group1", "group2"}
> public void group1AndGroup2Test(){}
>
> But in reality, the Before and After annotated methods are being run
> before every single method and not just the ones in the same group.
> Have I misunderstood how groups are supposed to work in this
> situation?
>

Nope, I don't think you've misunderstood 'em. But you will see that
behavior only when running group based configurations.

I assume that you are seeing the above behavior when using generic
configurations (no groups provided in the command line/Ant
task/testng.xml). If you switch to specifying groups then you will
start seeing the behavior you are describing.

bests,

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

> -Matt
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: BeforeXxx, AfterXxx, and groups

Matt Welch-2
In reply to this post by Cédric Beust ♔

Thanks for the reply, Cédric. I'm not quite sure I understand what you
mean by,

>>The groups @Before/AfterMethod belong to only determine whether they
>>are picked by TestNG or not"

but that's OK. I'll just assume that it doesn't fit with my current
goal.

With that in mind, perhaps someone can help me figure out how to
accomplish what I'm trying to do.

I need to run some setup and teardown code in front of and after about
1/4 of my unit/integration tests. This needs to be run before and
after each test method. It seemed intuitive that @BeforeMethod and
@AfterMethod were the right choice here, but if I'm understanding
Cédric's reply correctly then apparently they aren't. So what is the
right choice? The code needs to be run before and after every method
and not just before and after all of the methods in the group.

What I need to do:
- setup
 - method test1 (group1)
- teardown
- setup
 - method test2 (group1)
- teardown
- setup
 - method test3 (group1)
- teardown

The following won't meet my needs:
- setup
 - method test1 (group1)
 - method test2 (group1)
 - method test3 (group1)
- teardown

How do I do this? I'm betting it's pretty straightforward and I'm just
a dingleberry for missing it, because this seems like a situation that
a lot of people must face.

-Matt


On Apr 13, 2:29 pm, "Cédric Beust ♔ " <[hidden email]> wrote:

> Hi Matt,
>
> To achieve what you are trying to do, you should use @
> BeforeGroups/@AfterGroups.
>
> As you found out, @BeforeMethod/@AfterMethod are invoked around all test
> methods, regardless of their group.  The groups @Before/AfterMethod belong
> to only determine whether they are picked by TestNG or not.
>
> --
> Cédric
>
> On 4/13/07, Watter <[hidden email]> wrote:
>
>
>
> > I'm seeing something that I don't quite understand. I have methods
> > annotated with:
>
> > @BeforeMethod(groups={"group1", "group2"}
>
> > and
>
> > @BeforeMethod(groups={"group1", "group2"}
>
> > I thought that this meant that the methods using these annotations
> > would only be run when Tests were run int he same groups like:
>
> > @Test(groups = {"group1"}
> > public void group1test(){}
>
> > or
>
> > @Test(groups = {"group1", "group2"}
> > public void group1AndGroup2Test(){}
>
> > But in reality, the Before and After annotated methods are being run
> > before every single method and not just the ones in the same group.
> > Have I misunderstood how groups are supposed to work in this
> > situation?
>
> > -Matt


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply | Threaded
Open this post in threaded view
|

Re: BeforeXxx, AfterXxx, and groups

Alexandru Popescu ☀
On 4/13/07, Watter <[hidden email]> wrote:

>
> Thanks for the reply, Cédric. I'm not quite sure I understand what you
> mean by,
>
> >>The groups @Before/AfterMethod belong to only determine whether they
> >>are picked by TestNG or not"
>
> but that's OK. I'll just assume that it doesn't fit with my current
> goal.
>
> With that in mind, perhaps someone can help me figure out how to
> accomplish what I'm trying to do.
>
> I need to run some setup and teardown code in front of and after about
> 1/4 of my unit/integration tests. This needs to be run before and
> after each test method. It seemed intuitive that @BeforeMethod and
> @AfterMethod were the right choice here, but if I'm understanding
> Cédric's reply correctly then apparently they aren't. So what is the
> right choice? The code needs to be run before and after every method
> and not just before and after all of the methods in the group.
>

Please read my previous post. I think it will clarify the answer to
your question and scenario.

bests,

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

> What I need to do:
> - setup
>  - method test1 (group1)
> - teardown
> - setup
>  - method test2 (group1)
> - teardown
> - setup
>  - method test3 (group1)
> - teardown
>
> The following won't meet my needs:
> - setup
>  - method test1 (group1)
>  - method test2 (group1)
>  - method test3 (group1)
> - teardown
>
> How do I do this? I'm betting it's pretty straightforward and I'm just
> a dingleberry for missing it, because this seems like a situation that
> a lot of people must face.
>
> -Matt
>
>
> On Apr 13, 2:29 pm, "Cédric Beust ♔ " <[hidden email]> wrote:
> > Hi Matt,
> >
> > To achieve what you are trying to do, you should use @
> > BeforeGroups/@AfterGroups.
> >
> > As you found out, @BeforeMethod/@AfterMethod are invoked around all test
> > methods, regardless of their group.  The groups @Before/AfterMethod belong
> > to only determine whether they are picked by TestNG or not.
> >
> > --
> > Cédric
> >
> > On 4/13/07, Watter <[hidden email]> wrote:
> >
> >
> >
> > > I'm seeing something that I don't quite understand. I have methods
> > > annotated with:
> >
> > > @BeforeMethod(groups={"group1", "group2"}
> >
> > > and
> >
> > > @BeforeMethod(groups={"group1", "group2"}
> >
> > > I thought that this meant that the methods using these annotations
> > > would only be run when Tests were run int he same groups like:
> >
> > > @Test(groups = {"group1"}
> > > public void group1test(){}
> >
> > > or
> >
> > > @Test(groups = {"group1", "group2"}
> > > public void group1AndGroup2Test(){}
> >
> > > But in reality, the Before and After annotated methods are being run
> > > before every single method and not just the ones in the same group.
> > > Have I misunderstood how groups are supposed to work in this
> > > situation?
> >
> > > -Matt
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "testng-users" 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-users?hl=en
-~----------~----~----~----~------~----~------~--~---