testng_TestNG @工厂注释

testng

TestNG @Factory annotation is used to specify a method as a factory for providing objects to be used by TestNG for test classes. The method marked with @Factory annotation should return Object array.

TestNG @Factory注释用于将方法指定为工厂,以提供TestNG用于测试类的对象。 标有@Factory批注的方法应返回Object数组。

TestNG @工厂注释 (TestNG @Factory Annotation)

TestNG Factory is useful when you want to run multiple test classes through a single test class. Let’s see a quick example of TestNG Factory annotation.

当您想通过一个测试类运行多个测试类时,TestNG Factory很有用。 让我们看一下TestNG Factory批注的快速示例。

Let’s say we have two Test classes with few test methods defined.

假设我们有两个Test类,其中定义了很少的测试方法。

package com.journaldev.utils;

import org.testng.annotations.Test;

public class Test1 {

	@Test
	public void test1() {
		System.out.println("Test1 test method");
	}
}
package com.journaldev.utils;

import org.testng.annotations.Test;

public class Test2 {

	@Test
	public void test2() {
		System.out.println("Test2 test method");
	}
}

Now we can define a Factory method that returns the Object array of above classes.

现在,我们可以定义一个Factory方法,该方法返回上述类的Object数组。

package com.journaldev.utils;

import org.testng.annotations.Factory;

public class TestNGFactory {

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

Below image shows the output of running above class as TestNG test class.

下图显示了将以上类作为TestNG测试类运行的输出。

Here is the output produced in the Eclipse Console.

这是Eclipse控制台中产生的输出。

[RemoteTestNG] detected TestNG version 6.14.3
Test1 test method
Test2 test method
PASSED: test1
PASSED: test2

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


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

Note that if we do the maven build and there are more TestNG test classes present, all of them will also get executed. I have another TestNG test class and below is the output from maven build.

请注意,如果我们进行Maven构建,并且存在更多的TestNG测试类,则它们还将全部执行。 我还有另一个TestNG测试类,下面是maven build的输出。

Running TestSuite
Configuring TestNG with: org.apache.maven.surefire.testng.conf.TestNG652Configurator@3224f60b
Test2 test method
Test3 test method
Test1 test method
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.342 sec

Results :

Tests run: 3, Failures: 0, Errors: 0, Skipped: 0

So if the Test class is getting returned by any factory method, then it’s excluded from the default test suite.

因此,如果任何工厂方法都返回了Test类,则将其从默认测试套件中排除。

We can also create multiple @Factory methods in the same test class or in different test classes.

我们还可以在同一测试类或不同的测试类中创建多个@Factory方法。

使用DataProvider的TestNG工厂 (TestNG Factory with DataProvider)

When we do project build, all our test cases are being executed. So what’s the benefit of using Factory method?

当我们进行项目构建时,我们所有的测试用例都将被执行。 那么使用Factory方法有什么好处?

Well, it’s beneficial when you use it with DataProvider and your test class constructor has arguments.

好吧,将其与DataProvider一起使用并且测试类构造函数具有参数时,这是有益的。

Let’s say our Test1 class is defined as:

假设我们的Test1类定义为:

package com.journaldev.utils;

import org.testng.annotations.Test;

public class Test1 {

	private String str;
	
	public Test1(String s) {
		this.str = s;
	}
	
	@Test
	public void test1() {
		System.out.println("Test1 test method. str = "+str);
	}
}

If we simply run this class as TestNG test class, then it will assign the default value for the string argument of the constructor and produce following output.

如果仅将此类作为TestNG测试类运行,则它将为构造函数的字符串参数分配默认值,并产生以下输出。

Test1 test method. str = Default test
PASSED: test1

In this case, we can use DataProvider along with Factory to provide inputs for the constructor.

在这种情况下,我们可以将DataProvider和Factory一起使用,以为构造函数提供输入。

package com.journaldev.utils;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;

public class TestNGFactory {

	@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"};
	}
}

When we run above test class, it produces following output.

当我们在测试类之上运行时,它将产生以下输出。

Test1 test method. str = A
Test1 test method. str = B
Test2 test method
Test2 test method
PASSED: test1
PASSED: test1
PASSED: test2
PASSED: test2

摘要 (Summary)

TestNG Factory methods are useful when your test classes have constructors with arguments or you want to create the different set of test classes and execute them at once.

当您的测试类具有带参数的构造函数,或者您想要创建不同的测试类集并立即执行它们时,TestNG Factory方法将非常有用。

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

翻译自: https://www.journaldev.com/21237/testng-factory-annotation

testng

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值