Specifics of @BeforeClass and @AfterClass execution order

301 views
Skip to first unread message

peter sgouros

unread,
Jun 13, 2018, 3:05:37 AM6/13/18
to testng-users
at what point are the @Before and @After class annotated methods (actually and intended?) meant to run?

For a variety of reasons I need to run a set of complex test cases sequentially (i.e. parallel=none, thread-count=1) though not in any particular order. In each case, I need to set up a complex environment, run the tests, then tear down the complex environment before moving on to the next test case where I will need to set up a new (but different and potentially conflicting) environment.  but my hope and expectation is that the entire test class is executed before proceeding to the next test case.  after a fair amount of experimentation and searching it appears to me that the @BeforeClass and @AfterClass methods may be executed at any arbitrary time providing that the time is indeed before or after the test methods within the class in question. as a result, the @AfterClass method may not execute (going by the log file) until potentially several test class executions later. causing my environments to conflict

am I incorrect in assuming that the @AfterClass method of one test class should execute before the @BeforeClass method of another test class?

how is it, when running in what appears to be be single threaded conditions, the test runner still seems to be intent on running the @AfterClasses in arbitrary order or at arbitrary times?

is there a way to configure testng such that each test class run to completion (with all associated @BeforeClass and @AfterClass methods) before proceeding to the next test class?

what is occurring now is that (for example) the @AfterClass methods are being invoked not at the end of the test class they are attached to, but at a later time, often in the middle of one of the subsequent test classes. sometimes just after the @BeforeClass method has been invoked for the next class. this is causing issues. as the before and after methods are counterparts. the test methods depend on things created by the @before.  is this really the intended operation for this feature?

the one viable (but annoying) option I have found is to place the @AfterClass actions into a test method and either set up a dependency chain or priority order which seems to be at odds with general good test practice. has anyone any other viable options?

or alternatively setup a testng.xml file that lists each test case a as a separate suite or something similar.  what I really want to know is whether the current behavior of the @BeforeClass and @AfterClass annotations is a feature or a bug.

cheers

akash kansal

unread,
Jun 13, 2018, 12:24:20 PM6/13/18
to testng...@googlegroups.com

--
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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

khushboo vyas

unread,
Jun 14, 2018, 3:20:02 AM6/14/18
to testng...@googlegroups.com
Hi peter, I think it might help you, try to use @BeforeClass or @BeforeTest with the function that you have created to set up your complex environment and then after that likewise you can use @AfterClass or @AfterTest to your tear down function. 

On Wed, Jun 13, 2018 at 4:31 AM, peter sgouros <psgo...@gmail.com> wrote:

--

total QA

unread,
Jun 14, 2018, 4:15:03 AM6/14/18
to testng-users
Hi All,

You  can find the complete hierarchy of all Testng 13 Annotations in this link.


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

peter sgouros

unread,
Jun 14, 2018, 1:26:48 PM6/14/18
to testng...@googlegroups.com
I'm aware of the documented behavior, what I'm concerned about is that the evidence of my tests indicate that testng is not following the described behavior pattern.
if you imagine that I have 2 test classes, call them A and B. each has a @BeforeClass, @AfterClass and several test methods. what I am seeing as my tests run is that (assuming they occur in the order A, B) is that the B.BeforeClass is executed before the A.AfterClass. and A.AfterClass may actually be occurring after the first B.TestMethod is occurring.  In my case the test cases are built relying on the fact that they can be run in isolation and in any order, meaning that they clean up after themselves. if A cleans up after B is started, it may (and is) affecting the environment in a way that causes test B to fail.

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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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/nVgH6F-TIBo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.

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


--
Peter Sgouros

peter sgouros

unread,
Jun 14, 2018, 1:27:44 PM6/14/18
to testng...@googlegroups.com
this is indeed what I am trying to do, but there seems to be an issue with how (an when) the Before and After Class methods are being called, causing the difficulty.

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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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/nVgH6F-TIBo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users...@googlegroups.com.
To post to this group, send email to testng...@googlegroups.com.
Visit this group at https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.


--
Peter Sgouros

akash kansal

unread,
Jun 14, 2018, 9:52:41 PM6/14/18
to testng...@googlegroups.com
Hi Peter ,

Can you share your project repository/git link.
As, the scenario you are taking working fine in my case.

Are you using selenium web driver or simple test.

