51Testing软件测试论坛

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

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

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

[求助] Junit4 参数化后测试执行顺序问题求教

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2013-10-15 15:23:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 六月长青 于 2013-10-15 15:42 编辑

使用Junit4进行参数化后测试。运行结果如下,每运行一个test,都会调用setup和tear:
  1. setUpBeforeClass
  2. constructed
  3. setUp
  4.   test1:111
  5. tear
  6. constructed
  7. setUp
  8.   test2:111
  9. tear
  10. constructed
  11. setUp
  12.   test1:222
  13. tear
  14. constructed
  15. setUp
  16.   test2:222
  17. tear
  18. tearDownAfterClass
复制代码
但是想要这样的结果,即每执行一轮参数时,所有test前后运行一次指定参数。要如何实现?setUpBeforeClass中不能获取参数化数据。所以不能放到这里。
  1. setUpBeforeClass
  2. setUptest
  3.   test1:111111
  4.   test2:111111
  5. teartest
  6. setUptest
  7.   test1:222222222
  8.   test2:222222222
  9. teartest
  10. tearDownAfterClass
复制代码
  1. @RunWith(Parameterized.class)  
  2. public class test {
  3.         private int str1;       
  4.        
  5.          @Parameters   
  6.      public static Collection prepareData(){  
  7.                 Object [][] object = {{111111},{222222222}};  
  8.                 System.out.println("prepareData");
  9.                 return Arrays.asList(object);  
  10.     }  
  11.        
  12.     public test(int s1){
  13.             this.str1=s1;
  14.             System.out.println("constructed");
  15.            
  16.     }

  17.         @BeforeClass
  18.         public static void setUpBeforeClass() throws Exception {
  19.                 System.out.println("setUpBeforeClass");
  20.         }

  21.         @AfterClass
  22.         public static void tearDownAfterClass() throws Exception {
  23.                 System.out.println("tearDownAfterClass");
  24.         }

  25.         @Before
  26.         public void setUp() throws Exception {
  27.                 System.out.println("setUp");
  28.         }

  29.         @After
  30.         public void tearDown() throws Exception {
  31.                 System.out.println("tear");
  32.         }

  33.         @Test
  34.         public void test1() {
  35.                 System.out.println("  test1:"+str1);
  36.                
  37.         }

  38.         @Test
  39.         public void test2() {
  40.                 System.out.println("  test2:"+str1);
  41.                
  42.         }
  43. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

  • TA的每日心情
    擦汗
    前天 09:00
  • 签到天数: 1025 天

    连续签到: 4 天

    [LV.10]测试总司令

    2#
    发表于 2014-6-17 15:58:45 | 只看该作者
    网上有些资料可以看的,估计这个很多人不怎么好说,也不确定。建议LZ查看下。并且帮LZ召唤下大神吧~
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

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

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

    GMT+8, 2024-9-28 07:31 , Processed in 0.079553 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

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