可以使用 TestNG DataProvider 和测试套件参数吗?

发布于 2024-07-15 22:09:27 字数 434 浏览 5 评论 0原文

有谁知道是否有办法在使用 @Parameter 注释的同时使用 TestNG DataProvider 进行测试? 我们的测试套件有一些常量配置信息,这些信息通过 @Parameter 注释传递给测试方法。 我们现在希望使用 DataProvider 对一组数据值运行这些测试。

我理解确定结果参数的顺序的内部问题,但如果可能的话我们需要此功能。

有什么想法吗?

在理想的世界中,我可以做这样的事情:

@Test(dataprovider = "dataLoader")
@Parameters("suiteParam")
public void testMethod(String suiteParam, String fromDataParam) {
...
}

Does anyone know if there is a way to use a TestNG DataProvider with a test at the same time as using the @Parameter annotation? Our test suites have some constant configuration information that is being passed to the test methods via the @Parameter annotation. We would now like to use a DataProvider to run these tests over a set of data values.

I understand the internal problem of determining the order the resulting parameters would be in but we need to this feature if possible.

Any thoughts?

In an ideal world, I could do something like this:

@Test(dataprovider = "dataLoader")
@Parameters("suiteParam")
public void testMethod(String suiteParam, String fromDataParam) {
...
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泅人 2024-07-22 22:09:27

嘿,这可能有点笨重,但是为什么不使用 @BeforeClass 方法将 suiteParam 本地存储在测试类的字段上,就像这样。

private String suiteParam;

@BeforeClass
@Parameter("suiteParam")
public void init(String suiteParam) {
  this.suiteParam = suiteParam;
}

这样您就可以按照通常的方式使用数据提供程序,并且仍然可以访问您的套件参数。

Hey, it may be a bit clunky but why don't you use a @BeforeClass method to store the suiteParam locally on a field of the test class like so.

private String suiteParam;

@BeforeClass
@Parameter("suiteParam")
public void init(String suiteParam) {
  this.suiteParam = suiteParam;
}

This way you can use your data providers in the usual way and still have access to your suite param.

清君侧 2024-07-22 22:09:27

是的,使用 TestNG 的依赖注入功能。 您可以访问 DataProvider 中所有定义的参数。 这是需要 test_param 参数的一些示例 DataProvider:通过

@DataProvider(name = "usesParameter")
public Object[][] provideTestParam(ITestContext context) {
    String testParam = context.getCurrentXmlTest().getParameter("test_param");
    return new Object[][] {{ testParam }};
}

这种方式,您可以在 DataProvider 中收集配置和生成的参数,然后将其用于测试。 有关 ITestContext 类的详细信息,请参阅 TestNG JavaDoc

Yes, using the using TestNG's dependency injection capabilies. You can access all defined parameters in your DataProvider. This is some example DataProvider in need of the test_param parameter:

@DataProvider(name = "usesParameter")
public Object[][] provideTestParam(ITestContext context) {
    String testParam = context.getCurrentXmlTest().getParameter("test_param");
    return new Object[][] {{ testParam }};
}

This way you can collect configured and generated parameters in a DataProvider which is then used for your test. See the TestNG JavaDoc for details on the ITestContext class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文