当前位置:  开发笔记 > 编程语言 > 正文

自动将测试从JUnit 3迁移到JUnit 4的最佳方法是什么?

如何解决《自动将测试从JUnit3迁移到JUnit4的最佳方法是什么?》经验,为你挑选了2个好方法。

我有一帮其扩展TestCase,并希望自动迁移他们是JUnit4测试与注释如JUnit 3班的@Before,@After,@Test
任何工具赫然出现在大批量运行做到这一点?



1> guerda..:

在我看来,它不会那么难.所以让我们试试吧:

0.进口

您需要导入三个注释:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;`

在完成下几个更改后,您将不需要import junit.framework.TestCase;.

1.注释test*方法

所有以方法开头的方法都public void test必须以@Test注释开头.正则表达式很容易完成这项任务.

2.注释SetUp和TearDown方法

Eclipse生成以下setUp()方法:

@Override
protected void setUp() throws Exception { }

必须替换为:

@Before
public void setUp() throws Exception { }

同样的tearDown():

@Override
protected void tearDown() throws Exception { }

取而代之

@After
public void tearDown() throws Exception { }

3.摆脱 extends TestCase

删除字符串的每个文件只发生一次

" extends TestCase"

4.删除主要方法?

可能有必要删除/重构将执行测试的现有主要方法.

5.将suite()方法转换为@RunWithClass

根据saua的评论,必须转换suite()方法.谢谢,saua!

@RunWith(Suite.class)
@Suite.SuiteClasses({
  TestDog.class
  TestCat.class
  TestAardvark.class
})

结论

我认为,通过一组正则表达式可以很容易地完成它,即使它会杀死我的大脑;)


我将suite()方法的转换添加到@RunWith(Suite.class)@SuiteCkasses()
setUp(),tearDown()方法必须在JUnit4中公开

2> Geoffrey De ..:

以下是我用来执行furtelwart建议的实际正则表达式:

// Add @Test
Replace:
^[ \t]+(public +void +test)
With:
    @Test\n    $1
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Remove double @Test's on already @Test annotated files
Replace:
^[ \t]+@Test\n[ \t]+@Test
With:
    @Test
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove all empty setUp's
Replace:
^[ \*]+((public|protected) +)?void +setUp\(\)[^\{]*\{\s*(super\.setUp\(\);)?\s*\}\n([ \t]*\n)?
With nothing
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Add @Before to all setUp's
Replace:
^([ \t]+@Override\n)?[ \t]+((public|protected) +)?(void +setUp\(\))
With:
    @Before\n    public void setUp()
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Remove double @Before's on already @Before annotated files
Replace:
^[ \t]+@Before\n[ \t]+@Before
With:
    @Before
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove all empty tearDown's
Replace:
^[ \*]+((public|protected) +)?void +tearDown\(\)[^\{]*\{\s*(super\.tearDown\(\);)?\s*\}\n([ \t]*\n)?
With nothing
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Add @After to all tearDown's
Replace:
^([ \t]+@Override\n)?[ \t]+((public|protected) +)?(void +tearDown\(\))
With:
    @After\n    public void tearDown()
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java

// Remove double @After's on already @After annotated files
Replace:
^[ \t]+@After\n[ \t]+@After
With:
    @After
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove old imports, add new imports
Replace:
^([ \t]*import[ \t]+junit\.framework\.Assert;\n)?[ \t]*import[ \t]+junit\.framework\.TestCase;
With:
import org.junit.After;\nimport org.junit.Before;\nimport org.junit.Test;\nimport static org.junit.Assert.*;
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java


// Remove all extends TestCase
Replace:
[ \t]+extends[ \t]+TestCase[ \t]+\{
With:
 {
Regular Expression: on
Case sensitive: on
File name filter:
*Test.java



// Look for import junit.framework;
Find:
import junit\.framework
Manually fix
Regular Expression: on
Case sensitive: on


// Look for ignored tests (FIXME, disabled, ...)
Find:
public[ \t]+void[ \t]+\w+test
Manually fix
Regular Expression: on
Case sensitive: on


// Look for dummy/empty tests
Find:
public[ \t]+void[ \t]+test[\w\d]*\(\s*\)\s*\{\s*(//[^\n]*)?\s*\}
Manually fix
Regular Expression: on
Case sensitive: on

注意:按照上面显示的顺序执行它们非常重要.

推荐阅读
pan2502851807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有