51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1793|回复: 2

[转贴] 【JBehave】集成spring测试

[复制链接]

该用户从未签到

发表于 2019-2-13 14:43:30 | 显示全部楼层 |阅读模式

通过研究网上的教程(这儿说一句,网上关于jbehave的教程确实有点少)和官网的文档,完成了基于jbehave和spring的整合测试案例,现将相关的代码发出来,供大家。

博主后期应该会大量使用jbehave,大家有什么问题可以一起交流。


1、根据测试场景,编写相应的story。

  1. Meta:

  2. Narrative:
  3. As a user
  4. I want to login into system and i will own my right

  5. Scenario: system admin login
  6. Given uid is v3admin@v3 and password is 123456
  7. When i login
  8. Then i have role manager


  9. Scenario: company admin login
  10. Given uid is chenshengli@zyqckjcd and password is 123456
  11. When i login
  12. Then i have role company_manager
复制代码

在这个story中,包含两个场景的测试,其一是系统管理员登陆,其二是公司管理员登陆


2、编写对应的step java

  1. @Component
  2. public class LoginSteps {

  3.     private String uid;

  4.     private String password;

  5.     private User user;

  6.     @Autowired
  7.     private UserService userService;

  8.     @Given("uid is $uid and password is $password")
  9.     public void init(String uid, String password) {
  10.         this.uid = uid;
  11.         this.password = password;
  12.     }


  13.     @When("i login")
  14.     public void login() throws V3Exception {
  15.         System.out.println(uid + "," + password);
  16.         this.user = userService.login(uid, password);
  17.     }

  18.     @Then("i have role $role")
  19.     public void check(String role) {
  20.         assertTrue(user.getRoles().contains(role));
  21.     }

  22. }
复制代码

因为要集成spring,所以该类加了注解@Component,以便执行时能自动扫描到。

因为集成了spring,所以需要的userService可以通过注解@Autowired自动获取到。


3、测试主类

  1. public class SpringTestRunner extends JUnitStories {

  2.     private ApplicationContext context;

  3.     public SpringTestRunner() {
  4.     }

  5.     @Override
  6.     public Configuration configuration() {
  7.         return new MostUsefulConfiguration()
  8.                 .useStoryParser(new RegexStoryParser(new ExamplesTableFactory(new LoadFromClasspath(this.getClass()))))
  9.                 .useStoryReporterBuilder(new StoryReporterBuilder().withRelativeDirectory("doc/jbehave")
  10.                         .withCodeLocation(CodeLocations.codeLocationFromClass(getClass())).withDefaultFormats()
  11.                         .withFormats(CONSOLE, TXT, HTML, XML));
  12.     }

  13.     @Override
  14.     protected List<String> storyPaths() {
  15.         List<String> result = new StoryFinder().findPaths(CodeLocations.codeLocationFromClass(this.getClass()), "**/*.story", "");
  16.         System.out.println("storyPaths:" + result);
  17.         return result;
  18.     }


  19.     public InjectableStepsFactory stepsFactory() {
  20.         return new SpringStepsFactory(configuration(), context());
  21.     }

  22.     private ApplicationContext context() {
  23.         if (context == null) {
  24.             context = new SpringApplicationContextFactory("v3-market.xml").createApplicationContext();
  25.         }
  26.         return context;
  27.     }
  28. }
复制代码

因为是要执行所有的story测试,所以该类扩展了JUnitStories,并重写了storyPaths 方法,根据自己的情况发现并加载相应的story文件。


4、执行

JUnitStories有一个注解为@Test的run(),所以SpringTestRunner是可以直接执行的junit类,通过执行该类,将会扫描所有的story并进行相应的测试,最后生成测试报告。


5、测试报告

生成报告文件列表如下,主要的测试通过view中的index.html查看。

详细的测试结果,因为没有将jbehave-site-resources和jbehave-core-resources.zip中的内容解压到view目录下,所有看到的测试报告页面有点丑。



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

x
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-3-29 21:50 , Processed in 0.064881 second(s), 25 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表