订购jbehave故事按顺序运行

时间:2012-08-22 18:15:18

标签: java integration-testing jbehave

我试图按顺序运行jbehave故事。

我的集成测试的包结构如下所示

src/it/some/package/name/packageA/a.story
src/it/some/package/name/packageB/b.story
src/it/some/package/name/c.story

我希望故事以这个顺序运行a.story,b.story,c.story

我尝试在jBehave中使用GivenStories,但它们似乎没有用(可能我没有正确指定它们)。如果有人能指出GivenStories文本的创建,并且在运行Integration测试时jbehave如何创建排序,我会非常感激,因为我看到在我的机器和jenkins上运行的故事似乎在屈服不同的执行顺序。

非常感谢任何帮助。谢谢!

3 个答案:

答案 0 :(得分:2)

猜猜这个回应可能有点迟了,但无论如何。我们也在试用JB,似乎仍有很多问题围绕着这项工作,尤其是对任何现实世界而言。

我们已经在一个模块中提供了故事,但是如果您尝试跨模块调用(例如,您有一个依赖jar,它包含您想要调用的常见故事,那么这似乎根本不起作用) 。

在同一模块中,请确保将GS条目放在故事中的正确位置,如下所示:

Story: Running BBB

GivenStories: com/xxx/test/stories/test_aaa_user.story

Given a BBB string
When I set BBB to activate
Then the BBB string is set to activate

这会在BBB之前运行AAA故事。

答案 1 :(得分:1)

我实际上已经找到了解决这个问题的方法,我认为这比GivenStories

方便得多

首先我添加了像这样的maven surefire配置

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>${surefire.version}</version>
  <configuration>
    <includes>
       <include>**/*TesterSequence.java</include>
    </includes>
  </configuration>
</plugin>

SampleTesterSequence的结构如下所示

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;



@RunWith(Suite.class)
@Suite.SuiteClasses({ A.class,B.class, 
        C.class })
public class SampleTesterSequence {
    @BeforeClass
    public static void beforeStories() throws Exception {
       //TODO Implement before story steps
    }

    @AfterClass
    public static void afterStories() throws Exception {
      //TODO Implement after story steps
    }
}

正如你所看到的那样,套件将按照我提到的套件顺序运行故事a,b,c,当surefire运行测试时,它会查找以TesterSequence结尾的模式并运行该类首先,从该类开始,它按照指定的顺序执行我们想要运行的故事。

答案 2 :(得分:0)

发生这种情况的原因是因为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
相关问题