易百教程

17、@Factory注解有什么用?

当想通过单个测试类运行多个测试用例时,@Factory 注解很有用。主要用于测试用例的动态执行。

让我们通过一个例子来理解。

testcase1.java

package com.yiibai;  
import org.testng.annotations.Test;  
public class Testcase1  
{  
@Test  
public void test1()  
{  
System.out.println("testcase 1");  
}  
}

testcase2.java

package com.yiibai;  
import org.testng.annotations.Test;  
public class Testcase2   
{  
    @Test  
    public void test1()  
    {  
        System.out.println("testcase 2");  
    }  
}

Factory.java

import org.testng.annotations.Factory;  
public class Factory1  
{  
    @Factory  
    public Object[] getTestClasses()  
    {  
        Object tests[]=new Object[2];  
        tests[0]=new Testcase1();  
        tests[1]=new Testcase2();  
        return tests;  
    }  
}