Jbehave 学习笔记(二)

5 篇文章 0 订阅
4 篇文章 0 订阅
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
Story代码   收藏代码
  1. Meta:  
  2. @author liuxianning  
  3. @theme  student  
  4.   
  5. Narrative: In order to get a new student,as a teacher, I want to add a student into the Class  
  6.   
  7. Scenario: Add a student into the class  
  8.   
  9. Given There is a student  
  10. And his name is 'Lincoln'  
  11. And his age is '18'  
  12. And his hobby is 'basketball'  
  13. And his father's name is 'Mike'  
  14. And his mother's name is 'Mary'  
  15. And his brother's name is 'Luis'  
  16. When system add the student into class  
  17. Then we can get student 'Lincoln2' from class  

2)、写Story对应的Steps类
Java代码   收藏代码
  1. public class StudentSteps {  
  2.     private ClassRoom classRoom;  
  3.     private Student student;  
  4.   
  5.     @Given("There is a student")  
  6.     public void initStudent() {  
  7.         student = new Student();  
  8.     }  
  9.   
  10.     @Given("his name is '$name'")  
  11.     public void setName(@Named("name")String name) {  
  12.         student.setName(name);  
  13.     }  
  14.   
  15.     @Given("his age is '$age'")  
  16.     public void setAge(@Named("age")Integer age) {  
  17.         student.setAge(age);  
  18.     }  
  19.   
  20.     @Given("his hobby is '$hobby'")  
  21.     public void setHobby(@Named("hobbu")String hobby) {  
  22.         student.setHobby(hobby);  
  23.     }  
  24.   
  25.     @Given("his father's name is '$fatherName'")  
  26.     public void setFatherName(@Named("fatherName")String fatherName) {  
  27.         student.setFatherName(fatherName);  
  28.     }  
  29.   
  30.     @Given("his mother's name is '$motherName'")  
  31.     public void setMotherName(@Named("motherName")String motherName) {  
  32.         student.setMotherName(motherName);  
  33.     }  
  34.   
  35.     @Given("his brother's name is '$brotherName'")  
  36.     public void setBrotherName(@Named("brotherName")String brotherName) {  
  37.         student.setBrotherName(brotherName);  
  38.     }  
  39.   
  40.     @When("system add the student into class")  
  41.     public void addStudentIntoClass(){  
  42.         classRoom = new ClassRoom();  
  43.         classRoom.addStudent(student);  
  44.     }  
  45.   
  46.     @Then("we can get student '$studentName' from class")  
  47.     public void checkGetStudent(@Named("studentName")String studentName){  
  48.          assertThat(student, is(classRoom.getStudent(studentName)));  
  49.     }  
  50. }  

3)、配置运行环境
Java代码   收藏代码
  1. public class StudentStories extends JUnitStories {  
  2.   
  3.     public Configuration configuration() {  
  4.         return new MostUsefulConfiguration().useStoryLoader(new LoadFromClasspath(this.getClass()))  
  5.                 .useStoryReporterBuilder(new StoryReporterBuilder().withCodeLocation(codeLocationFromClass(this.getClass())));  
  6.   
  7.     }  
  8.   
  9.     @Override  
  10.     protected List<String> storyPaths() {  
  11.         return new StoryFinder().findPaths(codeLocationFromClass(this.getClass()),"**/*.story","");  
  12.   
  13.     }  
  14. }  

4)、执行结果,执行StudentStories类,即可得到运行结果。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值