使用TestNG获取@Factory注释类中传递的参数值



是否可以从任何ITestListener, ISuiteListener或任何其他侦听器方法中获得用于初始化@Factory注释测试类的参数值?

下面是一个示例测试类。我的目的是获得类初始化参数'value'的值使用任何侦听器方法,可能。

    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();
        }
    }

可以从ITestResult#getInstance()访问您的对象。你只需要转换为适当类型的对象(TestClass),并为value添加getter(或更改其可见性)。

ITestResult在许多侦听器中可用。

最新更新