English 中文(简体)
• 如何规定在“SuiteClasses in junit4”中增加哪些类别
原标题:How to specify which classes to be added in @SuiteClasses in junit4
  • 时间:2012-05-16 10:52:25
  •  标签:
  • junit
  • junit4

在Junit 4中,我们使用以下说明添加了试验套:

@RunWith(Suite.class)
@SuiteClasses({
A.class, B.class })
public class MyTestsuite
{
}

我的询问是,我们是否有任何办法具体确定条件,决定哪几类人想要列入@SuiteClasses。 例如,如果某一具体条件属实,请在上述法典中说一句话,否则就不希望将其添加到@SuiteClasses。

In junit 3.8 we do it using suite() method like mentioned below:

public class MyTestsuite extends TestCase
{
  public static Test suite()
  {
     TestSuite suite = new TestSuite();
     if(some_condition_true)
     {
       suite.addTest(A.suite);
     }
     suite.addTest(B.suite);
     return suite;
  }
}

难道我们能用第4号法官来做到这一点吗?

最佳回答

推广:

@RunWith(MySuite.class)//Customized
@SuiteClasses({
A.class, B.class })
public class MyTestsuite
{}

定义:

public class MySuite extends Suite {
           public MySuite(Class<?> klass, RunnerBuilder builder)
                    throws InitializationError {
                super(klass, builder);
                try {
                    filter(new Filter() {
                        @Override public boolean shouldRun(Description description) {
                            return some_condition_true? true : false;
                        }

                        @Override
                        public String describe() {
                            return "...";
                        }
                    });
                } catch (NoTestsRemainException e) {
                    System.out.println("Hey, all test cases are not satisfied your condition.");
                }
            }
        }

plus: Although it can work, I recommend keep the test case simple. It is hard to maintain complex thing.

问题回答

亲币的答案将奏效。

另一种选择是,总是增加试验类别,但在你的班级中则有一个假设(制约),它将决定何时操作,何时何谓。

(如果条件与测试先决条件有关,那么这种做法就更好:测试本身是了解它是否应当运行的最佳场所。)





相关问题
Run JUnit automatically when building Eclipse project

I want to run my unit tests automatically when I save my Eclipse project. The project is built automatically whenever I save a file, so I think this should be possible in some way. How do I do it? Is ...

Embedded jetty ServletTester serving single static file

I m unit testing with jetty and I want to serve not only my servlet under test but a static page as well. The static page is needed by my application. I m initializing jetty like this tester = new ...

Applying one test to two separate classes

I have two different classes that share a common interface. Although the functionality is the same they work very differently internally. So naturally I want to test them both. The best example I ...

热门标签