发生这种情况的原因是因为 JBehave 按照它在文件系统中找到它们的顺序运行测试。为避免这种情况,您可以扩展 JBehave 的 StoryFinder 类并覆盖 findClassNames() 以使用存储在某处的有序列表(属性文件、build.xml 等):
@Override
public List<String> findClassNames(String searchIn, List<String> includes, List<String> excludes) {
String[] orderedTestListArray = retrtieveTestNamesFromBuildXml();
List<String> scannedTestList = scan(searchIn, includes, excludes);
System.out.println("Scanned Test List: " + scannedTestList);
List<String> finalTestList = new ArrayList<String>();
for(String x: orderedTestListArray) {
for(String y: scannedTestList) {
if(y.contains(x))
finalTestList.add(y);
}
}
System.out.println("Final Ordered Test List: " + finalTestList);
return classNames(normalise(finalTestList));
}
在这种情况下,我通过解析我的 ant build.xml 文件来检索orderedTestListArray,该文件包含我想要运行的有序测试列表:
private String[] retrtieveTestNamesFromBuildXml() {
String[] orderedTestListArray = null;
InputStream iStream = null;
try {
File file = new File("build.xml");
if(file.exists()) {
iStream = new FileInputStream(file);
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(iStream);
NodeList propertyNodes = doc.getElementsByTagName("property");
String orderedTestListString = null;
for (int i = 0; i < propertyNodes.getLength(); i++) {
Element elementNode = (Element) propertyNodes.item(i);
if(elementNode.getAttribute("name").equals("xed.tests.to.run")) {
orderedTestListString = elementNode.getAttribute("value");
break;
}
}
orderedTestListArray = orderedTestListString.split(",");
for(int i = 0; i <= orderedTestListArray.length-1; i++) {
orderedTestListArray[i] = orderedTestListArray[i].trim();
orderedTestListArray[i] = orderedTestListArray[i].substring(3, orderedTestListArray[i].length());
}
}
}
catch (Exception e) {
System.out.println("Error parsing XML info from build.xml");
e.printStackTrace();
System.exit(1);
}
finally {
try
{
if(iStream != null)
iStream.close();
}
catch (IOException e)
{
System.out.println("Error closing InputStream for build.xml");
e.printStackTrace();
}
}
return orderedTestListArray;
}
最后,确保在从 ant 或 maven 运行时指定这个新的 StoryFinder 类,如下所示:
storyFinderClass=fullyQualifiedNameOfNewStoryFinderClass