【问题标题】:Eclipse: how to update a JUnit test file with newly added method in the source file?Eclipse:如何使用源文件中新添加的方法更新 JUnit 测试文件?
【发布时间】:2011-08-24 14:46:56
【问题描述】:

使用 Eclipse (Helios),我可以使用 New -> JUnit Test Case -> Class under test...,创建源文件 ClassA.java 的 JUnit 测试文件 ClassATest.java,然后选择 ClassA 的所有方法待测试。

如果稍后我们向 ClassA 添加更多方法,我们如何轻松地在 ClassATest 中反映这种添加? (请不要复制/粘贴)。

【问题讨论】:

    标签: eclipse methods junit add


    【解决方案1】:

    一种解决方案是使用MoreUnit

    MoreUnit 安装到 Eclipse 后,可以右键单击新添加的方法(尚未进行单元测试),然后选择“生成测试”

    当然,如果始终遵循先写后测的方式,则不需要这种解决方案。然而实际上有时你并不清楚你想做什么,在这种情况下,你必须编写一些方法,使用它,然后重新思考并再次编码,直到你对代码感到满意并想要通过添加单元测试使其稳定。

    【讨论】:

    • MoreUnit 如何为我的类中新创建的方法添加新的测试方法到现有的 junit 测试类中? Generate Test 将生成一个新的测试类,但不会在现有测试类之上生成一个新的测试方法。我错过了什么?我正在使用 Eclipse Luna
    • @5YrsLaterDBA:在 Kepler 上,它确实会在现有测试类中生成新的测试方法。我用TestNG看到了它。也许 MoreUnit 的作者还没有提供 Luna 的版本?
    【解决方案2】:

    您应该考虑创建一个 JUnit 测试套件,它将在您指定的类中执行所有测试。因此,添加新的测试用例就像创建一个新类并将其添加到@Suite.SuiteClasses 列表一样简单(如下所示)。

    这是一个例子。

    示例 JUnit 测试套件类:

    @RunWith(Suite.class)
    @Suite.SuiteClasses({
        TestClassFoo.class
    })
    public class ExampleTestSuite {}
    

    示例测试用例类:

    public class TestClassFoo {
        @Test
        public void testFirstTestCase() {
            // code up test case
        }
    }
    

    【讨论】:

    • 嗨,Uperez,我的问题是关于自动更新 TestClassFoo 如果 ClassFoo 中有新方法。
    猜你喜欢
    • 1970-01-01
    • 2021-05-10
    • 2019-06-02
    • 2012-06-18
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多