如何调试使用数据提供者的testng测试?

How to debug testng tests which use dataproviders?

我有一个 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")
public static Object[][] stringSource()
{
    return new Object[][] {
        {"hello", "hell", true},
        {"nice", "iced", false},
        {etc...}
    };
}

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

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

这是一个示例

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 ?

我认为仅仅看框架代码不能说很多。您分享的示例中几乎没有任何内容可供我们参考。