On Thu, Jun 14, 2018 at 10:56 PM, peter sgouros <psgo...@gmail.com> wrote:
I'm aware of the documented behavior, what I'm concerned about is that the evidence of my tests indicate that testng is not following the described behavior pattern.
if you imagine that I have 2 test classes, call them A and B. each has a @BeforeClass, @AfterClass and several test methods. what I am seeing as my tests run is that (assuming they occur in the order A, B) is that the B.BeforeClass is executed before the A.AfterClass. and A.AfterClass may actually be occurring after the first B.TestMethod is occurring.  In my case the test cases are built relying on the fact that they can be run in isolation and in any order, meaning that they clean up after themselves. if A cleans up after B is started, it may (and is) affecting the environment in a way that causes test B to fail.

On Wed, Jun 13, 2018 at 9:24 AM akash kansal <akashka...@gmail.com> wrote:
On Wed, Jun 13, 2018 at 4:31 AM, peter sgouros <psgo...@gmail.com> wrote:
at what point are the @Before and @After class annotated methods (actually and intended?) meant to run?

For a variety of reasons I need to run a set of complex test cases sequentially (i.e. parallel=none, thread-count=1) though not in any particular order. In each case, I need to set up a complex environment, run the tests, then tear down the complex environment before moving on to the next test case where I will need to set up a new (but different and potentially conflicting) environment.  but my hope and expectation is that the entire test class is executed before proceeding to the next test case.  after a fair amount of experimentation and searching it appears to me that the @BeforeClass and @AfterClass methods may be executed at any arbitrary time providing that the time is indeed before or after the test methods within the class in question. as a result, the @AfterClass method may not execute (going by the log file) until potentially several test class executions later. causing my environments to conflict

am I incorrect in assuming that the @AfterClass method of one test class should execute before the @BeforeClass method of another test class?

how is it, when running in what appears to be be single threaded conditions, the test runner still seems to be intent on running the @AfterClasses in arbitrary order or at arbitrary times?

is there a way to configure testng such that each test class run to completion (with all associated @BeforeClass and @AfterClass methods) before proceeding to the next test class?

what is occurring now is that (for example) the @AfterClass methods are being invoked not at the end of the test class they are attached to, but at a later time, often in the middle of one of the subsequent test classes. sometimes just after the @BeforeClass method has been invoked for the next class. this is causing issues. as the before and after methods are counterparts. the test methods depend on things created by the @before.  is this really the intended operation for this feature?

the one viable (but annoying) option I have found is to place the @AfterClass actions into a test method and either set up a dependency chain or priority order which seems to be at odds with general good test practice. has anyone any other viable options?

or alternatively setup a testng.xml file that lists each test case a as a separate suite or something similar.  what I really want to know is whether the current behavior of the @BeforeClass and @AfterClass annotations is a feature or a bug.

cheers

--
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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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/nVgH6F-TIBo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to testng-users+unsubscribe@googlegroups.com.

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


--
Peter Sgouros

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

khushboo vyas

unread,
Jun 15, 2018, 8:40:27 AM6/15/18
to testng...@googlegroups.com
Not sure but, If you have one environment set up that ur using for both A and B class, in that case you can create one separate Utility class with @AfterClass and @BeforeClass and define your environment setup there, after that you can extend same utility class with Both of your classes as "class A extends UtilityClass" and class B extends UtilityClass" .

Ram Vishwakarma

unread,
Jun 15, 2018, 10:15:40 AM6/15/18
to testng...@googlegroups.com
Hello Team,

Currently, I'm new for automation but at my level, I achieving something good, I establish one framework for automation.
In my framework, I'm using TestNG with Page Object Model and for reporting, we are using the Extent report.
But in my application mostly test cases are failed due to the performance issue, I handle this situation with the help of IRetryAnalyzer
I'm using extent report for reporting issues but in this report failed test cases lock in multiple time, how to lock the last status for the executed test case.
please help me.....

Regards,
Ram Vishwakarma
--
Regards,
Ramniwas Vishwakarma
Cell No. :- 9977050509

Krishnan Mahadevan

unread,
Jun 15, 2018, 10:17:32 AM6/15/18
to testng...@googlegroups.com

Ram,

Please DONOT hijack threads.

For your query, please post a new query on the forum.

And when you post, don’t forget to include sample code etc., that we can look at.

 

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/

--

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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.

--
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 https://groups.google.com/group/testng-users.
For more options, visit https://groups.google.com/d/optout.



 

--

Regards,

Ramniwas Vishwakarma

Cell No. :- 9977050509

--

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.

Reply all
Reply to author
Forward
0 new messages