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

编程入门 行业动态 更新时间:2024-10-24 08:29:53
本文介绍了自动将测试从JUnit 3迁移到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的最佳方法是什么?

本文发布于:2023-10-15 01:08:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492795.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   方法   JUnit

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!