2

My code structure is like this:

I have four classes having ten tests and one @BeforeClass method to run before any of the tests. In each of four classes all tests are of one single group.

And I have one another class which I need to run before that @BeforeClass method in four of the tests.

How do I achieve that? Please help me.

2 Answers 2

3

Please try the below case, it executes both @BeforeGroups and @BeforeClass.

First create a base class name ="TestNGBase" add all @after/@Before methods here (including @BeforeClass,@BeforeGroups etc..)

And extend this class on all your main test class.

Example:

public class TestNGBase {
    boolean logToStandardOut = true;

    @BeforeSuite()
    public void beforeSuite() {
        Reporter.log("--------        @BeforeSuite", logToStandardOut);
    }

    @BeforeGroups(groups = "regression_test")
    public void beforeGroups() {
        Reporter.log("--------        @BeforeGroups", logToStandardOut);
    }

    @BeforeClass
    public void beforeClass() {
        Reporter.log("--------        @BeforeClass", logToStandardOut);
    }

    @BeforeTest
    public void setUp() {
        Reporter.log("--------        @BeforeTest", logToStandardOut);
    }

    @BeforeMethod
    public void beforeMethod() {
        Reporter.log("--------        @BeforeMethod", logToStandardOut);
    }

    @AfterMethod
    public void afterMethod() {
        Reporter.log("--------        @AfterMethod", logToStandardOut);
    }

    @AfterTest
    public void tearDown() {
        Reporter.log("--------        @AfterTest", logToStandardOut);
    }

    @AfterClass
    public void afterClass() {
        Reporter.log("--------        @AfterClass", logToStandardOut);
    }

    @AfterGroups(groups = "regression_test")
    public void afterGroups() {
        Reporter.log("--------        @AfterGroups", logToStandardOut);
    }

    @AfterSuite
    public void afterSuite() {
        Reporter.log("--------        @afterSuite", logToStandardOut);
    }
}

Create new TestNGXml Class for Test Case 01:

public class TestNGxml_TS001 extends TestNGBase {
    boolean logToStandardOut = true;

    @Test(groups = { "regression_test" }, priority = 1)
    public void test001() {
        Reporter.log("test001", true);
    }

    @Test(groups = { "regression_test" })
    public void test002() {
        Reporter.log("test002", true);
    }

    @Test(groups = { "regression_test" })
    public void test003() {
        Reporter.log("test003", true);
    }

    @Test(groups = { "regression_test" })
    public void test004() {
        Reporter.log("test004", true);
    }

    @Test(alwaysRun = true)
    public void test005() {
        Reporter.log("test005 - always run = true", true);
    }

}

TestNGXml Class for Test Case 02:

public class TestNGxml_TS002 extends TestNGBase {
    boolean logToStandardOut = true;

    @Test(groups = { "regression_test" })
    public void test201() {
        Reporter.log("test102", true);
    }

    @Test(groups = { "regression_test" })
    public void test202() {
        Reporter.log("test202", true);
    }

    @Test(groups = { "regression_test" })
    public void test203() {
        Reporter.log("test203", true);
    }

    @Test(groups = { "regression_test" })
    public void test204() {
        Reporter.log("test204", true);
    }

}

and add a testng.xml file as below:

<?xml version="1.0" encoding="UTF-8"?>
<suite name="testngsuite" verbose="1">
    <test name="testCase002">
        <classes>
            <class name="testngxml.TestNGxml_TS001" />
            <class name="testngxml.TestNGxml_TS002" />
        </classes>
    </test>
</suite>

The output would looks like this when we execute testng.xml file:

--------        @BeforeSuite
--------        @BeforeTest
--------        @BeforeClass
--------        @BeforeGroups
--------        @BeforeMethod
test002
--------        @AfterMethod
--------        @BeforeMethod
test003
--------        @AfterMethod
--------        @BeforeMethod
test004
--------        @AfterMethod
--------        @BeforeMethod
test005 - always run = true
--------        @AfterMethod
--------        @BeforeMethod
test001
--------        @AfterMethod
--------        @AfterClass
--------        @BeforeClass
--------        @BeforeMethod
test102
--------        @AfterMethod
--------        @BeforeMethod
test202
--------        @AfterMethod
--------        @BeforeMethod
test203
--------        @AfterMethod
--------        @BeforeMethod
test204
--------        @AfterMethod
--------        @AfterGroups
--------        @AfterClass
--------        @AfterTest
--------        @afterSuite

===============================================
testngsuite
Total tests run: 9, Failures: 0, Skips: 0
===============================================
2

And I have one another class which I need to run before that @BeforeClass method in four of the tests.

To achieve this you can try to do this: Just make all of the four class extend this "AnotherClass" and add @BeforeClass annotation in the constructor of this class.

Hope this will help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.