6

The problem is that my test class is already implemented some class:

public class MainTest extends ExecutionContext ...

Thus, I can move general logic to parent class as it is usually done:

public class TestBase {
    protected final String baseUrl = Config.getProperty(Config.TEST_HOST);
    protected final String driverName = Config.getProperty(Config.BROWSER);
    protected final DriversEnum driverInstance = DriversEnum.valueOf(driverName.toUpperCase());

    @BeforeMethod(alwaysRun = true)
    public void start() { 
       ...
    }

    @AfterMethod(alwaysRun = true)
    public void end() {
        Logger.logDebug("Start AFTER Method");
    }

I tried to use Java 8 features like default or static methods. Here is a sample:

@Listeners(TestListener.class)
public interface ITestBase {
    String baseUrl = Config.getProperty(Config.TEST_HOST);
    String driverName = Config.getProperty(Config.BROWSER);
    DriversEnum driverInstance = DriversEnum.valueOf(driverName.toUpperCase());

    @BeforeMethod(alwaysRun = true)
    default public void start() {
        ...
    }

    @AfterMethod(alwaysRun = true)
    default public void end() {
        ...
    }

When I run typical TestNG test method, like:

public class AppUiDemo implements ITestBase {
    @Test
    public void checkWebDriverCreation() {
      ...
    }

start() and end() methods haven't called.

Is it possible to use interface with any implementation for execution code before and after test methods with TestNG?

1

Default methods in interfaces are not yet supported but they will be in the next release (https://github.com/cbeust/testng/blob/master/CHANGES.txt#L36) which should be 6.13 (release soon).

0

Yes, it is possible but the version of TestNG that allow using interface methods in tests is not released yet. You need to download it from this repository.

If you use Maven you can specify additional repository in your pom.xml:

<repository>
    <id>testng</id>
    <name>testng</name>
    <url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>

And then add TestNG dependency:

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.11.1-SNAPSHOT</version>
</dependency>

Example:

package test;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class DummyTest implements ITest {

    @BeforeMethod
    public void beforeTest() {
        System.out.println("before from class");
    }

    @Test
    public void test1() {
        System.out.println("I am test1");
    }
}

and ITest interface

package test;

import org.testng.annotations.BeforeMethod;

public interface ITest {

    @BeforeMethod
    default void beforeDefaultInterface() {
        System.out.println("before from default interface method");
    }
    @BeforeMethod
    static void beforeStaticInterface() {
        System.out.println("before from static interface method");
    }
}

The output of the test above will be:

    before from default interface method
    before from static interface method
    before from class
    I am test1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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