Is BeforeSuite in my example behaving as expected ?

350 views
Skip to first unread message

shekar

unread,
Feb 6, 2012, 1:06:53 PM2/6/12
to testng-users
I have defined three tests in a Suite as follows:
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" verbose="1">
<test name="test1" preserve-order="true">
<classes>
<class name="com.tests.ModuleTest" />
<methods>
<include name="loginTestNew1" />
</methods>
</classes>
</test>

<test name="test2" preserve-order="true">
<classes>
<class name="com.tests.ModuleTest" />
<methods>
<include name="loginTestNew1" />
<include name="loginTestNew2" />
</methods>
</classes>
</test>
<test name="test3" preserve-order="true">
<classes>
<class name="com.tests.ModuleTest" />
<methods>
<include name="loginTestNew1" />
</methods>
</classes>
</test>
</suite>

The class ModuleTest is implemented as follows:
package com.tests;

import org.testng.annotations.Test;

import com.yahoo.qa.apg.fw.base.common.Constants;

public class ModuleTest extends ModuleTestBase implements Constants {
/**
* Login Test
*
*/
@Test
public void loginTestNew1() {

System.out.println("Trying Multi Test Suite, Variable Set to " +
this.prodClass);
}
@Test
public void loginTestNew2() {

System.out.println("Trying Multi Test Suite-2, Variable Set to " +
this.prodClass);
}
}

ModuleTestBase is implemented as follows:
package com.tests;

import java.io.IOException;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;

import com.yahoo.qa.apg.fw.rmx.ui.WebDriverContext;

public class ModuleTestBase {
protected WebDriverContext context = null;

protected String prodClass = "bDefault";

@BeforeSuite(alwaysRun = true)
protected void commonSetUpTestSuite() throws IOException {
prodClass = "bSuite";
}
}


The output of the tests is as follows:
Trying Multi Test Suite, Variable Set to bDefault
Trying Multi Test Suite, Variable Set to bDefault
Trying Multi Test Suite, Variable Set to bSuite


I was expecting a output of
Trying Multi Test Suite, Variable Set to bSuite
Trying Multi Test Suite, Variable Set to bSuite
Trying Multi Test Suite, Variable Set to bSuite

Could you please help me understand if this is expected behavior of
BeforeSuite.

shekar

unread,
Feb 7, 2012, 6:38:29 AM2/7/12
to testng-users
Cedric,

Could you please confirm if this is the right behavior.

Krishnan Mahadevan

unread,
Feb 7, 2012, 6:43:09 AM2/7/12
to testng...@googlegroups.com
Shekar,
One thing stands out in your implementation.

    @BeforeSuite(alwaysRun = true)
       protected void commonSetUpTestSuite() throws IOException {
               prodClass = "bSuite";
       }

In my limited understanding, I am not sure as to how could TestNG invoke a "Non Public" method despite it being annotated using an annotation.

Can you please retry after defining it to be a public method ?


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"


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


shekar

unread,
Feb 7, 2012, 6:50:10 AM2/7/12
to testng-users
I did try by making the method public and the result was same.

From what I have seen so far, it appears that the right variable value
becomes available only when the last test in the suite is invoked. If
there is only one test in the suite, it seems to work.

Thanks,
shekar

Cédric Beust ♔

unread,
Feb 7, 2012, 1:16:33 PM2/7/12
to testng...@googlegroups.com
Hi Krishnan,

FYI, TestNG has no problem invoking protected methods, it's only private methods that will be skipped, even if they are annotated with @Test.

-- 
Cédric

Cédric Beust ♔

unread,
Feb 7, 2012, 1:17:21 PM2/7/12
to testng...@googlegroups.com
From a quick look at your code, I think your expectation is correct. Could you email me a small zipped project that I can build to reproduce this?

Thanks.

-- 
Cédric




Krishnan Mahadevan

unread,
Feb 7, 2012, 1:32:58 PM2/7/12
to testng...@googlegroups.com
Cedric
Thank you for pointing out my mis understanding there.
> --
> 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.
>

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

Krishnan

unread,
Feb 9, 2012, 3:57:01 AM2/9/12
to testng-users
Cedric,
Based on your inputs that TestNG is capable of invoking protected
methods, I decided to run a quick check and I found something which I
thought I would clarify with you.

If a class has a mixture of protected and public TestNG annotated
methods then TestNG seems to invoke all of them, but if a class has
only protected methods then TestNG doesnt invoke them. Why is it so ?

To elaborate, here's a sample :
//Both test methods are invoked
public class TestNGExploration {
@Test
protected void foo() {
System.out.println("Protected Method says hi");
}

@Test
public void bar() {
System.out.println("Public Method says hi");
}
}

Output :

Public Method says hi
Protected Method says hi
PASSED: bar
PASSED: foo

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

//Nothing is invoked
public class TestNGExploration {
@Test
protected void foo() {
System.out.println("Protected Method says hi");
}

@Test
protected void bar() {
System.out.println("Another Protected Method says hi");
}
}

Output :


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

-Krishnan



On Feb 7, 11:32 pm, Krishnan Mahadevan

niharika varshney

unread,
Feb 10, 2012, 12:45:02 AM2/10/12
to testng...@googlegroups.com
dug a lil in the code to understand the behavior.....
below is my understanding..

to determine whether the class is an annotated class or not, TestNG depends on getMethods(), which returns inherited methods as well, but doesn't return any non-public methods... .I believe this was done to take care of inherited annotated tests..
So your class is not considered, since there are only protected methods in it and no inherited annotated stuff..

However, when you have even a single public method, the getmethods returns that one public method with annotation, which makes it a class with some annotated methods..

Sample code for the above explanation..

public class CheckClassExistence {

@Test
public void test1(){
Method[] m = TestNgExploration.class.getDeclaredMethods();
for(Method m1:m){
System.out.println(m1.getName()+Modifier.isPublic(m1.getModifiers()));
System.out.println(TestNGClassFinder.isTestNGClass(TestNgExploration.class, new JDK15AnnotationFinder(new DefaultAnnotationTransformer())));
}
m = TestNgExploration.class.getMethods();
for(Method m1:m){
System.out.println(m1.getName()+Modifier.isPublic(m1.getModifiers()));
}
}
}

On your second question, why the protected methods run, I think the getDeclaredMethods is being used which returns all but private methods..and hence the protected methods are added to the set of methods to run..

It seems to be a bug to me though when you just have protec

Regards,
Niharika

Regards,
Niharika

shekar

unread,
Feb 23, 2012, 8:33:47 AM2/23/12
to testng-users
Hi Cedric,

Did you get a chance to look at this.

Cédric Beust ♔

unread,
Feb 23, 2012, 1:25:53 PM2/23/12
to testng...@googlegroups.com
Hi Shekar,

I was able to reproduce the behavior you're seeing but I haven't had the time to investigate it yet, sorry.

-- 
Cédric




On Thu, Feb 23, 2012 at 5:33 AM, shekar <nagesh...@gmail.com> wrote:
Hi Cedric,

Did you get a chance to look at this.
Reply all
Reply to author
Forward
0 new messages