Difference between @BeforeTest and @BeforeMethod annotations

25,280 views
Skip to first unread message

abhi.uppala

unread,
Dec 11, 2009, 12:40:00 AM12/11/09
to testng-users
Hi,

I am new to TestNG and was going through the documentation at the
TestNG home page. The documentation is great and descriptive. Yet, I
am confused a bit. I would like to know the difference between
@BeforeTest and @BeforeMethod annotations.

Would be grateful for the help

Thanking You
Abhishek Uppala

Karthik Krishnan

unread,
Dec 11, 2009, 3:19:55 AM12/11/09
to testng...@googlegroups.com
@BeforeMethod is equivalent to JUnit's setUp method.

@BeforeTest is called once before execution of any test method


--

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



Cédric Beust ♔

unread,
Dec 11, 2009, 3:27:16 AM12/11/09
to testng...@googlegroups.com
On Fri, Dec 11, 2009 at 12:19 AM, Karthik Krishnan <krishn...@gmail.com> wrote:
@BeforeMethod is equivalent to JUnit's setUp method.

@BeforeTest is called once before execution of any test method

More precisely, it's called before a <test> tag.

--
Cédric


Gaurang shah

unread,
Dec 11, 2009, 5:21:31 AM12/11/09
to testng...@googlegroups.com
@BeforeTest Will be called before any Test only.. ( not every method in a file is Test) while @BeforeMethod will be called before every method ( every Test is a method)


Gaurang

Ed

unread,
Dec 11, 2009, 12:36:45 PM12/11/09
to testng-users
Interesting.....
Can you give a few examples to make this more clear?


Like I read it above then:
suppose my test method calls another public helper method...
Then, from what I understand from the above, the method marked as
@BeforeMethod should be called twice, and the method marked as
@BeforeTest should be called once...

Just did a test, and they are both called once...
What don't I understand here?


Ed


On Dec 11, 11:21 am, Gaurang shah <gaurangns...@gmail.com> wrote:
> @BeforeTest Will be called before any Test only.. ( not every method in a
> file is Test) while @BeforeMethod will be called before every method ( every
> Test is a method)
>
> Gaurang
>
> On Fri, Dec 11, 2009 at 11:10 AM, abhi.uppala <abhi.upp...@gmail.com> wrote:
> > Hi,
>
> > I am new to TestNG and was going through the documentation at the
> > TestNG home page. The documentation is great and descriptive. Yet, I
> > am confused a bit. I would like to know the difference between
> > @BeforeTest and @BeforeMethod annotations.
>
> > Would be grateful for the help
>
> > Thanking You
> > Abhishek Uppala
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "testng-users" group.
> > To post to this group, send email to testng...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > testng-users...@googlegroups.com<testng-users%2Bunsu...@googlegroups.com>
> > .

Ed

unread,
Dec 11, 2009, 12:40:29 PM12/11/09
to testng-users
Did some more thinking...
Could it be that @beforeMethod is called more in case the Test method
is configured through @Test that it should be called more then once
for example, because of the invocation configuration...??

Cédric Beust ♔

unread,
Dec 11, 2009, 12:47:31 PM12/11/09
to testng...@googlegroups.com
@BeforeMethod is invoked before each method with a @Test annotation.

@BeforeTest is invoked before a <test> tag.

<test>
  <classes>
    <class name="A" />  <-- contains tests a1() and a2()
    <class name="B" />   <-- contains tests b1() and b2()
  </classes>
</test>

In this case, you will see:

@BeforeTest
  @BeforeMethod
    a1()
  @AfterMethod
  @BeforeMethod
    a2()
  @AfterMethod
  @BeforeMethod
    b1()
  @AfterMethod
  @BeforeMethod
    b22()
  @AfterMethod
@AfterTest

Hope this clears things up.

--
Cédric




To unsubscribe from this group, send email to testng-users...@googlegroups.com.

Ed

