constructor of test classes being called before @BeforeSuite

2,237 views
Skip to first unread message

aarti_gupta

unread,
Jan 6, 2011, 11:44:24 PM1/6/11
to testng-users
Hi,

I am using factories with testng, and have a method with
annotation@BeforeSuite.
The factory class returns an object array consisting of the java
classes which I want testng engine to run.
At this point I find that the constructor of these classes is called
before the @BeforeSuite method.
While it is expected that the @BeforeSuite method is run before @Test
methods, shouldnt the @BeforeSuite also be invoked before the
constructor of any class invoked by testng?

I am using testng 5.14.6

Thanks
Aarti

Marcus Döring

unread,
Jan 7, 2011, 2:33:50 PM1/7/11
to testng-users
the constructor is ionvoked as soon as the object is created. So, it's
expected that the @BeforeSuite method, which is within the created
object, runs after the constructor.
(normally test-methods should not do anything in their constructor(-
s). Use the @Before variations for that.)

Cédric Beust ♔

unread,
Jan 7, 2011, 2:41:17 PM1/7/11
to testng...@googlegroups.com
If your @BeforeSuite method is not static, TestNG needs to create an instance of its class before it can invoke it.

-- 
Cedric



--
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


aarti gupta

unread,
Jan 7, 2011, 5:48:15 PM1/7/11
to testng...@googlegroups.com
Hi Cedric, 

This makes sense, I made my @BeforeSuite method static, but I still find that it is invoked after the constructor of the test class is called.
Is there any way of forcing that it is called before the constructor?
Note: I also have other non static @BeforeSuite methods, (for which it does not matter if they run before or after the constructor)

Thanks
aarti

2011/1/7 Cédric Beust ♔ <ced...@beust.com>

Cédric Beust ♔

unread,
Jan 7, 2011, 5:50:26 PM1/7/11
to testng...@googlegroups.com
Aarti,

Can you post a short class showing what you're doing?

Thanks.

-- 
Cédric

aarti gupta

unread,
Jan 7, 2011, 6:33:13 PM1/7/11
to testng...@googlegroups.com

Hi Cedric, 


I was able to do what I wanted to by using @BeforeClass annotation.


However I am still wondering why the Before Suite method is executed after the constructor of the test class, even when it is static.


Here is a symbolic representation of what I am doing, (removed the context for legal reasons)

________________________________________________________________________________________________________



public class XYZ{


@BeforeSuite(groups = { "abstract" })

public static void Alpha() throws Exception

{

//Operation A

System.out.println("Performing essential operation, which must be done before constructor of class");

       

}


@BeforeSuite(groups = { "abstract" })

public  void Beta() throws Exception

{

//Perform unrelated operation

       

}

}


______________________________________________________________________________________________________________



public class ABC extends XYZ{


public ABC(String, String)

{

try{

            //Operation B


            System.out.println("Performing operation which must be done after operation A but before C");


   } catch(Exception e){

                      System.out.println("exception: " + e.getMessage());

       }

}


  @Test

  public void Gamma()

{


   perform operation c

   perform Test

}


}


___________________________________________________


I find that the output is something like this


Performing operation which must be done after operation A but before C

Performing essential operation, which must be done before constructor of class

perform operation c

perform Test


______________________________________

 I am just curious at what point TestNG engine calls the constructor, and whether this is expected behavior or not.

It seems that Before suite should execute before constructor of all test classes, at least intuitively, since it is run before the test suite, and hence should be run before constructor of test classes as well?


--------------------------------------------

Thanks

-aarti





2011/1/7 Cédric Beust ♔ <ced...@beust.com>

Nalin Makar

unread,
Jan 27, 2011, 4:18:38 PM1/27/11
to testng...@googlegroups.com
My understanding ist that the tests are all instantiated before the start of the test run. This is done to prevent running into any situation where a class doesn't exist or can not be instantiated later on.

Why cant you move the code from class ctor to a @BeforeClass method?


-nalin
Reply all
Reply to author
Forward
0 new messages