【问题标题】:How to run all my inner class junit tests at once如何一次运行我所有的内部类junit测试
【发布时间】:2012-01-31 10:10:08
【问题描述】:

我已经按照描述使用每个方法使用一个内部类来组织我的 junit 测试:

Here, in a haacked article (about nunit testing),和
Here, in this SO question

public class TestMyThing {

    public static class TestMyThingMethodOne {

        @Test
        public void testMethodOneDoesACertainBehaviour() {
            // terse, clear testing code goes here
        }

        @Test
        public void testMethodOneHasSomeOtherDesiredBehaviour() {
            // more inspiring testing code here
        }
    }

    public static class TestMyThingMethodTwo {
        ... etc
    }
}

但是,当我尝试为此运行 junit 测试时,Eclipse 会询问我要运行哪个内部类测试,而我只能选择一个。有没有办法可以指定我希望每个内部类中的每个测试都运行 TestMyThing 类?

【问题讨论】:

    标签: java eclipse junit


    【解决方案1】:

    我在this post 的Eclipse 论坛上提出了我的问题,结果证明Eclipse 可以运行所有内部类。我引用了我已经尝试过的回复,并且可以确认它运行良好(即,我可以使用一个命令调用内部类中的所有测试):

    JUnit 4 带有一个名为 Enclosed 的运行器类,它可以满足您的需要。

    用注释外部类 @RunWith(Enclosed.class) 附上 = 导入 org.junit.experimental.runners.Enclosed;

    【讨论】:

      【解决方案2】:

      通常您只需创建单独的类(只是带有默认构造函数的 pojos),而不是将不同的单元测试打包到单个类中。由于您的内部类是静态的,因此这样做没有真正的优势。

      【讨论】:

      • +1。您还可以补充一点,大多数工具,例如 maven、ant、Eclipse 也不能很好地处理这个问题。
      • 嗯,好的。遗憾的是 Eclipse 不能很好地处理它,因为它非常漂亮和整洁,并且将一个方法的所有测试放在一起,而一个类的所有测试仍然只有一个类。不过我很感兴趣,当你说“通常你只会创建单独的类......”你的意思是我正在测试的每种方法都有单独的类吗?当您说“不同的单元测试”时,您如何定义它?单元测试是您为一种方法所做的所有测试吗?谢谢你的回答:)
      • 我倾向于为每个被测类创建单独的测试类。如果需要的话,我还可以添加一些额外的测试类。
      猜你喜欢
      • 1970-01-01
      • 2011-01-30
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 2011-01-03
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多