Order of execution of @BeforeTest and @BeforeClass

1,811 views
Skip to first unread message

Dan Haynes

unread,
Jun 20, 2014, 4:29:12 PM6/20/14
to testng...@googlegroups.com
Hi,

Either I'm missing something or this is a bug in V6.8. I have a class that uses @BeforeClass, @BeforeTest, @AfterClass and @AfterTest

When I run the class (from Eclipse) the first method to execute is @BeforeTest and the last method to execute is @AfterTest.

It seems like the @BeforeClass should run first and @AfterClass last.

What am I missing?

Thanks in advance.

Example

package main;

import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Bug
{
    @BeforeClass
    public void classPrologue()
    {
        System.out.println("I run before any @Test methods do");
    }

    @AfterClass
    public void classEpilogue()
    {
        System.out.println("I run after all the @Test methods do");
    }
   
    @BeforeTest
    public void methodPrologue()
    {
        System.out.println("I run before each @Test");
    }
   
    @AfterTest
    public void methodEpilogue()
    {
        System.out.println("I run after each @Test");
    }

    @Test
    public void shouldRunAfterPrologue1()
    {
        System.out.println("Test1");
    }

    @Test
    public void shouldRunAfterPrologue2()
    {
        System.out.println("Test2");
    }
}


I get the following result:

[TestNG] Running:
  C:\Users\dphaynes\AppData\Local\Temp\testng-eclipse-45812670\testng-customsuite.xml

I run before each @Test
I run before any @Test methods do
I run after all the @Test methods do
I run after each @Test
PASSED: shouldRunAfterPrologue1
PASSED: shouldRunAfterPrologue2

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================

Krishnan Mahadevan

unread,
Jun 20, 2014, 11:34:42 PM6/20/14
to testng...@googlegroups.com
Dan
TestNG is working as designed. 
@BeforeTest - gets executed before any execution starts from a <test>

Even when you run from eclipse programmatically a suite is being created behind the scenes with a single <test>  

Following is the order that TestNG follows. 
@BeforeSuite
@BeforeTest
@BeforeClass
@BeforeMethod
@Test
@AfterMethod
@AfterClass
@AfterTest
@AfterSuite

~ Krishnan

iSent. iPhone. iReget.iTypos!
--
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.
Visit this group at http://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

kappati malli

unread,
Jun 20, 2014, 11:48:43 PM6/20/14
to testng...@googlegroups.com

No that is not bug....!
@BeforeClass vl execute before @BeforeTest  same applicable to @AfterClass and @AfterTest

Dan Haynes

unread,
Jun 24, 2014, 12:15:32 AM6/24/14
to testng...@googlegroups.com
Thanks for the response! Can you point me to any documentation that explains the rationale?

I thought @BeforeClass was supposed to run before any @Test method, which seems to imply that it should also run before any @BeforeTest would?

Thanks again!

   Dan

niharika varshney

unread,
Jun 24, 2014, 5:51:01 AM6/24/14
to testng...@googlegroups.com
@Test and <test> are two different things in testng
@Test is referred to as a test method - before for a @Test corresponds to @BeforeMethod  (and not @BeforeTest)

Structure of testng xml on which the @Before naming conventions are based :http://testng.org/doc/documentation-main.html#testng-xml

The structure of a testng xml is

<suite>  - @BeforeSuite
   <test> - @BeforeTest
      <class> - @BeforeClass
           <include methods..>  - @BeforeMethod  ..@Test....@AfterMethod
      </class> - @AfterClass
   </test> - @AfterTest
...


You can try referring to this to clear the confusion : http://testng.org/doc/documentation-main.html#annotations



Regards,
NV


Dan Haynes

unread,
Jun 24, 2014, 9:29:39 AM6/24/14
to testng...@googlegroups.com
Finally - the light goes on in my dim little head! :)

Thanks, that's exactly what was wrong with the way I was thinking. I was conflating @Test with <test>

Thanks much!


    Dan

On Friday, June 20, 2014 2:29:12 PM UTC-6, Dan Haynes wrote:
Reply all
Reply to author
Forward
0 new messages