Test NG starts running next Test Class before running @AfterClass method of previously running Test Class

535 views
Skip to first unread message

Kuldeep

unread,
Mar 4, 2009, 1:00:42 AM3/4/09
to testng-users
Hi,
I am having a suite of test classes
ex:
<suite name="DbSupportTestCases" verbose="2">
<test name="somename" >
<classes>
<class name="a.A" />
<class name="a.B" />
<class name="a.C" />

</classes>
</test>
</suite>

Now testng starts running "a.C" but does not complete the whole test
class (i.e : dont invoke @AfterClass method of a"a.C") but starts
executing another test class say: "a.A".

How can i make testNg to run test class as whole and thenonly start
new test class.

Kuldeep

unread,
Mar 4, 2009, 4:02:09 AM3/4/09
to testng-users
To Add to this

this is from testNG Doc

Methods run in no particular order. These are all the methods that
don't belong in the first category. The order in which these test
methods are run is random and can vary from one run to the next
(although by default, TestNG will try to group test methods by
class).

What is meant by "TestNG will try to group test methods by
class" ,will try means what ?? : in what conditions will it fail to
group by class.

borojevic

unread,
Mar 4, 2009, 8:09:26 AM3/4/09
to testng-users
Try thread-count="1" on suite, should be default anyway, but I am not
sure.
Maybe it will help.

Kuldeep

unread,
Mar 4, 2009, 11:16:42 PM3/4/09
to testng-users
This doesn't seems to work .

Is this is not exprienced by anyone else.

Karthik Krishnan

unread,
Mar 5, 2009, 2:42:13 AM3/5/09
to testng...@googlegroups.com
Have you tried using ITestNGListener?

Kuldeep

unread,
Mar 5, 2009, 9:03:40 AM3/5/09
to testng-users
No i haven't , but i was under the impression that by default TestNg
run's all the method's of a Test Class in one go.

On Mar 5, 12:42 pm, Karthik Krishnan <krishnan.1...@gmail.com> wrote:
> Have you tried using ITestNGListener?
>

Ben G

unread,
Mar 13, 2009, 10:20:02 AM3/13/09
to testng-users
I have exactly the same issue - did you find a way to make it run all
tests in a class together?

Kuldeep

unread,
Mar 14, 2009, 2:46:47 AM3/14/09
to testng-users
Not yet , but have downloaded the Source code and now debugging it to
see if something can be twiked to rectify this.
From Test NG Documentation
<------>
A) Methods run sequentially. These are all the test methods that
have dependencies or dependents. These methods will be run in a
specific order.
B) Methods run in no particular order. These are all the methods
that don't belong in the first category. The order in which these test
methods are run is random and can vary from one run to the next
(although by default, TestNG will try to group test methods by
class).
<------>

For B you can write a Listner implementing IMethodInterceptor and
override 'intercept' method.

public List<IMethodInstance> intercept(List<IMethodInstance> methods,
ITestContext context)
{

Comparator<IMethodInstance> comparator = new
Comparator<IMethodInstance>()
{

private String getQualifiedPath(IMethodInstance mi)
{
Method method = mi.getMethod().getMethod();
return method.toString();
}

public int compare(IMethodInstance m1, IMethodInstance m2)
{
return getQualifiedPath( m1 ).compareTo(getQualifiedPath( m2 ));
}

};
IMethodInstance[] array = methods.toArray( new IMethodInstance
[methods.size()] );
Arrays.sort( array, comparator );

return Arrays.asList( array );
}

Its for A) methods i am having the problem.

Cédric Beust ♔

unread,
Mar 14, 2009, 11:00:59 AM3/14/09
to testng...@googlegroups.com
Hi Kuldeep,

Can you make your code as small as possible and post it here?  I'd like to see what you are experiencing.  Your later messages seem to imply that there are dependencies in your classes below, and these can influence the ordering that TestNG will create before it runs your tests.

Thanks.

--
Cedric


--
Cédric


Kuldeep

unread,
Mar 16, 2009, 12:34:59 PM3/16/09
to testng-users
Hi Cedric,
What do you mean by "dependencies in your classes" and how this could
effect ordering of test-cases.
I am looking at TestRunner.java where you segregate 'parallel and
Sequential' methods.I think this is the only
place where order is decided.

It's only in Db related test-cases ,i am having some problem.

Problem Statement : Let's say i am having 2 test - classes
[com.A,com.B]
Each Test class has a BeforeClass method which starts a Hibernate
Transaction.

So If one Test Class say com.B Starts Executing :
It will start a transaction and will have some modified Hibernate
Entities in Transaction.
Now even before AfterClass method of com.B is executed ,TestNG starts
new executing com.A methods.
Now we are having two Hibernate transaction's open modifying same data
in Db and hence i get stale Object exception
from Hibernate.

Note : This behavior is fine in application that it will throw stale
object Excpetion to User if more than one User modifies
simultaneously.

What I need : A Test Class Should execute all the methods between
BeforeClass and AfterClass before it starts executing another Test
Class BeforeClass method.