unread,
Dec 11, 2009, 2:24:32 PM12/11/09
to testng-users
Ahhhhhh of course....
I confused the <test> with the @Test annotation.... :(

abhi.uppala

unread,
Dec 14, 2009, 8:35:33 AM12/14/09
to testng-users
Thanks Cedric, your explanation has driven away all the confusion I
had.

Thanks
Abhishek Uppala
> ***Cédric
> *
> > > > testng-users...@googlegroups.com<testng-users%2Bunsubscribe@google groups.com>
> > <testng-users%2Bunsu...@googlegroups.com<testng-users%252Bunsubscribe@g ooglegroups.com>
>
> > > > .
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/testng-users?hl=en.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "testng-users" group.
> > To post to this group, send email to testng...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > testng-users...@googlegroups.com<testng-users%2Bunsubscribe@google groups.com>

Cédric Beust ♔

unread,
Dec 14, 2009, 10:08:49 PM12/14/09
to testng...@googlegroups.com
On Mon, Dec 14, 2009 at 5:35 AM, abhi.uppala <abhi....@gmail.com> wrote:
Thanks Cedric, your explanation has driven away all the confusion I
had.

You're welcome, and you're not the first one to report this confusion, which I brought upon myself by introducing both @Test and <test>, but looking back, I'm not even sure if I would do things differently if I could (maybe call the tag <tests> instead of <test>?).

--
Cédric


Krishnan Mahadevan

unread,
Apr 19, 2013, 11:52:44 AM4/19/13
to testng...@googlegroups.com
If you are invoking your tests by selecting groups, please add alwaysRun=true to your configuration methods (@Before, @After) to have them called always.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/


On Fri, Apr 19, 2013 at 2:55 PM, santhosh soma <santhos...@gmail.com> wrote:

Hi Cedric,

As per you,

1. Every @BeforeTest()  method in java class should invoke with respect to the <test> tag in testng.xml file
2.Every @BeforeMethod() method in java Class should invoke with respect to the @Test Annotation in java class


If the above what i mentioned is correct, Observed that @BeforeTest() method is not getting executed when <test> tag used <Group> mechanism.

Please help me and let me know if you need more details.

Thanks
Santhosh.

--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.

santhosh soma

unread,
Apr 20, 2013, 11:41:48 AM4/20/13
to testng...@googlegroups.com
Thanks Krishna for Quick Response.

I know that to add the attribute "allwaysrun=true" to run Before/After for all the tests.

But i want to know the reason or internal implementation.

Thanks in Advance
Santhosh.


--
You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/7CnKL_eTFvQ/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.

Nariga Rao

unread,
Jan 17, 2014, 3:14:18 PM1/17/14
to testng...@googlegroups.com
Another question that is a bit confusing:

If both class A and B have  a @BeforeTest, then how are these executed?

In your example below,

@BeforeTest
  @BeforeMethod
    a1()
...

Is the @BeforeTest  the method defined in class A, or class B, or combined? Does the user have to ensure there is only @BeforeTest method in all the class files?
How does this work with @BeforeSuite? I am assuming @BeforeClass will be run before each class, and @BeforeMethod in each class run before @Test in *that* class file.

Thx,
Nariga


Thx,
nariga

Krishnan Mahadevan

unread,
Jan 19, 2014, 9:47:16 AM1/19/14
to testng...@googlegroups.com
It depends on which class is being executed by you. If you create a suite file in which both Class A and Class B are included then the @BeforeTest method in both the classes will be executed. 
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.

For more options, visit https://groups.google.com/groups/opt_out.


--

ash...@agilelogics.co.uk

unread,
May 1, 2014, 7:28:56 PM5/1/14
to testng...@googlegroups.com
A bit repetitive terms are included here but to make it clear: 

Method annotated as @BeforeTest in Class A will run before any method of Class A will run 
Method annotated as @AfterTest in Class A will run after all the methods of Class A have run
Method annotated as @BeforeTest in Class B will run before any method of Class B will run 
Method annotated as @AfterTest in Class B will run after all the methods of Class B have run

> > .
> > For more options, visit this group at
> >http://groups.google.com/group/testng-users?hl=en.
>
>

--

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


--
You received this message because you are subscribed to the Google Groups "testng-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testng-users+unsubscribe@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/groups/opt_out.

Cédric Beust ♔

unread,
May 1, 2014, 10:28:44 PM5/1/14
to testng...@googlegroups.com
This is incorrect.

@BeforeTest / @AfterTest wrap all the methods in a <test> tag.

You are probably trying to describe @BeforeClass/@AfterClass.



-- 
Cédric



To unsubscribe from this group and stop receiving emails from it, send an email to testng-users...@googlegroups.com.

To post to this group, send email to testng...@googlegroups.com.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

Ashish Srivastava

unread,
May 2, 2014, 4:17:13 AM5/2/14
to testng...@googlegroups.com
Hi Cedric,
So if Class A and Class B each have a method annotated @BeforeTest, do you mean both the methods will run

So if the class definition goes:

Class A {
@BeforeTest
public void setUpA(){
}

// rest of the members
}

Class B {
@BeforeTest
public void setUpB(){
}

// rest of the members
}

and testNg xml goes:


  <test name="Nopackage" >
    <classes>
       <class name=“ClassA" />
       <class name=“ClassB" />
    </classes>
  </test>

then both methods setUpA() and setUp() will execute before any test is run from the given <test> tag? If so is their any order in which they will run?

with regards,
Ashish

You received this message because you are subscribed to a topic in the Google Groups "testng-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/testng-users/7CnKL_eTFvQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.

Krishnan Mahadevan

unread,
May 2, 2014, 4:31:40 AM5/2/14
to testng...@googlegroups.com
Yes, all the @BeforeTest annotated methods are going to be run before executing any @Test annotated test method that is part of the <test> tag.
The order of execution of these methods AFAIK is governed by your parallel attribute. 

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/
Reply all
Reply to author
Forward
0 new messages