Using @BeforeSuite and @AfterSuite

7,629 views
Skip to first unread message

Anand

unread,
Dec 2, 2010, 5:09:56 PM12/2/10
to testng-users
Hi,

I am trying to use TestNG and Selenium for UI testing. I have a
question on using @BeforeSuite and @AfterSuite annotations. I want to
setup and start selenium drivers in the @BeforeSuite annotated method
and stop in @AfterSuite. I have defined these in a class, say,
TestSuiteSetup

public class TestSuiteSetup () {
@BeforeSuite(alwaysRun = true)
public void setupSuite() {
...
}

@AfterSuite(alwaysRun = true)
public void tearDown() {
...
}
}

My test method are defined in separate classes, say LoginTest and
MainPageTest

public class LoginTest {
@Test(description = "Testdnvalid Login")
public void testInvalidLogin() {
}

@Test(description = "Test valid login")
public void testValidLogin () {
}

@Test(description = "Test logout.")
public void testLogout () {
}
}


public class MainPageTest {
@Test(description = "test banner")
public void testBanner () {
}

@Test(description = "test navigation")
public void testNavigation () {
}
}

The XML suite file looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="UI Tests - FireFox">
<listeners>
<listener class-name="org.uncommons.reportng.HTMLReporter" />
<listener class-name="org.testng.reporters.XMLReporter" />
</listeners>

<parameter name="selenium.browser" value="*firefox" />
<parameter name="selenium.port" value="4444" />
<parameter name="test.login.username" value="test" />
<parameter name="test.login.password" value="test" />

<test name="LoginTest">
<classes>
<class name="LoginTest"></class>
</classes>
</test>

<test name="MainPage">
<classes>
<class name="MainPageTest"></class>
</classes>
</test>
</suite>


Where should I include the TestSuiteSetup. The setup and tear down
needs to be executed before and after the suite is executed. It works
if I include it in within the <classes> tag. Do I need to include it
in both the test definitions? But since this is for the suite, is
there a way to include this in the <suite> definition?

Thanks
Anand

Cédric Beust ♔

unread,
Dec 2, 2010, 5:22:19 PM12/2/10
to testng...@googlegroups.com
Hi Anand,

You should be able to just specify TestSuiteSetup in one <test> tag (preferably its own <test> tag).

-- 
Cédric



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


Anand

unread,
Dec 2, 2010, 7:48:09 PM12/2/10
to testng-users
Thanks. Putting it in a separate <test> tag works.

<test name="SuiteSetup>
<classes>
<class name="TestSuiteSetup"></class>
</classes>
</test>

Anand

Liam Power

unread,
Dec 3, 2010, 4:06:24 PM12/3/10
to testng-users
Anand,
If it's of any help to you, here's what I did in setting up the
@BeforeSuite & @AfterSuite for Selenium testing.
I defined a Base Test class(see code below), which contains the
@BeforeSuite & @AfterSuite methods.
The @BeforeSuite starts the Selenium RC driver instance, sets the
speed and logs in. The @AfterSuite logs out & shuts down.
Other test classes then extend this Base Test class. This way I only
need to refer to the Test classes in the xml file.


