`

JBehave学习笔记(3)--高级特性介绍

阅读更多
1、Composite Steps
顾名思义就是能够把多个Steps组合为一个Step。例子
Meta:
@author liuxianning
@theme  composite

Narrative: This story is used to show composite steps.

Scenario: Add a student into the class

Given There is a student with default details
When system add the student into class
Then we can get student 'Lincoln2' from class


然后在StudentSteps中增加方法
    @Given("There is a student with default details")
    @Composite(steps = {
            "Given his name is 'Lincoln2'",
            "Given his age is '18'",
            "Given his hobby is 'basketball'",
            "Given his father's name is 'Mike'",
            "Given his mother's name is 'Mary'",
            "Given his brother's name is 'Luis'"
    })
    public void createStudentWithDefaultDetails() {
    }

这样就可以用一句话执行之前已经实现的很多步骤。

2、Tabular parameters
表结构参数,这是一个简化和结构化参数输入的特性,我们可以在一个步骤中把需要传递的多个参数结构化的传递给Step方法。例子:
Meta:
@author liuxianning
@theme  table

Narrative: In order to get a new student,as a teacher, I want to add a student into the Class

Scenario: Add a student into the class

Given There is a student  with details:
|name    |age |hobby      |father name |mother name |brother name|
|lincoln3|18  |basketball |Mike        |Mary        |Luis        |

When system add the student into class
Then we can get student 'lincoln3' from class

可以通过一个Table以键值对的形势来输入参数,在StudentSteps添加对应的方法来接收参数
@Given("There is a student  with details:$details")
    public void setDetails(@Named("details") ExamplesTable details) {
        student = new Student();
        Parameters parameters = details.getRowAsParameters(0);
        student.setName(parameters.valueAs("name",String.class));
        student.setAge(parameters.valueAs("age",Integer.class));
        student.setHobby(parameters.valueAs("hobby",String.class));
        student.setFatherName(parameters.valueAs("father name",String.class));
        student.setMotherName(parameters.valueAs("mother name",String.class));
        student.setBrotherName(parameters.valueAs("brother name",String.class));
    }


3、Examples
Scenario多实例,这个特性主要的目的在于可以使用不同的数据反复执行一个Scenario。例子:
Meta:
@author liuxianning
@theme  examples

Narrative: In order to get a new student,as a teacher, I want to add a student into the Class

Scenario: Add a student into the class

Given There is a student
And his name is '$name'
And his age is '$age'
When system add the student into class
Then we can get student '$studentName' from class

Examples:
|name|age|studentName|
|lxn1|18 |lxn1       |
|lxn2|19 |lxn2       |
|lxn3|20 |lxn3       |

该实例中回使用Examples中的数据反复执行该Scenario。

4、Meta Filtering
Meta信息过滤,这个特性允许JBehave指定运行特定的Story或者Scenario。使用方法为在配置Embedder的时候添加上MetaFilter属性。
public Embedder configuredEmbedder() {
        Embedder embedder = new Embedder();
        embedder.useConfiguration(configuration());
        embedder.useCandidateSteps(candidateSteps());
        embedder.useStepsFactory(stepsFactory());
      [color=red]  embedder.useMetaFilters(asList("+theme composite", "-skip"));[/color]
        return embedder;
    }

在标红的代码段中指明了只执行Meta段中属性theme为composite的Story。

5、GivenStories
Story依赖,这个特性让一个Story可以作为另一个Story的Scenario的前提条件。例子
Meta:
@author liuxianning
@theme  givenstories

Narrative: In order to maintian student's info,as a teacher, I want to update a student's name
Scenario: Add a student into the class
GivenStories: AddStudentIntoClass.story
Given Get student "Lincoln"
When system Update student name to "Lin"
Then we can get student 'Lin' from class
分享到:
评论
2 楼 ningandjin 2012-11-09  
fengcanfly 写道
Composite Steps
你写的这个run了以后组合的都是pengding啊!不知道是我写的问题,还是?求解
,你可以debug到Jbehave'的源代码里去看看,其实原理比较简单,Composite的标签会把里面的Steps的字符串数组,变成一个个的candidatestep,执行story的时候,用正则表达式匹配上了就执行。 你那边pending的原因就是你的story语句没有找到对应的step。这是我做练习时候的源码,你可以对比下有什么不同,具体你的情况我不太清楚https://github.com/xianlinbox/ChessDemo/tree/master/src/main/java/steps
1 楼 fengcanfly 2012-11-08  
Composite Steps
你写的这个run了以后组合的都是pengding啊!不知道是我写的问题,还是?求解

相关推荐

Global site tag (gtag.js) - Google Analytics