首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自动将测试从JUnit 3迁移到JUnit 4的最佳方法是什么?

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

Stack Overflow用户
提问于 2008-11-05 09:32:46
回答 7查看 17.7K关注 0票数 50

我有一堆扩展TestCase的JUnit 3类,并希望自动将它们迁移为带有注释的JUnit4测试,如@Before、@After、@Test等。

有没有什么工具可以在大批量运行中做到这一点?

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2009-03-24 13:19:44

在我看来,这不可能那么难。因此,让我们尝试一下:

0。导入

您需要导入三个注释:

代码语言:javascript
复制
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()方法:

代码语言:javascript
复制
@Override
protected void setUp() throws Exception { }

必须替换为:

代码语言:javascript
复制
@Before
public void setUp() throws Exception { }

tearDown()也是如此

代码语言:javascript
复制
@Override
protected void tearDown() throws Exception { }

替换为

代码语言:javascript
复制
@After
public void tearDown() throws Exception { }

3.摆脱extends TestCase

仅删除字符串的每个文件的一个匹配项

代码语言:javascript
复制
" extends TestCase"

4.删除main方法?

可能有必要移除/重构将执行测试的现有main方法。

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

根据the的评论,必须对suite()方法进行转换。谢谢你,索亚!

代码语言:javascript
复制
@RunWith(Suite.class)
@Suite.SuiteClasses({
  TestDog.class
  TestCat.class
  TestAardvark.class
})

结论

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

票数 58
EN

Stack Overflow用户

发布于 2010-12-27 03:45:11

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

代码语言:javascript
复制
// 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

注意:按照上面所示的顺序进行操作非常重要。

票数 37
EN

Stack Overflow用户

发布于 2013-05-24 17:10:08

我们正在将相当大的代码库迁移到JUnit4。由于这是我第二次做这样的迁移,我决定将代码保存在某个地方:

https://github.com/FranciscoBorges/junit3ToJunit4

它处理了比上面答案中列举的更多的转角情况。例如:

在子类constructor

  • calls中调用TestCase.setUp()和TestCase.tearDown()

  • calls to TestCase(String)构造函数的
  • 调用移动到Assert.
  • fixing包名称TestCase.setUp() to org.junit
  • etc

的TestCase.assert*方法

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/264680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档