使用@factory运行depend testng方法

时间:2015-06-14 17:57:11

标签: testng testng-dataprovider

场景: -

My Testdata是1)String A 2)String B 3)String C

现在我上课了,我有数据提供者,工厂和数据 provider有3个值作为String

public class TestParent {

    public String tenant;

    @Factory(dataProvider = "myList")
    public TestParent(String s) {
        tenant = s;
    }

    @DataProvider(name = "myList")
    public Object[][] myList() {
        List<String> s = new ArrayList<String>();
        s.add("A");
        s.add("B");
        s.add("C");
        Object[][] objArray = new Object[s.size()][];

        for (int i = 0; i < s.size(); i++) {
            objArray[i] = new Object[1];
            objArray[i][0] = s.get(i);
        }
        return objArray;
    }

    @Test()
    public void setup() {
        System.out.print(tenant);
        System.out.print("\n");
    }

    @Test(dependsonMethod = setup)
    public void beforeclass() {
        System.out.print("Hello");
        System.out.print(tenant);
        System.out.print("\n");
    }

    @Test(dependsonMethod = beforeclass)
    public void afterclass() {
        System.out.print("BYE");
        System.out.print(tenant);
        System.out.print("\n");
    }

    @Test(dependsonMethod = afterclass)
    public void mm() {
        System.out.print("YES");
        System.out.print(tenant);
        System.out.print("\n");
    }
}

我希望输出

一个
HelloA
BYEA
耶撒

HelloB
BYEB
YESB
ç
HelloC
BYEC
YESC

1 个答案:

答案 0 :(得分:0)

创建两个单独的类FactoryClass和TestDataClass,然后从textng.xml运行FactoryClass

FactoryClass:

public class FactoryClass {

    @Factory(dataProvider = "myList")
    public Object[] createInstances(String str) {
        return new Object[] {new TestData(str)};
    }

    @DataProvider(name = "myList")
    static public Object[][] myList(){
     return new Object[][] {
        new Object[] { new String("A") },
        new Object[] { new String("B") },
      };
    }
} 

TestDataClass:

public class TestData {

    private String tenant;

    public TestData(String s)
    {
        this.tenant=s;
    }

    @Test()
    public void setup( )
    {
        System.out.print(tenant);
        System.out.print("\n");
    }

    @Test(dependsOnMethods="setup")
    public void beforeclass()
    {
        System.out.print("Hello");
        System.out.print(tenant);
        System.out.print("\n");

    }

    @Test(dependsOnMethods="beforeclass")
    public  void afterclass()
    {
        System.out.print("BYE");
        System.out.print(tenant);
        System.out.print("\n");
    }

    @Test(dependsOnMethods="afterclass")
    public  void mm()
    {
        System.out.print("YES");
        System.out.print(tenant);
        System.out.print("\n");

    }

}

XML文件:

<suite name="StackNew" verbose="1">
<test name="test" group-by-instances="true">
    <classes>
        <class name="stack.FactoryClass"></class>
    </classes>
</test>
</suite>

如果您面对序列问题:Link