我有这个测试,它使用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
并在SkipException
方法中抛出beforeInvocation
。这不是坏事,但我宁愿不在测试结果中跳过,因为它们会影响通过百分比。我知道我可以使用另一个监听器来操纵TestNG结果,但这会变得混乱。
是否有某种方法使用IAnnotationTransformer
或IAnnotationTransformer2
来获取使用dataProvider的测试的每次迭代的参数值,然后禁用?即使我可以操纵dataProvider方法来删除"坏"案件,这将有效。
答案 0 :(得分:0)
从较高的层面来说,这就是你解决这个问题的方法:
<suite>
(或)<test>
标记中添加一个参数,以指示过滤。IHookable
接口。IHookable
实现中检查数据提供者传递给实际测试方法的参数是否满足(1)中提到的过滤条件。SKIP
并设置自定义异常。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] ------------------------------------------------------------------------