TestNg自动化测试框架获取参数列表

Java体系中,对单元测试常用的方式是Junit和TestNg,对于测试工程师来说,一般会使用TestNg来搭建自动化测试框架。其中,为了使单元测试提高复用性,会遇到以下场景:

动态获取测试的参数、获取测试参数

1.在@DataProvider的方法中获取Method

@DataProvider
    public Object[][] getData(Method method) {
        Object[][] result = null;
        // 根据方法名称判断
        if (method.getName().equals("fast")) {
            result = new Object[][] { new Object[] { 1 } };
        }  else {
            result = new Object[][] { new Object[] { 0 } };
        }
        return result;
    }

2.在testng.xml中获取参数,在上下文中获取配置文件中的参数

@DataProvider(name="getData")
public Object[][]getData(ITestContext context)throws IOException {
Map map = context.getAllTestMethods()[0].getTestClass().getXmlClass().getAllParameters();
    map.get("caseId");
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容