2

I have tried this : BaseClass

package pages;

import org.testng.annotations.BeforeSuite;

public class BaseClass {

    @BeforeSuite
    public static void setup() {
        System.out.println("in set up method");
    }

}

TestClass

 package pages;

 import org.testng.annotations.Test;

 public class TestClass extends BaseClass {

@Test(groups = { "group1" })

public void grpOnetest() {
    System.out.println("Method grp one");

}

@Test(groups = { "group2" })

public void grpTwotest() {
    System.out.println("Method grp two");

}

@Test(groups = { "group3" })

public void grpThreetest() {
    System.out.println("method grp three");

}
}

testng.xml

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

    <test name="Test">
        <groups>
            <run>
                <include name="group1" />
            </run>
        </groups>
        <classes>
            <class name="pages.TestClass" />
        </classes>
    </test> <!-- Test -->
</suite> <!-- Suite -->

It doesnt give any error, just don't print: "in set up method" i.e. doesn't run @BeforeSuite

paul
  • 4,333
  • 16
  • 71
  • 144
  • Add `alwaysRun = true` on `@BeforeSuite` and/or remove `static` – juherr May 31 '16 at 07:11
  • Worked, whats the concept behind it, can you put it in an answer? Why we need `alwaysRun` (obviously because it is setup method) why it can't run without it? – paul May 31 '16 at 07:21

2 Answers2

9

You need alwaysRun = true on @BeforeSuite.

From the documentation:

For before methods (beforeSuite, beforeTest, beforeTestClass and beforeTestMethod, but not beforeGroups): If set to true, this configuration method will be run regardless of what groups it belongs to.

In other words, if you run a suite with specific group(s), configuration methods must have the expected group(s) or have alwaysRun that will activate the method with every goups.

juherr
  • 5,640
  • 1
  • 21
  • 63
  • When i add alwaysRun=True, I am getting the error : The attribute alwaysRun is undefined for the annotation type BeforeSuit. Any insights? – anandhu Jan 03 '20 at 05:18
1

You need to put the setup method in the group that you are including, else it wouldn't run since that method is not part of the group.

Add the suite to all the groups, that should work.

niharika_neo
  • 8,441
  • 1
  • 19
  • 31