Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
robinmatz opened this issue Dec 12, 2022 · 0 comments

Comments

@robinmatz
Copy link

robinmatz commented Dec 12, 2022

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:

  • I have already considered switching to org.testng.Reporter.log() together with the ExtentIReporterSuiteClassListenerAdapter listener. But then, the logs are not shown in the Test view (Spark Reporter), which I find a bit inconvenient when investigating the report.

  • I have also tried cheating by extending the @before method like so

@BeforeMethod
    void setup(ITestResult result) {
        ExtentTestManager.createMethod(result, true);
        performAction1();
    }

However, then testSomething will appear as two tests in the log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant