【问题标题】:How to skip a data set (on error) from TestNG data provider?如何从 TestNG 数据提供者跳过数据集(出错)?
【发布时间】:2018-10-03 13:33:30
【问题描述】:

我正在尝试使用 TestNG(当然还有 dataprovider 注释)执行数据驱动测试。

我的场景是这样的......

  1. 使用 dataProvider 拥有一个 2 暗淡的数组。 (我正在使用它从 Excel 中读取,但为了问题的简洁而避免使用它)。

    @DataProvider(name = "featureTest")
    public Object[][] dataSets() throws Exception {
        return new Object[][] { {"TC_01", "testuser_1", "Test@123", "ABC Street", "123-456-7899" }, 
                                { "TC_02", "testuser_1", "Test@123", "PQR Street", "222-456-7899" }
                               };
    }
    
  2. @Test 方法中,根据功能流程有几种方法-

    @Test(dataProvider = "featureTest")
    public void executeTest(String... data) throws Exception {
    
         try{
    
             feature_1.execute(data);
             feature_2.execute(data);
             feature_3.execute(data);
             feature_4.execute(data);
         }
         catch(Exception e){
             log.error("Error has occured");
         }
    }
    

现在我的主要问题是功能错误可能发生在我在@Test 中指定的这 4 (n) 个方法中的任何地方。

如果任何方法出现异常,我需要“跳过”特定数据集并继续下一个。 例如:TC_01执行过程中,feature_2.execute()发生异常,不应该执行feature_3和feature_4方法。

注意: 我尝试使用@BeforeMethod、@AfterMethod 处理它,但它仍然会通过我想要避免的不需要的方法。

提前感谢您的帮助/输入 && 为这个冗长的问题道歉,虽然是一个相对简单的概念来解释!!!

【问题讨论】:

  • 请注意,我已经尝试过使用 SkipException (TestNG),但是在 TC_01 失败时,它会跳过 TC_02 和其他后续数据集。此外,它也不能达到跳过上述方法的目的。
  • 你的执行方法中有什么?除非你在那里捕捉到你的异常,否则你所说的是不可能的,尽管有 feature_2.execute(data);抛出异常,feature_3 被执行。

标签: java testng


【解决方案1】:

它非常简单。使用返回!

使用条件或尝试catch来评估失败,然后使用return语句;

【讨论】:

    【解决方案2】:

    我能想到的一种方法是工厂方法,

    你的测试类

    class Test{
      Data data;
      Test(Data){
      this.data=data;
    }
    
    
    @Test
    test1(){
      feature_1.execute(data);
    }
    
    @Test
    test2(dependsOnMethods ="test1"){
      feature_2.execute(data);
    }
    
    @Test(dependsOnMethods ="test2")
    test3(){
      feature_3.execute(data);
    }
    
    @Test(dependsOnMethods ="test3")
    test4(){
      feature_4.execute(data);
    }
    
    
    }
    

    在你的工厂类中

    class Factory{
    
    @Factory(DataProvider = "myDP")
    public Object[] factoryTest(Data data){
        new Test(data);
    }
    
    
    @DataProvider
    public Object [][] myDP(){
    
        enter code here
    }
    
    }
    

    【讨论】:

    • 虽然工厂方法是一个很好的解决方案,但我不想为其他人更改现有的编码模式,而且用这种方法移动所有现有脚本将是一个艰难的要求。想知道这是否可以在 TestNG 本身内处理。
    猜你喜欢
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2015-06-04
    相关资源
    最近更新 更多