2016-09-25 26 views
1

我正在第一次使用testNG並有問題。我正在嘗試從json文件加載一些數據,並將這些數據與dataProviders一起使用來編寫一些測試。我有一個幫助文件,它有一個parseData方法,它從json文件中提取數據並構建我需要測試的所有數據的地圖。在我的主要測試文件,我定義的測試如下:由於數據提供程序配置不正確而跳過的測試

我主要的測試文件,我也有加載一個BeforeClass方法調用的tDataHelper類的parseData方法。

每當測試運行,雖然時,它就會跳過去,因爲tDataHelper文件具有空映射每次我嘗試調試createStudents數據提供者。我認爲這與靜態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 
       } 
     } 
} 
+0

能否請您提供更多的細節或具體的前充足? –

+0

@amitbhoraniya嗨,我添加了一些更詳細的希望它可以幫助 –

+0

謝謝...這些可能是有用的:) –

回答

0

可能是您的數據提供程序類中存在配置問題。

你想迭代循環對所有的學生嗎?對!然後你的測試方法應該如下。

@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); 
    } 
} 

我使用qafJson Data Provider,所以手動你不需要解析您的JSON數據,並從數據提供程序類去掉。

0

數據提供者應該是獨立的,而不是從外部初始化的。檢查the documentation

你必須選擇:

  1. 使用數據提供者它是如何設計爲:

    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(); 
          } 
         }; 
        } 
    } 
    
  2. 使用你的類utils的一個,並在測試中使用它:

    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(....)) 
        }  
    } 
    
相關問題