0

Is it possible to get the value of parameters that are used to initialize a @Factory annotated test class from any of the ITestListener, ISuiteListener or any other listener methods?

Below is a sample test class. My intention is to get the value of the class init parameter 'value' using any of the listener methods, possibly.

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;

    import org.testng.Assert;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Factory;
    import org.testng.annotations.Listeners;
    import org.testng.annotations.Test;
    import org.testng.reporters.EmailableReporter2;

    @Listeners({ TestExecutionListener.class, EmailableReporter2.class })
    public class TestClass {

        private int value;

        @Factory(dataProvider = "data", dataProviderClass = TestClass.class)
        public TestClass(final int value) {
            this.value = value;
        }

        @Test(alwaysRun = true)
        public void testOdd() {
            Assert.assertTrue(value % 2 != 0);
        }

        @Test(alwaysRun = true)
        public void testEven() {
            Assert.assertTrue(value % 2 == 0);
        }

        @DataProvider(name = "data")
        public static Iterator<Object[]> data() {

            List<Object[]> list = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                list.add(new Object[] { i });
            }
            return list.iterator();
        }
    }

Thanks in advance.

Sarath
  • 1,438
  • 4
  • 24
  • 40

1 Answers1

1

You can have access to your object from ITestResult#getInstance(). You'll just have to cast to object in the appropriate type (TestClass) and add a getter for value (or change its visibility).

ITestResult is available in many listeners.

juherr
  • 5,640
  • 1
  • 21
  • 63