【问题标题】:Can we make sure @beforesuite gets called before dataprovider?我们能否确保在 dataprovider 之前调用 @beforesuite?
【发布时间】:2020-01-16 14:56:25
【问题描述】:

我们可以按照下面的方法来初始化测试数据吗?有两点我想实现。

  1. 需要从文件中初始化/加载一次测试数据,并在所有数据提供者中使用相同的测试数据。考虑在@beforesuite 类中实现测试数据加载器。
  2. @test 方法中同时需要来自 dataprovider 的数据和来自 testNG 文件的参数。

    @BeforeSuite
    @Parameters(value = { "test_data_file" })
    public static synchronized void init(String test_data_file) {
        TestDataFactory.load(test_data_file);       
    }    
    @Test(dataProvider="dp_dummy",dataProviderClass = DP_1.class)
    public void testDummyAPI(TestData test_data,ITestContext context){
        String param = context.getCurrentXmlTest().getParameter("param");
    }        
    @DataProvider(name = "dp_dummy")
    public Object[][] getDataFromDataprovider(ITestContext context) {    
        List<TestData> test_data_collection = TestDataFactory.getTestData(targated_test_data);
        Object[][] test_data_set = new Object[test_data_collection.size()][1];    
        for(TestData test_data : test_data_collection)
            test_data_set[i++][0] = test_data;    
         return test_data_set;}
    

【问题讨论】:

    标签: java automated-tests testng-dataprovider test-data


    【解决方案1】:

    假设您正确创建了test_data_set,您可以像这样实现第二点

     @Test(dataProvider="dp_dummy",dataProviderClass = DP_1.class)
        public void testDummyAPI( String p, Object[][] ob){
          System.out.println(p);      
          System.out.println(ob[0][0]);
    
        }   
    
        @DataProvider(name = "dp_dummy")
        public Object[][] getDataFromDataprovider(ITestContext context) {    
            List<TestData> test_data_collection = TestDataFactory.getTestData(targated_test_data);
            Object[][] test_data_set = new Object[test_data_collection.size()][1];    
            for(TestData test_data : test_data_collection)
                test_data_set[i++][0] = test_data;    
              String param = context.getCurrentXmlTest().getParameter("param");
    
               return new Object[][] {
    
                   { param,  test_data_set}
               };
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-08
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多