extent-framework / extentreports-testng-adapter

TestNG Adapter for Extent Framework
http://extentreports.com/docs/versions/4/java/testng.html
Apache License 2.0
50 stars 14 forks source link

Question: How to use ExtentTest.log() method in @Before and @After methods #26

Open robinmatz opened 1 year ago

robinmatz commented 1 year ago

Hi there,

I am trying to build an e2e test automation solution using TestNG, extentreports and this adapter. I like using the extentreports test logger in the test methods I am using like so:

import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.service.ExtentTestManager;
import com.aventstack.extentreports.testng.listener.ExtentITestListenerClassAdapter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners({ExtentITestListenerClassAdapter.class})
public class ExampleTests {
    @Test
    void testSomething() {
        performAction1();
        performAction2();
    }

    private void performAction1() {
        ExtentTestManager.getTest().log(Status.INFO, "performing action 1");
        /* other code goes here */
    }

    private void performAction2() {
        ExtentTestManager.getTest().log(Status.INFO, "performing action 2");
    }
}

However, the problem comes if I want to use the performAction1() as a setup, like when it makes sense that all tests in the suite perform this action before the actual test starts:

import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.service.ExtentTestManager;
import com.aventstack.extentreports.testng.listener.ExtentITestListenerClassAdapter;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

@Listeners({ExtentITestListenerClassAdapter.class})
public class ExampleTests {
    @BeforeMethod
    void setup() {
        performAction1();
    }

    @Test
    void testSomething() {
        performAction2();
    } 

    private void performAction1() {
        ExtentTestManager.getTest().log(Status.INFO, "performing action 1");
    }

    private void performAction2() {
        ExtentTestManager.getTest().log(Status.INFO, "performing action 2");
    }
}

This will obviously fail, because ExtentTestManager.getTest() will return null when we are in the @Before method.

Is there a graceful way to include the ExtentTest.log() method in @Before methods?

Alternatives I have considered: