简体   繁体   English

使用 DataProvider 将 lambdas 作为 testng 参数传递?

[英]Passing lambdas as testng parameters using DataProvider?

I was wondering if there is a way to pass lambdas from testng's data providers.我想知道是否有办法从 testng 的数据提供者传递 lambda。 So I want to do something like the following:所以我想做如下的事情:

    @DataProvider(name="checkProvider")
    public Object[][] checkProvider() {
        return new Object[][]{
            "Some string to test", (String string) -> string.length() > 2 //A predicate on how to test response
        }
    }

And after that have some test like so在那之后有一些像这样的测试

@Test(dataProvider="checkProvider")
public void testsomething(String s, Predicate<String> checker) {
    //so some operation on s to get a string I want to check
    Assert.assertTrue(checker.check(someString));
}

Right now I'm not able to do this as I get Target type of lambda conversion must be an interface .现在我无法执行此操作,因为我得到Target type of lambda conversion must be an interface Does anyone have an idea on how to do this or even an alternative would be good so I could achieve the desired functionality.有没有人知道如何做到这一点,甚至替代方案会很好,这样我就可以实现所需的功能。

EDIT: The answer is in the first comment.编辑:答案在第一条评论中。 I was trying to pass a lambda directly but if you first declare it and then pass it in Object[][] then it works fine.我试图直接传递 lambda 但是如果你先声明它然后在 Object[][] 中传递它,那么它工作正常。

So in real world scenarios, I usually use lambda expression with functional interface to perform some operations on the data we get from excel sheet.所以在现实世界的场景中,我通常使用带有函数接口的 lambda 表达式对我们从 excel 表中获取的数据执行一些操作。 You may know that sometimes it may happen that your excelsheet does not have value in a column then in order to provide the default value.您可能知道,有时您的 excelsheet 可能会在列中没有值,然后才能提供默认值。 sometimes we use this.有时我们使用它。 Currently I have created a simple example in which it is retrieving the data from the list.目前我已经创建了一个简单的例子,它从列表中检索数据。 Data stored in the list contains whitespaces that we do not want.存储在列表中的数据包含我们不想要的空格。 So in order to achieve the same, I have used functional interface.所以为了达到同样的效果,我使用了功能接口。

Functional Interface :功能接口

package testng.package1;


@FunctionalInterface
public interface Trimmer {

    String getTrimmedString(String value);

}

TestClass :测试类

package testng.package1;

import java.util.ArrayList;
import java.util.List;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestClass
{


    List<String> dataList = new ArrayList<>();
    {
        dataList.add("Tomato");
        dataList.add(" Cucumber");
        dataList.add("Onion ");
        dataList.add(" Vegetable that you like ");
    }

    public String trim(String data)
    {
        Trimmer trim = (value) -> { return value.trim(); };

        return trim.getTrimmedString(data);
    }


    @DataProvider(name = "data")
    public Object[][] getData()
    {
        Object[][] data = { {"veg1", trim(dataList.get(0))}, {"veg2", trim(dataList.get(1))}, {"veg3", trim(dataList.get(2))}, {"veg4", trim(dataList.get(3))}, };

        return data;
    }


    @Test( dataProvider = "data" )
    public void test(String vegId, String vegVal)
    {
        System.out.println("Vegetable ID: " + vegId + " : " + "Vegetable Name: " + vegVal);
    }

}

Output : Output

[RemoteTestNG] detected TestNG version 7.0.1
Vegetable ID: veg1 : Vegetable Name: Tomato
Vegetable ID: veg2 : Vegetable Name: Cucumber
Vegetable ID: veg3 : Vegetable Name: Onion
Vegetable ID: veg4 : Vegetable Name: Vegetable that you like
PASSED: test("veg1", "Tomato")
PASSED: test("veg2", "Cucumber")
PASSED: test("veg3", "Onion")
PASSED: test("veg4", "Vegetable that you like")

===============================================
    Default test
    Tests run: 4, Failures: 0, Skips: 0
===============================================

As requested here is my comment as an answer:根据要求,这是我的评论作为答案:

You can do:你可以做:

Predicate<String> p = string -> string.length() > 2; 
public Object[][] dataProviderMethod() { 
    return new Object[][] { { "Some string to test" }, { p } }; 
} 

I don't know how useful it is with TestNG.我不知道它对 TestNG 有多大用处。

You need just cast array element to Function<String, Boolean>您只需要将数组元素转换为 Function<String, Boolean>

public Object[][] dataProviderMethod() { 
    return new Object[][] {{ (Function<String, Boolean>) (string -> string.length() > 2) }}; 
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM