TestNG 禁用某些数据集 dataProvider



我有一个测试,它使用一个数据提供程序,我只想执行 2 次运行中的一次,因为其中一个测试预计会失败。

@Test(dataProvider = "data")
public void testThatPassesAndFails(boolean test, String whatever) {
Assert.assertTrue(test);//Once scenario passes, other fails
}
@DataProvider(name = "data")
public static Object[][] paramData() {
return new Object[][] {
{true, "blah"},
{false, "blah2"}
};
}

实际上,我们有一个自定义注释,将测试绑定到跟踪预期测试失败的测试数据库。对于非数据提供程序测试,我们使用IAnnotationTransformer并调用annotation.setEnabled(false),这适用于单实例测试。

但是,我们有一些数据提供程序测试,我们希望运行除 1 或 2 个数据集之外的所有数据提供程序测试。是否可以仅禁用或删除某些调用?我现在能得到的最接近的是实现IInvokedMethodListener并在beforeInvocation方法中抛出SkipException。这还不错,但我宁愿测试结果中不要跳过,因为它们会损害及格率。我知道我也可以使用另一个侦听器来操纵 TestNG 结果,但这会变得混乱。

有没有办法使用IAnnotationTransformerIAnnotationTransformer2来获取使用 dataProvider 的测试的每个迭代的参数值,然后禁用?即使我可以操纵 dataProvider 方法来删除"坏"情况,那也会起作用。

在高层次上,这是你解决这个问题的方法:

  1. 在套件 xml 文件中向<suite>(或)<test>标记添加一个参数,以指示过滤。
  2. 让测试类实现IHookable接口。
  3. IHookable实现中,检查数据提供程序传递给实际测试方法的参数是否满足 (1) 中提到的过滤条件。
  4. 如果筛选条件通过,则运行测试方法,否则将状态显式设置为SKIP并设置自定义异常。
  5. 生成一个TestListenerAdaptor自定义项,该自定义项筛选出状态为SKIP且其例外为自定义异常的所有测试结果。

这应该可以解决问题。

下面是测试类的外观:

import org.testng.Assert;
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.Arrays;
public class TestclassSample implements IHookable {
@Override
public void run(IHookCallBack callBack, ITestResult testResult) {
Object[] parameters = callBack.getParameters();
String runType = parameters[0].toString();
String expectedInputType = testResult.getTestContext().getCurrentXmlTest().getAllParameters().get("runType");
if (runType.equalsIgnoreCase(expectedInputType)) {
callBack.runTestMethod(testResult);
} else {
testResult.setStatus(ITestResult.SKIP);
testResult.setThrowable(new DataSetSkipException("Skipping data set " + Arrays.toString(parameters) ));
}
}
@Test(dataProvider = "dp")
public void testMethod(String inputType, int number) {
String expectedInputType = Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getAllParameters().get("runType");
Assert.assertTrue(number > 0);
Assert.assertEquals(inputType, expectedInputType);
System.err.println("(" + inputType + ", " + number + ")");
}
@DataProvider(name = "dp")
public Object[][] getData() {
return new Object[][]{
{"bad", 100},
{"good", 500},
{"good", 600},
{"good", 200},
{"new", 300}
};
}
public static class DataSetSkipException extends RuntimeException {
public DataSetSkipException(String message) {
super(message);
}
}
}

以下是侦听器的外观:

import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class BadResultRemovingListener extends TestListenerAdapter {
@Override
public void onFinish(ITestContext testContext) {
Iterator<ITestResult> skipTestsIterator = testContext.getSkippedTests().getAllResults().iterator();
List<ITestNGMethod> methodsToRemove = new ArrayList<>();
while (skipTestsIterator.hasNext()) {
ITestResult result = skipTestsIterator.next();
if (result.getThrowable() instanceof TestclassSample.DataSetSkipException) {
methodsToRemove.add(result.getMethod());
}
}
for (ITestNGMethod method : methodsToRemove) {
testContext.getSkippedTests().removeResult(method);
}
}
}

TestNG 套件 xml 文件如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="46472511_Suite" parallel="false" verbose="2">
<listeners>
<listener class-name="com.rationaleemotions.stackoverflow.qn46472511.BadResultRemovingListener"/>
</listeners>
<parameter name="runType" value="good"/>
<test name="46472511_test1">
<classes>
<class name="com.rationaleemotions.stackoverflow.qn46472511.TestclassSample"/>
</classes>
</test>
</suite>

当您从 IDE 中运行它时,您可能会看到一些尴尬的结果,但是当您从构建工具(例如 Maven)运行时,您的输出将如下所示:

11:36 $ mvn clean test -DsuiteXmlFile=46472511.xml
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building testbed 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO]
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ testbed ---
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running TestSuite
...
... TestNG 6.12 by Cédric Beust (cedric@beust.com)
...
(good, 500)
(good, 600)
(good, 200)
Tests run: 5, Failures: 0, Errors: 0, Skipped: 2, Time elapsed: 0.313 sec - in TestSuite
Results :
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.783 s
[INFO] Finished at: 2017-09-29T11:36:21+05:30
[INFO] Final Memory: 37M/331M
[INFO] ------------------------------------------------------------------------

最新更新