51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1188|回复: 1
打印 上一主题 下一主题

Junit多策略测试切换

[复制链接]
  • TA的每日心情
    无聊
    5 天前
  • 签到天数: 41 天

    连续签到: 1 天

    [LV.5]测试团长

    跳转到指定楼层
    1#
    发表于 2018-1-31 14:37:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    本帖最后由 海鸥一飞 于 2018-1-31 14:58 编辑

           介绍                               
           最近在弄单元测试,之前基础版本的测试基本都是写好的,但是缺少高级版本的测试,

    又不想把代码copy一份,要就地实现基础版和高级版的测试复用,并且需要从配置读取测试策略来指
    定当前测试策略。因为基础版和高级版只是大部分功能差不多,因此还要做到通过注解的方式来
    限定当前测试方法或测试类是否运行于当前测试策略。下面废话少说,Let's Go!
      实践
      之前也没有好好深入junit,仔细想了一下,我们要做到以下两点:
      1.需要做到通过注解限定当前测试类或测试方法是否执行。这个使用Category注解可以实
    现,但是Category的作用太过于简单,仅仅只能做到限定测试是否执行。
      2.需要做到针对同一测试方法在不同测试策略下可以有不相同的处理,其中可能请求地址不一
    样,返回结果以及断言处理不一样等。这个之前最初考虑的时候太过于复杂,当时想实现自定义
    注解,然后通过aop拦截去改写请求参数和处理流程。这样做太过于复杂,而且不同测试策略不会
    同时运行。最后的做法就是将测试流程提取到接口,各自的测试策略自己去实现,然后在初始化
    时,根据当前测试策略去绑定对应的实例,测试使用时直接使用接口就可以实现多策略测试的切
    换,再结合第一点基本上就可以满足需求了。
      主要代码如下:
    1. package com.lux.junit;
    2. import com.lux.junit.category.BasicCategory;
    3. import com.lux.junit.category.SeniorCategory;
    4. import com.lux.junit.helper.BaseUserHelper;
    5. import org.junit.Test;
    6. import org.junit.experimental.categories.Category;
    7. import org.junit.runner.RunWith;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.boot.test.context.SpringBootTest;
    10. import org.springframework.test.context.junit4.SpringRunner;
    11. @RunWith(SpringRunner.class)
    12. @SpringBootTest
    13. public class JunitApplicationTests {
    14. @Autowired
    15. private BaseUserHelper userHelper;
    16. @Test
    17. public void contextLoads() {
    18. }
    19. @Test
    20. @Category({BasicCategory.class, SeniorCategory.class})
    21. public void getUserTest() {
    22. System.out.println(userHelper.getUser());
    23. }
    24. @Test
    25. @Category(SeniorCategory.class)
    26. public void getSeniorUserTest() {
    27. System.out.println(userHelper.getUser());
    28. }
    29. }
    30. package com.lux.junit.configure;
    31. import com.lux.junit.category.BasicCategory;
    32. import com.lux.junit.helper.BaseUserHelper;
    33. import com.lux.junit.helper.BasicUserHelper;
    34. import com.lux.junit.helper.SeniorUserHelper;
    35. import org.springframework.beans.factory.annotation.Value;
    36. import org.springframework.context.annotation.Bean;
    37. import org.springframework.context.annotation.Configuration;
    38. /**
    39. * Created with IntelliJ IDEA.
    40. * User: chenfeilong
    41. * Date: 2018/1/28
    42. * Time: 23:30
    43. * Description:
    44. */
    45. @Configuration
    46. public class CategoryConfigure {
    47. @Value("${test.category}")
    48. String currentCategory;
    49. @Bean("userHelper")
    50. public BaseUserHelper userHelper() {
    51. if (BasicCategory.class.getName().equals(currentCategory)) {
    52. return new BasicUserHelper();
    53. } else {
    54. return new SeniorUserHelper();
    55. }
    56. }
    57. }
    复制代码
           最后
      Category需要传递includeCategories参数,其实它可以是多个策略。通常使用命令行传入
    或使用gradle传入。但是我们需要从配置文件读入,需要像下面这样做。
    1. test {
    2. useJUnit {
    3. includeCategories getCategory()
    4. }
    5. }
    6. String getCategory() {
    7. def category = doGetCategory()
    8. println("current catagory: " + category)
    9. return category
    10. }
    11. String doGetCategory() {
    12. def propertiesFilePath = "src/main/resources/application.properties"
    13. def defaultCategory = "com.lux.junit.category.BasicCategory"
    14. File propFile = new File(propertiesFilePath)
    15. if (propFile.canRead()) {
    16. Properties props = new Properties()
    17. props.load(new FileInputStream(propFile))
    18. return props.getProperty("test.category")
    19. }
    20. println("can't find catagory config,use default catagory " + defaultCategory)
    21. return defaultCategory
    22. }
    复制代码









    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

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

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

    GMT+8, 2024-5-3 06:29 , Processed in 0.066880 second(s), 22 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

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