在@BeforeStory中获取故事名



我想在带有@BeforeStory注释的方法中获得Story名称。

我需要这个调试的目的,因为我正在运行一堆故事与runStoriesAsPaths和多线程,我试图记录哪个线程正在运行哪个故事。

有办法做到这一点吗?

首先你需要创建一个新的StoryReporter(扩展那个类)。在该类中,您可以在故事/场景/步骤之前/之后添加要执行的操作,并且您有故事名称。例子:

public class NewStoryReporter implements StoryReporter {
private StoryReporter delegate;
public NewStoryReporter(StoryReporter delegate) {
    this.delegate = delegate;
}
@Override
public void beforeStory(Story story, boolean givenStory) {
    delegate.beforeStory(story, givenStory);
}
@Override
public void beforeScenario(String scenarioTitle) {
    delegate.beforeScenario(scenarioTitle);
}
@Override
public void beforeStep(String step) {
    if(step.equals("When after each step")){
        return;
    }
    delegate.beforeStep(step);
}

那么你需要扩展StoryReporterBuilder,这将创建你的NewStoryReporter。例子:

public class NewStoryReporterBuilder extends StoryReporterBuilder {
@Override
public StoryReporter build(String storyPath) {
    StoryReporter delegate = super.build(storyPath);
    return new NewStoryReporter(delegate);
}
}
然后在你的配置中,创建一个NewStoryReporterBuilder的实例,并在 中使用它
Configuration configuration = new YourConfiguration().useStoryReporterBuilder(newStoryReporterBuilder....)

现在在Jbehave中以不同的方式配置。

所以,为了达到这个目标,你需要:

  1. 创建继承org. jbehavior .core.reporters. consoleoutput的新类。在这里,您可以修改各种方法。在您的情况下-您需要重写方法:

    public void beforeScenario(String title)
    

    查看附在这篇文章末尾的例子,看看它是如何做到的。

  2. 创建抽象类org. jbehavior .core.reporters. filter:

    public static final Format YOUR_CUSTOM_CONSOLE = new Format("YOUR_CUSTOM_CONSOLE")
    {
        @Override
        public StoryReporter createStoryReporter(FilePrintStreamFactory factory,
                StoryReporterBuilder storyReporterBuilder) {
            return new TeamCityConsoleOutput(storyReporterBuilder.keywords()).doReportFailureTrace(
                    storyReporterBuilder.reportFailureTrace()).doCompressFailureTrace(
                    storyReporterBuilder.compressFailureTrace());
        }
    };
    
  3. 然后你需要将这个格式添加到你的故事构建器中,也就是你在配置中使用的格式,这意味着:

    new MostUsefulConfiguration()
                .useStoryReporterBuilder(
                        new StoryReporterBuilder()
                                ....//here are another modifications of sorey report builder
                                .withFormats(YOUR_CUSTOM_CONSOLE , .../* another formats */ HTML, Format.XML, Format.TXT))
                ....//here are another modifications of configuration
                .useStepMonitor(new CrossReference().getStepMonitor());
    

下面是这样修改的例子,可以用来与TeamCity集成:https://github.com/jbehave/jbehave-core/blob/d15774bf763875662869cdc89ce924b1086af6f8/jbehave-core/src/main/java/org/jbehave/core/reporters/TeamCityConsoleOutput.java

最新更新