TestNG - 无法让@Factory工作

时间:2015-06-01 18:20:37

标签: java testng

尝试学习如何使用@Factory,但是当我以两种方式之一执行它时,我得到一个错误或根本没有执行任何测试。我在Eclipse中调用XML文件,右键单击它,然后选择“Run as TestNG Suite”。

使用此XML文件,套件会执行,但会选择/执行零测试:

<suite name="suite1" verbose="5">
    <test name="test1">
        <classes>
            <class name="qa.tests.MyFactory"/>
        </classes>
    </test>
</suite>

使用这个XML文件,我得到一个例外的测试失败:

<suite name="suite1" verbose="5">
    <test name="test1">
        <packages>
            <package name="qa.tests" />
        </packages>
    </test>
</suite>

错误:

Can't invoke public void qa.tests.MyTest.testSample1(): either make it static or add a no-args constructor to your class

MyFactory.java类:

package qa.tests;
import org.testng.annotations.Factory;

public class MyFactory {
    @Factory
    public Object[][] dp() {
        Object[][] data = new Object[][] {
            { "1", "TestCase1", "Sample test 1" },
            { "2", "TestCase2", "Sample test 2" },
            { "3", "TestCase3", "Sample test 3" }
        };
        return data;
    }
}

MyTest.java类:

package qa.tests;
import org.testng.annotations.Test;
import static org.testng.Assert.*;

public class MyTest {
    private String num;
    private String name;
    private String desc;

     public MyTest(String num, String name, String desc) {
         this.num = num;
         this.name = name;
         this.desc = desc;
     }

     @Test()
     public void testSample1() {
         System.out.println(num + ", " + name + ", " + desc);
         assertTrue( true );
     }
}

世界上我做错了什么?

3 个答案:

答案 0 :(得分:1)

首先,Factory应返回一维Object数组。 并且该工厂方法应该返回您尝试执行的测试类的实例。

因此,如果您更改为

,工厂将运行您的测试
public class MyFactory {
    @Factory
    public Object[] dp() {
        Object[] data = new Object[] {
            new MyTest("1", "TestCase1", "Sample test 1"), new Mytest("1", "TestCase1", "Sample test 1"), new MyTest("1", "TestCase1", "Sample test 1")
        };
        return data;
    }
}

答案 1 :(得分:0)

根据此错误:either make it static or add a no-args constructor to your class, 你的 MyTest 类需要有公共的no-args构造函数,Java不会添加默认的构造函数,因为你已经有了参数化的构造函数,即public MyTest(String num, String name, String desc)

<强>更新 改变你的工厂类

public class MyFactory {

@DataProvider(name = "MyData")
public static Object[][] dp(ITestContext context, Method m) throws Exception {

    return new Object[][] { new Object[] { "1", "TestCase1", "Sample test 1" }};
}

@Factory(dataProvider = "MyData")
public Object[] createInstances(String num, String name, String desc) {
    return new Object[] { new MyTest(num, name, desc) };
}
}

答案 2 :(得分:0)

//Factory class supplying data to test
public class DataFactory {  

    @Factory
    public Object[] DataSupplier(){

        Object[] testData = new Object[3];
        testData[0] = new FactoryTest(1);
        testData[1] = new FactoryTest(2);
        testData[2] = new FactoryTest(3);
        return testData;
    }

}

//Test class
public class FactoryTest {

    int i;

    public FactoryTest(int i) {
        this.i = i;
    }

    @Test
    public void f() {
        System.out.println(i);
    }

}

Testng.xml

<suite name="Suite">
  <test name="Test">
    <classes>
      <class name="factorydata.DataFactory"/>
    </classes>
  </test> <!-- Test -->
</suite> 

Datafactory类通过创建测试类的实例并将参数传递给FactoryTest类构造函数来提供测试数据。

FactoryTest类使用来自datafactory的数据。

在testng.xml中引用具有factory的类,在本例中为DataFactory。

这解释了一个简单的TestNg Factory注释用法。