我们自定义一个名为 TestCaseDataSource 的注解,
这里定义了2个参数:filename 和 subDirectory
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface TestCaseDataSource {
String filename() default "";
String subDirectory() default "";
}
在写case时,可以这么使用
public class LoginTest extends TestCase {
@TestCaseDesc(author = "best.fei",version = "1.0.0",)
@CaseDataSource(filename = "loginTestData.json",subDirectory = "user")
@Test(dataProvider = "providerTestCaseData")
public void testNormalLogin(InputParam inputParam) throws Exception {
}
这里我们用来指明case的数据源文件,
然后重写testng的DataProvider方法
@DataProvider(name = "providerTestCaseData")
public Object[][] providerTestData(Method method, ITestContext context) throws IOException {
Object[][] ret = (Object[][])null;
....
String filename = ((CaseDataSource)method.getAnnotation(CaseDataSource.class)).filename();
....
return ret;
}
网友评论