Java 由于数据提供程序配置不正确,已跳过测试

Java 由于数据提供程序配置不正确,已跳过测试,java,unit-testing,testng,Java,Unit Testing,Testng,我第一次与testNG合作,遇到了一个问题。我试图从一个json文件加载一些数据,并将这些数据与数据提供者结合使用来编写一些测试。我有一个助手文件,它有一个parseData方法,可以从json文件中获取数据,并构建我需要测试的所有数据的映射。在我的主测试文件中,我定义了一个测试,如下所示: 在我的主测试文件中,我还有一个BeforeClass方法,用于加载tDataHelper类中的parseData方法调用 但每次运行测试时,它都会被跳过,因为每次我尝试调试CreateStustudents

我第一次与testNG合作,遇到了一个问题。我试图从一个json文件加载一些数据,并将这些数据与数据提供者结合使用来编写一些测试。我有一个助手文件,它有一个parseData方法,可以从json文件中获取数据,并构建我需要测试的所有数据的映射。在我的主测试文件中,我定义了一个测试,如下所示:

在我的主测试文件中,我还有一个BeforeClass方法,用于加载tDataHelper类中的parseData方法调用

但每次运行测试时,它都会被跳过,因为每次我尝试调试CreateStustudents数据提供程序时,tDataHelper文件都有空映射。我认为这与静态vs实例有关,我不确定到底出了什么问题。下面的代码看起来正常吗?它应该工作吗?有人能解释一下吗

    public class testStudents
    {
        private static tDataHelper helper = new tDataHelper();

        @BeforeClass
        public void setup() throws Exception
        {
            tDataHelper.parseData();
        }

        @FunctionalTest
        @Test(dataProvider = "createStudents", dataProviderClass = tDataHelper.class)
        public void testCreateStudents(List<Student> studentsToCreate){}
    }


    public class tDataHelper
    {
        private static List<Student> studentsToCreate = new HashSet<>();

        static void parseData() throws Exception
        {
            // read in json file and add students to the students list
            // studentsToCreate.add(node.parse(....))
        }

        @DataProvider
        public static Object[][] createStudents()
        {
            return new Object[][]{
                {
                    studentsToCreate
                }
        }
}
公共课堂测试学生
{
私有静态tDataHelper=新tDataHelper();
@课前
public void setup()引发异常
{
tDataHelper.parseData();
}
@功能测试
@测试(dataProvider=“createStudents”,dataProviderClass=tDataHelper.class)
public void testCreateStudents(列出studentsToCreate){}
}
公共类tDataHelper
{
private static List studentsToCreate=new HashSet();
静态void parseData()引发异常
{
//读入json文件并将学生添加到学生列表中
//studentsToCreate.add(node.parse(..)
}
@数据提供者
公共静态对象[][]createStudents()
{
返回新对象[][]{
{
学生创造
}
}
}

您的数据提供程序类中可能存在配置问题

你们想在所有学生身上迭代循环吗?对!那个么你们的测试方法应该如下所示

@Test(dataProvider = "createStudents", dataProviderClass = tDataHelper.class)
public void testCreateStudents(Student studentsToCreate){}
我更新了示例,用String类替换了Student类,下面是工作示例。 请核对以下代码

public class tDataHelper {
    private static List<String> studentsToCreate = new ArrayList<String>();

    static void parseData() throws Exception {
        studentsToCreate.add("user1");
        studentsToCreate.add("user2");
        studentsToCreate.add("user3");
    }

    @DataProvider
    public static Object[][] createStudents() {
        Object[][] objArray = new Object[studentsToCreate.size()][];
        for (int i = 0; i < studentsToCreate.size(); i++) {
            objArray[i] = new Object[1];
            objArray[i][0] = studentsToCreate.get(i);
        }
        return objArray;
    }
}

public class testStudents {
    private static tDataHelper helper = new tDataHelper();

    @BeforeClass
    public void setup() throws Exception {
        tDataHelper.parseData();
    }
    @Test(dataProvider = "createStudents", dataProviderClass = tDataHelper.class)
    public void testCreateStudents(String studentsToCreate) {
        System.out.println(studentsToCreate);
    }
}
公共类tDataHelper{
私有静态列表studentsToCreate=newarraylist();
静态void parseData()引发异常{
studentsToCreate.add(“user1”);
studentsToCreate.add(“user2”);
studentsToCreate.add(“user3”);
}
@数据提供者
公共静态对象[][]createStudents(){
Object[]objArray=新对象[studentsToCreate.size();
对于(int i=0;i

我使用for,因此您不需要手动解析json数据并从数据提供程序类中删除。

您的数据提供程序类中可能存在配置问题

你们想在所有学生身上迭代循环吗?对!那个么你们的测试方法应该如下所示

@Test(dataProvider = "createStudents", dataProviderClass = tDataHelper.class)
public void testCreateStudents(Student studentsToCreate){}
我更新了示例,用String类替换了Student类,下面是工作示例。 请核对以下代码

public class tDataHelper {
    private static List<String> studentsToCreate = new ArrayList<String>();

    static void parseData() throws Exception {
        studentsToCreate.add("user1");
        studentsToCreate.add("user2");
        studentsToCreate.add("user3");
    }

    @DataProvider
    public static Object[][] createStudents() {
        Object[][] objArray = new Object[studentsToCreate.size()][];
        for (int i = 0; i < studentsToCreate.size(); i++) {
            objArray[i] = new Object[1];
            objArray[i][0] = studentsToCreate.get(i);
        }
        return objArray;
    }
}

public class testStudents {
    private static tDataHelper helper = new tDataHelper();

    @BeforeClass
    public void setup() throws Exception {
        tDataHelper.parseData();
    }
    @Test(dataProvider = "createStudents", dataProviderClass = tDataHelper.class)
    public void testCreateStudents(String studentsToCreate) {
        System.out.println(studentsToCreate);
    }
}
公共类tDataHelper{
私有静态列表studentsToCreate=newarraylist();
静态void parseData()引发异常{
studentsToCreate.add(“user1”);
studentsToCreate.add(“user2”);
studentsToCreate.add(“user3”);
}
@数据提供者
公共静态对象[][]createStudents(){
Object[]objArray=新对象[studentsToCreate.size();
对于(int i=0;i

我使用的是,因此您不需要手动解析json数据并从数据提供程序类中删除。

数据提供程序应该是独立的,而不是从外部初始化。请检查

您可以选择:

  • 使用数据提供程序的设计方式:

    public class testStudents {
    
        @FunctionalTest
        @Test(dataProvider = "createStudents", dataProviderClass = tDataHelper.class)
        public void testCreateStudents(List<Student> studentsToCreate){}
    }
    
    
    public class tDataHelper {
    
        private static List<Student> parseData() throws Exception {
            // read in json file and add students to the students list
            // studentsToCreate.add(node.parse(....))
        }
    
        @DataProvider
        public static Object[][] createStudents() {
            return new Object[][]{
                {
                    parseData();
                }
            };
        }
    }
    
    公共课堂测试学生{
    @功能测试
    @测试(dataProvider=“createStudents”,dataProviderClass=tDataHelper.class)
    public void testCreateStudents(列出studentsToCreate){}
    }
    公共类tDataHelper{
    私有静态列表parseData()引发异常{
    //读入json文件并将学生添加到学生列表中
    //studentsToCreate.add(node.parse(..)
    }
    @数据提供者
    公共静态对象[][]createStudents(){
    返回新对象[][]{
    {
    解析数据();
    }
    };
    }
    }
    
  • 使用a类utils one并在测试中使用它:

    public class testStudents {
    
        private static List<Student> studentsToCreate;
    
        @BeforeClass
        public void setup() throws Exception {
            studentsToCreate = tDataHelper.parseData();
        }
    
        @FunctionalTest
        @Test
        public void testCreateStudents() {}
    }
    
    
    public class tDataHelper {
    
        public static List<Student> parseData() throws Exception {
            // read in json file and add students to the students list
            // studentsToCreate.add(node.parse(....))
        }    
    }
    
    公共课堂测试学生{
    私有静态列表学生创建;
    @课前
    public void setup()引发异常{
    studentsToCreate=tDataHelper.parseData();
    }
    @功能测试
    @试验
    public void testCreateStudents(){}
    }
    公共类tDataHelper{
    公共静态列表parseData()引发异常{
    //读入json文件并将学生添加到学生列表中
    //studentsToCreate.add(node.parse(..)
    }    
    }
    
  • 数据专家