1、配置JBehave环境。
1)、添加JBehave依赖包,基本的只需要添加jbehave-core和Junit包就足够了。
2)、配置IntelliJ的JBehave插件,详情参见https://github.com/kumaraman21/IntelliJBehave/wiki
2、实现一个JBehave的Story基本过程
JBahave的测试用例主要由3部分组成,描述测试功能的story文件,story文件对应的Steps类和基于Junit的运行配置文件。Steps类是通过Anotation和正则匹配技术把story文件中的文本Step对应到Steps类中的方法,运行配置文件的主要作用是告诉junit去哪儿找story文件和Steps文件,把其配置在同一个Context中。实现一个story的基本过程如下:
1)、写Story
Meta:
@author liuxianning
@theme student
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 'Lincoln'
And his age is '18'
And his hobby is 'basketball'
And his father's name is 'Mike'
And his mother's name is 'Mary'
And his brother's name is 'Luis'
When system add the student into class
Then we can get student 'Lincoln2' from class
2)、写Story对应的Steps类
public class StudentSteps {
private ClassRoom classRoom;
private Student student;
@Given("There is a student")
public void initStudent() {
student = new Student();
}
@Given("his name is '$name'")
public void setName(@Named("name")String name) {
student.setName(name);
}
@Given("his age is '$age'")
public void setAge(@Named("age")Integer age) {
student.setAge(age);
}
@Given("his hobby is '$hobby'")
public void setHobby(@Named("hobbu")String hobby) {
student.setHobby(hobby);
}
@Given("his father's name is '$fatherName'")
public void setFatherName(@Named("fatherName")String fatherName) {
student.setFatherName(fatherName);
}
@Given("his mother's name is '$motherName'")
public void setMotherName(@Named("motherName")String motherName) {
student.setMotherName(motherName);
}
@Given("his brother's name is '$brotherName'")
public void setBrotherName(@Named("brotherName")String brotherName) {
student.setBrotherName(brotherName);
}
@When("system add the student into class")
public void addStudentIntoClass(){
classRoom = new ClassRoom();
classRoom.addStudent(student);
}
@Then("we can get student '$studentName' from class")
public void checkGetStudent(@Named("studentName")String studentName){
assertThat(student, is(classRoom.getStudent(studentName)));
}
}
3)、配置运行环境
public class StudentStories extends JUnitStories {
public Configuration configuration() {
return new MostUsefulConfiguration().useStoryLoader(new LoadFromClasspath(this.getClass()))
.useStoryReporterBuilder(new StoryReporterBuilder().withCodeLocation(codeLocationFromClass(this.getClass())));
}
@Override
protected List<String> storyPaths() {
return new StoryFinder().findPaths(codeLocationFromClass(this.getClass()),"**/*.story","");
}
}
4)、执行结果,执行StudentStories类,即可得到运行结果。
《未完待续》
分享到:
相关推荐
jsemver.zip,semver规范的java实现semver规范的java实现
jbehave-tutorial-maste
jbehave core用于BDD Java自动化代码开发jar包
jbehave4-scenario-restart 此maven项目重现了JBehave 4中的问题,并且在引发RestartingScenarioFailure Exception之后,未重新RestartingScenarioFailure Scenario。 使用JBehave 4.0.1运行 只需执行mvn clean ...
jbehave4-step-priority PerformableTree正在添加由于元过滤器而被禁止的ExamplePerformableScenario实例。 这会导致大量调用StepFinder.prioritise()方法,从而导致性能下降,尤其是在使用Levenshtein Distance时...
gitter-scala.zip,纯功能scala gitter api客户端纯功能gitter api客户端
jbehave-spring-sample
scala-time.zip,scalaj timea joda time的scala包装器
cucumber-reporting.zip,这个项目为cucumber提供了漂亮的html报告。它的工作原理是从cucumber json生成html
jbehave-testng-example完整的入门专案与范例一个简单加法器BDD范例使用JBehave + TestNG
jbehave-bigdecimal-converter 这个maven项目演示了JBehave允许BigDecimal(NumberConverter)使用的不良用法。使用Maven运行从终端,只需执行: mvn clean install 由于java.lang.RuntimeException: ********** ...