如何调试使用数据提供程序的 testng 测试? [英] How to debug testng tests which use dataproviders?

查看:29
本文介绍了如何调试使用数据提供程序的 testng 测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 testng 测试方法,它使用数据提供程序来获取测试输入.我只想在测试为第二个测试数据输入运行时调试测试.我怎么做 ?我应该如何设置断点?

这是一些示例代码.

@Test(dataProvider = "myDataProvider")public void findStringInString(String input, String toFind, boolean found){Assert.assertEquals(finder(input, toFind), found, "Could not find " + toFind + " in " + input);}@DataProvider(name = "myDataProvider")公共静态对象[][] stringSource(){返回新对象[][] {{你好",地狱",真实},{不错",冰",假},{等等...}};}

PS - 顺便说一句,这段代码看起来像反模式还是不好的做法?

解决方案

确定在哪里设置调试点的最简单方法是维护一个计数器来检查正在运行哪个数据提供程序迭代,然后当值达到您想要的值时值,执行调试打印语句并在该行中设置断点.

这是一个示例

import java.util.concurrent.atomic.AtomicInteger;导入 org.testng.annotations.DataProvider;导入 org.testng.annotations.Test;公共类 SampleTestCase {private final AtomicInteger counter = new AtomicInteger(1);@Test(dataProvider = "getData")公共无效测试方法(字符串){如果(计数器.getAndIncrement()== 2){System.err.println("调试模式开启");//在这一行设置断点}System.err.println("你好" + s);}@数据提供者公共对象[][] getData() {返回新对象[][]{{"TestNG"},{"Spring"},{"Selenium"}};}}

<块引用>

顺便说一句,这段代码看起来像反模式还是不好的做法?

我认为仅查看骨架代码并不能说明很多问题.您分享的示例中几乎没有任何我们可以用来建议的内容.

I have a testng test method which uses a dataprovider to get test inputs. I want to debug the test only when the test runs for the 2nd test data input. How do I do that ? How should I set the breakpoint ?

Here is some sample code.

@Test(dataProvider = "myDataProvider")
public void findStringInString(String input, String toFind, boolean found){
    Assert.assertEquals(finder(input, toFind), found, "Could not find " + toFind + " in " + input);
}

@DataProvider(name = "myDataProvider")
public static Object[][] stringSource()
{
    return new Object[][] {
        {"hello", "hell", true},
        {"nice", "iced", false},
        {etc...}
    };
}

PS - As an aside, does this code look like an anti-pattern or bad practice ?

解决方案

The easiest way of determining where to setup the debug point is to maintain a counter that checks which data provider iteration is being run and then when the value reaches your desired value, do a debug print statement and set the breakpoint in that line.

Here's a sample

import java.util.concurrent.atomic.AtomicInteger;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class SampleTestCase {

  private final AtomicInteger counter = new AtomicInteger(1);

  @Test(dataProvider = "getData")
  public void testMethod(String s) {
    if (counter.getAndIncrement() == 2) {
      System.err.println("Debug mode on"); //Set a breakpoint on this line
    }
    System.err.println("Hello " + s);

  }

  @DataProvider
  public Object[][] getData() {
    return new Object[][]{
        {"TestNG"},{"Spring"},{"Selenium"}
    };
  }

}

As an aside, does this code look like an anti-pattern or bad practice ?

I dont think a lot can be said by just looking at the skeleton code. There's hardly anything in the sample that you shared that we can use to suggest.

这篇关于如何调试使用数据提供程序的 testng 测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