我偶然发现了同样的问题,接下来是我的解决方案。在我的情况下,只需将参数转储到 sysout 即可查看测试方法失败的参数。如果您在测试后需要手头的参数,您可以(希望 :) )轻松地扩展此解决方案。
所以,我们开始...
将您的参数包装成一些复合材料,并确保您覆盖该toString()
方法:
public interface CommonTestParameter {
String getStuff();
Object getMoreStuff();
}
public class TestParameterImpl implements CommonTestParameter {
private final String stuff;
private final Object moreStuff;
public TestParameterImpl(String aStuff, Object aMoreStuff){
this.stuff = aStuff;
this.moreStuff = aMoreStuff;
}
@Override
public String getStuff(){
return this.stuff;
}
@Override
public Object getMoreStuff(){
return this.moreStuff;
}
@Override
public String toString(){
return "stuff = \"" + this.stuff + "\", moreStuff = \"" + this.moreStuff.toString() + "\"";
}
}
为您的测试类创建一个抽象父类:
public abstract class AbstractTestParent implements ITest {
private String testName = "";
// This method actually is what provides you with the functionality of renaming
// your tests for the standard testNG logger
@Override
public String getTestName(){
return this.testName;
}
protected abstract CommonTestParameter getTestParameter();
@BeforeMethod
public void beforeMethod(Method aMethod) {
this.testName = aMethod.getName() + "[" + getTestParameter().toString() + "]";
}
}
最后创建测试类:
public class TestClassImpl extends CommonTestParent {
private CommonTestParameter testParam;
@Override
protected CommonTestParameter getTestParameter(){
return this.testParam;
}
@DataProvider(name = "awesomeDataProvider")
public static Iterator<Object[]> provideData(){
List<Object[]> dataList = new LinkedList<Object[]>();
dataList.add(new TestParameterImpl("Stuff1!", "More stuff1!"));
dataList.add(new TestParameterImpl("Stuff2!", "More stuff2!"));
return dataList.iterator();
}
@Factory(dataProvider = "awesomeDataProvider")
public TestClassImpl(CommonTestParameter aTestParam){
this.testParam = aTestParam;
}
@Test
public void testStuff(){
System.out.println(this.testParam.getStuff());
}
@Test
public void testMoreStuff(){
System.out.println(this.testParam.getMoreStuff().toString());
}
}
现在,无论何时运行这些测试,而不是标准格式的控制台输出
PASSED: testStuff
PASSED: testStuff
PASSED: testMoreStuff
PASSED: testMoreStuff
你实际上会看到你用你通过覆盖toString()
方法指定的格式调用了测试方法的参数。
PASSED: testStuff[stuff = "Stuff1!", moreStuff = "More stuff1!"]
PASSED: testStuff[stuff = "Stuff2!", moreStuff = "More stuff!"]
PASSED: testMoreStuff[stuff = "Stuff1!", moreStuff = "More stuff1!"]
PASSED: testMoreStuff[stuff = "Stuff2!", moreStuff = "More stuff2!"]
如果您确实需要进一步处理参数,您可以迭代此概念并将它们存储到 a Map<String, List<CommonTestParameter>>
(其中键表示方法名称,值表示您调用该方法的所有参数),然后在某个@AfterSuite
方法中解析它。
希望这会有所帮助,珍妮克。