0

I am creating a test automation framework using selenium & TestNG. This Framework will be use by all of the other team member. Want to specify a class template for all the team member so that basic structure of the test class will same for all and reduce the effort for writing the same structure for all test.

Whenever any member create any class in a particular package the class will be created with some predefined code like below

package com.xxx.yyy.testmodule.dummytest;
import org.testng.annotations.Test;
import com.xxx.yyy.lib.zzz.CommonLib;

public class Test3 extends CommonUtilCommonLibities{
    @Test(description="", groups= {""})
    public void testTest3() {

        //Read Test Data Here

        //Test Logic

        //Test Verification

    }
}
joy87
  • 25
  • 4

2 Answers2

0

Use an abstract class :

package com.xxx.yyy.testmodule.dummytest;
import org.testng.annotations.Test;
import com.xxx.yyy.lib.zzz.CommonLib;

public abstract class Test3 extends CommonUtilCommonLibities {

    @Test(description="", groups= {""})
    public void testTest3() {

        //Read Test Data Here
        Data data = readTestData();

        //Test Logic
        test(data);

        //Test Verification
        testAssertion(data);

    }

    abstract Data readTestData();
    abstract void test(Data data);
    abstract void testAssertion(data);
}

Of course this only works if all your data beans extend some base bean which is Data in my example.

Fourat
  • 2,366
  • 4
  • 38
  • 53
0

If you use Intellij IDEA, you can define a test class template, including your methods. There's a similar question with the instructions.

You may also want to make use of a Template Method pattern:

public abstract class GenericTest<T> {

    @Test
    public void doTest() {
        T data = loadTestData();
        this.runTestLogic(data);
        this.runAssertions(data);
        // something else...
    }

    protected abstract T loadTestData();

    protected abstract void runTestLogic(T result);

    protected abstract void runAssertions(T result);

}

Your concrete test classes will extend this generic class and implement the actual testing and data reading logic:

public class ConcreteTest extends GenericTest<Integer> {

    @Override
    protected Integer loadTestData() {
        return 42;
    }

    @Override
    protected void runTestLogic(Integer result) {
        System.out.println(result);
    }

    @Override
    protected void runAssertions(Integer result) {
        assertEquals(0, result % 2);
    }
}
jolice
  • 98
  • 1
  • 6