testng数据驱动_TestNG数据提供者

testng数据驱动

DataProvider is one of the best features of TestNG framework. We can use TestNG DataProvider to inject arguments into our test methods.

DataProvider是TestNG框架的最佳功能之一。 我们可以使用TestNG DataProvider将参数注入到我们的测试方法中。

TestNG数据提供者 (TestNG DataProvider)

TestNG DataProvider helps us in creating loosely coupled test cases. We can separate testing logic and input data into different methods, this way we can also reuse the input data from a DataProvider for multiple test methods.

TestNG DataProvider帮助我们创建松散耦合的测试用例。 我们可以将测试逻辑和输入数据分成不同的方法,这样我们还可以将来自DataProvider的输入数据重用于多种测试方法。

Let’s see how we can create a simple DataProvider for a TestNG class using Eclipse plugin. In Eclipse, go to New | Other | TestNG Class and in the wizard screen, select @DataProvider annotation.

让我们看看如何使用Eclipse插件为TestNG类创建一个简单的DataProvider。 在Eclipse中,转到新建| 其他| TestNG Class,然后在向导屏幕中,选择@DataProvider批注。

When you click on Finish button, we get a default implementation of TestNG test class with DataProvider method.

当您单击Finish按钮时,我们将使用DataProvider方法获得TestNG测试类的默认实现。

package com.journaldev.dataprovider;

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

public class Test4 {
  @Test(dataProvider = "dp")
  public void f(Integer n, String s) {
	  
  }

  @DataProvider
  public Object[][] dp() {
    return new Object[][] {
      new Object[] { 1, "a" },
      new Object[] { 2, "b" },
    };
  }
}

Note that it’s not necessary to use Eclipse TestNG plugin, but it helps us in easily creating a base class and then change it according to our project use case.

请注意,不必使用Eclipse TestNG插件,但它可以帮助我们轻松创建基类,然后根据项目用例对其进行更改。

Notice that dp() method is annotated with @DataProvider annotation and our we have used it in @Test configuration.

注意, dp()方法带有@DataProvider注释,我们已经在@Test配置中使用了它。

DataProvider method must return Object array and its dimension must match to the test method. Since our test method has two arguments, our DataProvider method must return Object[][] so that it gets mapped to the method arguments.

DataProvider方法必须返回Object数组,并且其维数必须与测试方法匹配。 由于我们的测试方法具有两个参数,因此我们的DataProvider方法必须返回Object[][]以便将其映射到方法参数。

Just run the above class as TestNG test and we will get following output in the console.

只需运行上述类作为TestNG测试,我们将在控制台中获得以下输出。

[RemoteTestNG] detected TestNG version 6.14.3
PASSED: f(1, "a")
PASSED: f(2, "b")

TestNG @DataProvider属性 (TestNG @DataProvider Properties)

@DataProvider annotation has three properties:

@DataProvider批注具有三个属性:

  1. name: This is used to specify the data provider method name, default value is the function name. We can use it to give a proper name to our Data Provider method.

    name :用于指定数据提供者方法的名称,默认值为函数名称。 我们可以使用它为我们的数据提供者方法赋予适当的名称。
  2. indices: This is a very important property, this allows us to specify the indexes we want to use for our testing purpose. This is helpful when our data provider method may have a large number of values and we want to run our test cases for only a few of them. Its default value is “all” so tests will run for all the inputs generated by the data provider method.

    indices :这是一个非常重要的属性,它使我们可以指定要用于测试目的的索引。 当我们的数据提供者方法可能包含大量值并且我们只希望对其中一些进行测试时,这很有用。 它的默认值是“ all”,因此将对由数据提供者方法生成的所有输入运行测试。
  3. parallel: This parameter tells TestNG to run tests in parallel. So the input to test methods can be injected in random order. By default inputs are injected in the order and tests are executed sequentially.

    parallel :此参数告诉TestNG并行运行测试。 因此,可以随机顺序注入测试方法的输入。 默认情况下,将按顺序注入输入,并按顺序执行测试。

Let’s see an example where I have changed the name of DataProvider method and also using only a few of the inputs generated by the method.

让我们看一个示例,其中我更改了DataProvider方法的名称,并且还仅使用了该方法生成的一些输入。

package com.journaldev.dataprovider;

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

public class Test4 {
  @Test(dataProvider = "fooDP")
  public void foo(Integer n) {
	  System.out.println("Inside foo, input = "+n);
  }

  @DataProvider(name="fooDP", indices= {1,3,5,7,9})
  public Object[] dp() {
	  Object[] ints = new Object[100];
	  for(int i =0 ; i<100; i++) {
		  ints[i] = 100+i;
	  }
    return ints;
  }
}