public class OAMBaseTestClass {
protected static OAMMainPage myOAMMainPage;
protected static SeleniumHelper selenium;

@BeforeSuite (alwaysRun = true)

@Parameters({"selenium.host","selenium.port","selenium.browser","selenium.url",
"selenium.speed", "username","password"})
public void Setup(String selHost, String selPort, String
selBrowser,String selURL, String selSpeed, String username, String
password) {
// Start the Selenium RC instance
selenium = new SuiteUtils().getSeleniumHelperInstance(selHost,
selPort, selBrowser, selURL);
selenium.setSpeed(selSpeed);
OAMLoginPage myOAMLoginPage = new OAMLoginPage(selenium);
OAMAppSelectorPage myOAMAppSelectorPage = new
OAMAppSelectorPage(selenium);
System.out.print("\n$$$Waiting for Login Page\n");
while (!myOAMLoginPage.isLoginPageLoaded()){
System.out.print(".");
}

if (myOAMLoginPage.functionLogin(username, password)){
System.out.print("\n$$$Login was successful\n");
myOAMAppSelectorPage.doClickOAMLink();
System.out.print("\n$$$OAM Link clicked...\n");

}
else {
selenium.takeScreenShot("LOGIN Failed");
new SuiteUtils().endSeleniumInstance(selenium);
fail("Failed to login");
}
}

@AfterSuite(alwaysRun = true)
public void SuiteTeardown (){
myOAMMainPage.doLogout();
new SuiteUtils().endSeleniumInstance(selenium);

Cédric Beust ♔

unread,
Dec 3, 2010, 4:31:05 PM12/3/10
to testng...@googlegroups.com
Yes, that's another option.

There are two minor drawbacks to this approach, though:
  • All your test classes extend the set up class while only one really needs to.
  • It forces your classes to extend from that class, and you might want to save that "extends" slot for a class that does something else than suite set up.
I don't believe either of these drawbacks are a big deal, though.

-- 
Cédric


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


Felipe Knorr Kuhn

unread,
Dec 3, 2010, 4:38:04 PM12/3/10
to testng...@googlegroups.com
That's what I usually do as well.

I haven't faced any scenario that I would need to extend a different class yet.

FK

2010/12/3 Cédric Beust ♔ <ced...@beust.com>

Liam Power

unread,
Dec 3, 2010, 5:06:52 PM12/3/10
to testng-users
Hi Cedric,
Yes, it seems to work quite well for my tests. Interestingly, it only
invokes the @BeforeSuite & @AfterSuite methods once when I put 2 test
classes into a single xml suite with 2 tests. If the 2 test classes
had different @BeforeSuite & @AfterSuite methods, which ones would be
invoked for the suite?
xml example below - is it correct?
Liam.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="tests.someGUITests" verbose="3">
<parameter name="selenium.host" value="localhost"></parameter>
<parameter name="selenium.port" value="4444"></parameter>
<parameter name="selenium.browser" value="*firefox"></parameter>
<parameter name="selenium.url" value="http://alpha:50080//"></
parameter>
<parameter name="selenium.speed" value="300"></parameter>
<parameter name="username" value="Administrator"></parameter>
<parameter name="password" value="xxxx"></parameter>

<test name="PromotionTest" preserve-order="true">
<groups>
<run>
<include name="promotions"/>
<include name="categories"/>
</run>
</groups>
<classes>
<class name="com.myco.ManagementSystem.PromotionTest">
</class>
</classes>
</test>
<test name="EventCategoryTest" preserve-order="true">
<classes>
<class name="com.myco.ManagementSystem.EventCategoryTest">
</class>
</classes>
</test>
</suite>



On Dec 3, 9:31 pm, Cédric Beust ♔ <ced...@beust.com> wrote:
> Yes, that's another option.
>
> There are two minor drawbacks to this approach, though:
>
>    - All your test classes extend the set up class while only one really
>    needs to.
>    - It forces your classes to extend from that class, and you might want to
>    save that "extends" slot for a class that does something else than suite set
>    up.
>
> I don't believe either of these drawbacks are a big deal, though.
>
> --
> Cédric
>
>
>
>
>
> On Fri, Dec 3, 2010 at 1:06 PM, Liam Powerbegin_of_the_skype_highlighting     end_of_the_skype_highlighting<billy3she...@gmail.com> wrote:
> > Anand,
> > If it's of any help to you, here's what I did in setting up the
> > @BeforeSuite & @AfterSuite for Selenium testing.
> > I defined a Base Test class(see code below), which contains the
> > @BeforeSuite & @AfterSuite methods.
> > The @BeforeSuite starts the Selenium RC driver instance, sets the
> > speed and logs in. The @AfterSuite logs out & shuts down.
> > Other test classes then extend this Base Test class. This way I only
> > need to refer to the Test classes in the xml file.
>
> > public class OAMBaseTestClass {
> >        protected static OAMMainPage myOAMMainPage;
> >        protected static SeleniumHelper selenium;
>
> >        @BeforeSuite (alwaysRun = true)
>
> > @Parameters({"selenium.host","selenium.port","selenium.browser","selenium.u­rl",
> > testng-users...@googlegroups.com<testng-users%2Bunsubscribe@google­groups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/testng-users?hl=en.
>
> --
> Cédric- Hide quoted text -
>
> - Show quoted text -

Cédric Beust ♔

unread,
Dec 3, 2010, 5:55:16 PM12/3/10
to testng...@googlegroups.com
Hi Liam,

Yes, TestNG recognizes that the same method is being included more than once, and if that method is a @BeforeSuite/@AfterSuite, it will only run it once.  Which is why it's safe to have all your test classes extend that set up suite, it's just a bit wasteful (but negligibly so).

-- 
Cédric


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


Mayank

unread,
Aug 17, 2013, 4:26:54 PM8/17/13
to testng...@googlegroups.com
Hi All,

I am also not able to follow the behaviour of annotations 'BeforeSuite' and 'AfterSuite' for my following implementation. When I use the tag <groups> to include a group in my Test, it does not execute the methods for above annotations. But when I comment out the <groups> tag in testng.xml both methods are executed at the expected sequence in test. Could someone please clarify why the use of tag <groups> hinder the execution of annotations 'BeforeSuite' and 'AfterSuite'. Thanks

public class ClassTwo {

    @BeforeSuite
    public void runsBeforeSuiteClassOne(){
        System.out.println("Running before suite from ClassTwo");
    }
    @AfterSuite
    public void runsAfterSuiteClassOne(){
        System.out.println("Running after suite from ClassTwo");
     }
    @Test(groups={"groupTwo"},description="Testing in class two's method one")
    public void methodOneFromClassTwo(){
        System.out.println("Written from methodOneFromClassTwo");
   }
}


public class ClassOne extends ClassTwo{
   
    @Test(description="Testing in class one's method two", groups={"groupTwo"})
    public void methodTwoFromClassOne(){
        System.out.println("Written from methodTwoFromClassOne");
    }
   
    @Test(description="Testing in class one's method one",groups={"groupOne"})
    public void methodOneFromClassOne(){
        System.out.println("Written from methodOneFromClassOne ");
    }
}

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestNgExamples">
  <test verbose="2" name="Test1" preserve-order="true">
      <groups>
          <run>
              <include name="groupTwo"/>
          </run>
      </groups><!-- -->
    <classes>
      <class name="abc.learn.ClassOne"/>
    </classes>
  </test>
</suite>


Following is the result when I comment the tag groups :

Running before suite from ClassTwo
Written from methodOneFromClassOne
Written from methodTwoFromClassOne
Written from methodOneFromClassTwo
PASSED: methodOneFromClassOne
        Testing in class one's method one
PASSED: methodTwoFromClassOne
        Testing in class one's method two
PASSED: methodOneFromClassTwo
        Testing in class two's method one

===============================================
    Test1
    Tests run: 3, Failures: 0, Skips: 0
===============================================

Running after suite from ClassTwo

Following is the result when I use the tag groups :


Written from methodTwoFromClassOne
Written from methodOneFromClassTwo
PASSED: methodTwoFromClassOne
        Testing in class one's method two
PASSED: methodOneFromClassTwo
        Testing in class two's method one

===============================================
    Test1
    Tests run: 2, Failures: 0, Skips: 0
===============================================

Krishnan Mahadevan

unread,
Aug 18, 2013, 1:43:39 AM8/18/13
to testng...@googlegroups.com
Add the attribute alwaysRun=true to your before suite annotations and retry. They r not getting executed because you are selecting on groups and they are not part of any groups [ per your sample code ]
--
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/groups/opt_out.


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

Jin Chen

unread,
Aug 27, 2013, 10:55:36 AM8/27/13
to testng...@googlegroups.com
Hi Cedric:

I have one class SuiteSetup which only contains @BeforeSuite and @AfterSuite, however I don't want this class shows up in my report.
How can I do that? (On my report , I only want all classes names which does have the @Test in it )
BTW, I am using ISuite.getResults() to get all test results.

Thank you

Jin


Sneha Shejwal

unread,
Apr 2, 2014, 5:06:29 AM4/2/14
to testng...@googlegroups.com
Hi Cedric,

I am not able to execute test with the configuration mentioned above.

My XML looks like as below

<?xml version="1.0" encoding="UTF-8"?>
<suite name = "Selenium Project">



<test name = "First Test">
<classes>
<class name="DateLogin" >
<methods>
<include name="testLogin" />
<include name="testGlobalWorkPlan" />
<include name="testregionalWorkPlan" />
<include name="testProjectWorkPlan" />
</methods>
</class>
</classes>
</test>

<test name = "Second Test">
<classes>
<class name="new1">
</class>
</classes>
</test>
<test name="SuiteSetup"> 
    <classes> 
        <class name="testsetup"></class> 
    </classes> 
</test> 
</suite>

Krishnan Mahadevan

unread,
Apr 9, 2014, 2:11:57 PM4/9/14
to testng...@googlegroups.com
Sneha,
You would need to add a lot more context to your question. What exactly is the problem you are facing ? [ include stacktrace for e.g.,]

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/


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

Reply all
Reply to author
Forward
0 new messages