@BeforeSuite / @AfterSuite in the same class - same instance reused?

47 views
Skip to first unread message

Wujek Srujek

unread,
Dec 29, 2009, 7:34:14 AM12/29/09
to testng...@googlegroups.com
Hi.
I have two questions:
1. I define @BeforeSuite and @AfterSuite defined in the same class. Is TestNG using the same instance of the class to invoke the methods, or does it create a new instance for them? The question is important because it will tell me whether I can store state in @BeforeSuite and reuse it later in @AfterSuite.
2. If I define more than one method with the same annotation, is any order guaranteed at runtime?

Regards.

Cédric Beust ♔

unread,
Dec 29, 2009, 3:03:29 PM12/29/09
to testng...@googlegroups.com
Hi Wujek,

On Tue, Dec 29, 2009 at 4:34 AM, Wujek Srujek <wujek....@googlemail.com> wrote:
Hi.
I have two questions:
1. I define @BeforeSuite and @AfterSuite defined in the same class. Is TestNG using the same instance of the class to invoke the methods, or does it create a new instance for them? The question is important because it will tell me whether I can store state in @BeforeSuite and reuse it later in @AfterSuite.

It's not guaranteed so I wouldn't rely on that behavior.

A safer way would be to store your data in an ITestContext attribute in @BeforeSuite and retrieve that value in @AfterSuite, let me know if you need more details about this.

 
2. If I define more than one method with the same annotation, is any order guaranteed at runtime?


No.  If you want guaranteed ordering, you should use dependencies.

--
Cédric


Wujek Srujek

unread,
Dec 29, 2009, 3:38:21 PM12/29/09
to testng...@googlegroups.com
Hi Cedric,

1. I define @BeforeSuite and @AfterSuite defined in the same class. Is TestNG using the same instance of the class to invoke the methods, or does it create a new instance for them? The question is important because it will tell me whether I can store state in @BeforeSuite and reuse it later in @AfterSuite.

It's not guaranteed so I wouldn't rely on that behavior.

A safer way would be to store your data in an ITestContext attribute in @BeforeSuite and retrieve that value in @AfterSuite, let me know if you need more details about this.

I know how to use the ITestContext, and I'm perfectly fine with this approach, it looks cleaner anyways. Thanks.

2. If I define more than one method with the same annotation, is any order guaranteed at runtime?


No.  If you want guaranteed ordering, you should use dependencies.

Thanks for the tip with dependencies.



Thanks for your answers.

Regards.
Reply all
Reply to author
Forward
0 new messages