<!--------- Sample Code ----->
public class A{
@BeforeClass
public void beforeClass(){
System.err.println("Starting Hibernate Transaction for A");}
@AfterClass
public void afterClass(){
System.err.println("Commiting Hibernate Transaction for A");}

@Test(dependsOnMethods = "b")
public void a(){
printDetails( "A.a()" );}

@Test
public void b(){
printDetails( "A.b()" );}

@Test
public void c(){
printDetails( "A.c()" );}

@Test
public void d(){
printDetails( "A.d()" );}

private void printDetails(String methodName){
System.out.println( "Doing Some Db work in " + methodName );}
}

public class B{
@BeforeClass
public void beforeClass(){
System.err.println("Starting Hibernate Transaction for B");}
@AfterClass
public void afterClass(){
System.err.println("Commiting Hibernate Transaction for B");}
@Test(dependsOnMethods = "b")
public void a(){
printDetails( "B.a()" );}
@Test
public void b(){
printDetails( "B.b()" );}
@Test
public void c(){
printDetails( "B.c()" );}

@Test
public void d(){
printDetails( "B.d()" );}
private void printDetails(String methodName){
System.out.println( "Doing Some Db work in " + methodName );}
}

testng.xml:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="someSuit" >
<test verbose="2" name="com" >
<classes>
<class name="com.A"/>
<class name="com.B"/>
</classes>
</test>
</suite>
<!--------------------------------------------------------->
Output I get
Starting Hibernate Transaction for B
Doing Some Db work in B.b()
Starting Hibernate Transaction for A
Doing Some Db work in A.b()
Doing Some Db work in A.a()
Doing Some Db work in B.a()
Doing Some Db work in A.c()
Doing Some Db work in A.d()
Commiting Hibernate Transaction for A
Doing Some Db work in B.c()
Doing Some Db work in B.d()
Commiting Hibernate Transaction for B
> ***Cédric
> *

Cédric Beust ♔

unread,
Mar 16, 2009, 12:45:08 PM3/16/09
to testng...@googlegroups.com


On Mon, Mar 16, 2009 at 9:34 AM, Kuldeep <kuldee...@gmail.com> wrote:
What do you mean by "dependencies in your classes" and how this could
effect ordering of test-cases.

I'll take a few minutes to give some background technical information that might help you understand what's going on.

Dependent methods (methods that depend on other methods or on which other methods depend) are placed in the "sequential" list, all the other methods are put in the "parallel" list.

Methods in the parallel list are then run with maximum parallelism based on what the user requested (thread pool, threading strategy, etc...).

Methods in the sequential list are sorted topologically to respect the dependency constraints.  The topological order means that the order is not total.  In other words, you can come up with several orderings and still respect the dependency constraints.  For example, if b() depends on a() and d() depends on c(), the following orders are both valid: "a b c d" and "c d a b".

The algorithm that TestNG uses is therefore using two kinds of constraints:  "hard" ones (b depends on a, therefore a *must* be invoked before a) and "soft" ones.  Soft constraints are constraints that if omitted will still result in a correct ordering but probably one that's not optimal from the user's standpoints.

Some of these soft constraints are "run parent before methods before child before methods", "run parent after methods after child after methods", "invoke an afterClass as soon as possible", etc...

This latter one is the one we are discussing.  Assume you have A#a() and B#b(), the following two orderings are valid:

1) a() afterA() b() afterB()
2) a() b() afterA() afterB()

Obviously, 1) is preferable and TestNG tries to come up with that ordering most of the time, but there might be cases (involving inheritance) where it's not doing it, either because there is a bug or because it's not possible.

I'll have to take a closer look at your example to determine what's going on here.

Hope this helps.

--
Cédric


borojevic

unread,
Mar 17, 2009, 4:40:08 AM3/17/09
to testng-users
Hi ,
put @Test(sequential = true) on class level for both classes and that
should solve your problem.
Also, just to make sure although it should not matter set thread-
count=1 on suite level.
It works for me, on my machine here is the output:

Starting Hibernate Transaction for B
Doing Some Db work in B.b()
Doing Some Db work in B.c()
Doing Some Db work in B.a()
Doing Some Db work in B.d()
Commiting Hibernate Transaction for B
Starting Hibernate Transaction for A
Doing Some Db work in A.c()
Doing Some Db work in A.b()
Doing Some Db work in A.a()
Doing Some Db work in A.d()
Commiting Hibernate Transaction for A

Hope it helps,

Cheers,
Aleksandar


On Mar 16, 5:45 pm, Cédric Beust ♔ <cbe...@google.com> wrote:
> ***Cédric
> *

Kuldeep

unread,
Mar 17, 2009, 5:58:07 AM3/17/09
to testng-users
Hi Aleksandar ,
Yup this did the trick !!
I am really ashamed of myself :) ,am using TestNG for more than a
year now and still didn't bother to read documentation fully.
But rather trying to modify the source code to suite my
requirement ,typical developer mindset.

Thanks a lot bro
Reply all
Reply to author
Forward
0 new messages