Testng: 添加自定义测试方法参数注入的API

创建于 2015-03-03  ·  12评论  ·  资料来源: cbeust/testng

我需要一种将自定义参数注入到 TestNG 的测试方法中的方法。 参数是我自己在运行时从测试方法注释动态构造的对象。 TestNG 不知道自定义参数,它是我自己的类型,例如:

@com.mypackage.MyAnnotation("someValue)"
@org.testng.Test
public void test(MyCustomObject val) {
    ...
}

看起来现在我不能轻易做到。

我们可以添加这样的功能/SPI吗? 目前实现这一目标的最简单方法是什么?

injection Feature request

最有用的评论

我有同样的需求会非常有用

所有12条评论

我不想使用@DataProvider注释。

你为什么不想使用@DataProvider ,这是迄今为止最简单的方法
(只需几行代码)。

塞德里克

2015 年 3 月 3 日星期二上午 6:09,Crazyjavahacking通知@github.com
写道:

_我不想使用。_

直接回复此邮件或在 GitHub 上查看
https://github.com/cbeust/testng/issues/612#issuecomment -76951265。

问题是我要注入的对象是根据测试方法注释中的数据动态创建的。

在我最初的示例中,我需要解析@MyAnnotation的内容,并基于此创建MyCustomObject的实例。

是什么阻止您在数据提供程序中执行所有操作?

塞德里克

2015 年 3 月 3 日星期二下午 3:12,Crazyjavahacking通知@github.com
写道:

问题是我要注入的对象是动态创建的
基于测试方法注释中的数据。

在我最初的例子中,我需要解析@MyAnnotation的内容和
基于这一点,我需要创建一个 MyCustomObject 的实例。

直接回复此邮件或在 GitHub 上查看
https://github.com/cbeust/testng/issues/612#issuecomment -77060429。

该功能并不是真正的参数化测试,我也不想使用数据提供程序,例如:

@Columns("col1 | col2 || val)"
<strong i="6">@Rows</strong>  ({"a    | b    || c",
         "d    | e    || f})
@org.testng.Test
public void test1(MyCustomObject val) {
    // MyCustomerObject is created from the annotation values
    // some menipulation with val ...
}

@Columns("col1 | col2 || val)"
<strong i="7">@Rows</strong>  ({"1    | 2    || a",
         "3    | 3    || b})
@org.testng.Test
public void test1(MyCustomObject val) {
    // MyCustomerObject is created from the annotation values
    // some menipulation with val ...
}

所以我不认为数据提供者在这里是一个好方法

我有同样的需求会非常有用

这个想法很有趣,但正如@cbeust很久以前所说的那样,数据提供者是一个很好的解决方法。
根据之前的示例:

@Columns("col1 | col2 || val)")
@Rows(  {"a    | b    || c",
         "d    | e    || f"})
@Test(dataProvider = "dp")
public void test1(MyCustomObject val) {
    // MyCustomerObject is created from the annotation values
    // some menipulation with val ...
}

@Columns("col1 | col2 || val)"
@Rows(  {"1    | 2    || a",
         "3    | 3    || b"})
@Test(dataProvider = "dp")
public void test1(MyCustomObject val) {
    // MyCustomerObject is created from the annotation values
    // some menipulation with val ...
}

<strong i="8">@DataProvider</strong>
public static MyCustomObject[] dp(Method m) {
    Columns colums = m.getAnnotation(Columns.class);
    Rows rows = m.getAnnotation(Rows.class);
    MyCustomObject[] results = new MyCustomObject[rows.values().length];
    for (int i=0; i < rows.values().length; i++) {
        String value = rows.values()[i];
        // extract params with custom business
        results[i] = new MyCustomObject(/* params */);
    }
    return results;
}

这是真的@juherr ,我以这种方式结束了。 但是,唯一缺少的是为所有测试设置默认数据提供者的能力,在我的情况下,我的数据提供者是泛型,只有在有某些注释时才通过

我认为将 dataProvider 和添加设置默认提供程序或提供程序类的能力相结合,可以解决问题,并会打开一个新的可能性

@g13013你的意思是这样的

public class UseMyDataProvider implements IAnnotationTransformer {
  public void transform(ITest annotation, Class testClass, Constructor testConstructor, Method testMethod) {
    if (/* business logic (for example: testMethod has expected annotations) */) {
      annotation.setDataProviderClass(MyExternalClassWithDataProvider.class);
      annotation.setDataProvider("dataprovider name");
    }
  }
}

我也希望能够使用 Guice 在测试中注入参数。
DataProvider + IAnnotationTransformer 是一个有趣的解决方法,但它缺乏灵活性(例如,不可能通过@Listeners 设置 IAnnotationTransformer)

不可能通过@Listeners设置 IAnnotationTransformer

@gjuillot这可能是一个功能请求。 你能打开一个问题吗?

我会去做的。 但是,对我来说最好的还是有可能使用 Guice 在测试中注入参数。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

eskatos picture eskatos  ·  17评论

erikgb picture erikgb  ·  8评论

blackduck-joe picture blackduck-joe  ·  13评论

Drimix20 picture Drimix20  ·  18评论

juherr picture juherr  ·  11评论