Now when we run our test class, it generates following output.

现在,当我们运行测试类时,它将生成以下输出。

[RemoteTestNG] detected TestNG version 6.14.3
Inside foo, input = 101
Inside foo, input = 103
Inside foo, input = 105
Inside foo, input = 107
Inside foo, input = 109
PASSED: foo(101)
PASSED: foo(103)
PASSED: foo(105)
PASSED: foo(107)
PASSED: foo(109)

Now change the DataProvider method to run in parallel.

现在,将DataProvider方法更改为并行运行。

@DataProvider(parallel=true, name="fooDP", indices= {1,3,5,7,9})
public Object[] dp() {
  //method logic
}

The output generated when test class is executed:

执行测试类时生成的输出:

[RemoteTestNG] detected TestNG version 6.14.3
Inside foo, input = 105
Inside foo, input = 107
Inside foo, input = 109
Inside foo, input = 103
Inside foo, input = 101
PASSED: foo(103)
PASSED: foo(105)
PASSED: foo(107)
PASSED: foo(109)
PASSED: foo(101)

Note that indices values don't cause any exceptions if the DataProvider is returning fewer numbers of objects than specified. So below DataProvider implementation will work fine too.

请注意,如果DataProvider返回的对象数少于指定的数量,则indices值不会引起任何异常。 因此,下面的DataProvider实现也可以正常工作。

@DataProvider(parallel = true, name = "fooDP", indices = { 1, 3, 5, 7, 9 })
public static Object[] dp() {
	Object[] ints = new Object[3];
	ints[0] = 0;ints[1] = 1;ints[2] = 2;
	return ints;
}

TestNG DataProvider类 (TestNG DataProvider Class)

TestNG is designed to be loosely coupled, so it's no wonder that we have the option to separate our test class with data provider class. We can use @Test property dataProviderClass to provide the name of the class to look for DataProvider method.

TestNG被设计为松散耦合的,因此也难怪我们可以选择将测试类与数据提供者类分开。 我们可以使用@Test属性dataProviderClass提供要查找DataProvider方法的类的名称。

If this attribute is specified, the data provider method needs to be static in the specified class. If not specified, the data provider will be looked in the test class or its superclasses.

如果指定了此属性,则数据提供者方法在指定的类中必须是静态的。 如果未指定,则将在测试类或其超类中查找数据提供者。

Here is an example of separate TestNG DataProvider class.

这是一个单独的TestNG DataProvider类的示例。

package com.journaldev.dataprovider;

import org.testng.annotations.DataProvider;

public class Test4DataProvider {

	@DataProvider(parallel = true, name = "fooDP", indices = { 1, 3, 5, 7, 9 })
	public static Object[] dp() {
		Object[] ints = new Object[100];
		for (int i = 0; i < 100; i++) {
			ints[i] = 100 + i;
		}
		return ints;
	}
}

Our updated Test class:

我们更新的测试类:

package com.journaldev.dataprovider;

import org.testng.annotations.Test;

public class Test4 {
	@Test(dataProvider = "fooDP", dataProviderClass = Test4DataProvider.class)
	public void foo(Integer n) {
		System.out.println("Inside foo, input = " + n);
	}
}

带工厂的TestNG DataProvider (TestNG DataProvider with Factory)

We can use DataProvider with Factory methods too. Below code snippet shows a simple example for using DataProvider with Factory annotation.

我们也可以将DataProviderFactory方法一起使用。 下面的代码片段显示了一个使用带有Factory批注的DataProvider的简单示例。

@Factory(dataProvider = "dp")
public Object[] getTestClasses(String s) {
	Object[] tests = new Object[2];
	tests[0] = new Test1(s);
	tests[1] = new Test2();
	return tests;
}

@DataProvider
public Object[] dp() {
	return new Object[] {"A", "B"};
}

I have explained the use case and the benefits of using DataProvider with Factory in detail at TestNG Factory annotation tutorial.

我已经在TestNG Factory批注教程中详细解释了用例以及将DataProvider与Factory一起使用的好处。

摘要 (Summary)

TestNG is an awesome testing framework. DataProvider is one of the best features it has that I totally love and use a lot in my test cases.

TestNG是一个很棒的测试框架。 DataProvider是它最好的功能之一,我完全喜欢并在测试用例中使用了很多功能。

GitHub Repository. GitHub Repository下载项目源代码。

翻译自: https://www.journaldev.com/21257/testng-dataprovider

testng数据驱动

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值