自动将测试从JUnit 3迁移到JUnit 4的最佳方法是什么? [英] Best way to automagically migrate tests from JUnit 3 to JUnit 4?
问题描述
我有一堆JUnit 3类扩展了TestCase,并希望自动将它们迁移到带有注释的JUnit4测试,例如 @Before
, @After
, @Test
等等。
任何工具都可以在大批量运行中执行此操作?
I have a bunch of JUnit 3 classes which extend TestCase and would like to automatically migrate them to be JUnit4 tests with annotations such as @Before
, @After
, @Test
, etc.
Any tool out there to do this in a big batch run?
推荐答案
在我看来,它不会那么难。所以让我们试试吧:
In my opinion, it cannot be that hard. So let's try it:
您需要导入三个注释:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;`
完成下几个更改后,你不需要 import junit.framework.TestCase;
。
After you've made the next few changes, you won't need import junit.framework.TestCase;
.
所有以开头的方法public void test
必须以 @Test
注释开头。
使用正则表达式可以轻松完成此任务。
All methods beginning with public void test
must be preceded by the @Test
annotation.
This task is easy with a regex.
Eclipse生成以下 setUp()
方法:
Eclipse generates following setUp()
method:
@Override
protected void setUp() throws Exception { }
必须替换为:
@Before
public void setUp() throws Exception { }
同样的泪水()
:
@Override
protected void tearDown() throws Exception { }
替换为
@After
public void tearDown() throws Exception { }
3。摆脱 extends TestCase
删除字符串中每个文件的一个出现
3. Get rid of extends TestCase
Remove exactly one occurence per file of the string
" extends TestCase"
4 。删除主要方法?
可能需要删除/重构将执行测试的现有主要方法。
4. Remove main methods?
Probably it's necessary to remove/refactor existing main methods that will execute the test.
根据对于saua的评论,必须转换 suite()
方法。谢谢,saua!
According to saua's comment, there must be a conversion of the suite()
method. Thanks, saua!
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestDog.class
TestCat.class
TestAardvark.class
})
结论
我认为,通过一组正则表达式可以很容易地完成,即使它会杀死我的大脑;)
Conclusion
I think, it's done very easy via a set of regular expressions, even if it will kill my brain ;)
这篇关于自动将测试从JUnit 3迁移到JUnit 4的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!