Junit 4 Core Concept [3] suit of test cases and suit of suits

来源:互联网 发布:算法的基本控制结构 编辑:程序博客网 时间:2024/05/16 10:40

All Contents are referenced from Junit In Action 2nd Edtion

the Suite is used for organizing and running one or more test cases.

the test runner automatically creates a Suite if you don’t provide one of your own.

The default Suite scans your test class for any methods that you annotated with @Test. Internally, the default Suite creates an instance of your test class for each @Test
 method. JUnit then executes every @Test method independently from the others to avoid potential side effects.

A test suite:

package test;import org.junit.runner.RunWith;import org.junit.runners.Suite;import org.junit.runners.Suite.SuiteClasses;//specify Suite Class as test runner@RunWith(value=Suite.class)//add test case in the SuiteClasses annotation separated by comma if there are multiple cases,//each test method with the test class will be included in the suite // SimpleTestCase is defined in the previous article Junit 4 Core Concept [2] @SuiteClasses(value={SimpleTestCase.class})public class AllTestCase {// no methods are needed}

execution result:

it’s possible to create a suite of test suites

package test;import org.junit.runner.RunWith;import org.junit.runners.Suite;import org.junit.runners.Suite.SuiteClasses;//specify Suite class as the test runner@RunWith(value=Suite.class)//add all test suite into SuiteClasses annotation@SuiteClasses(value={AllTestCase.class})public class AllTestSuites {// no methods are needed}

execution result

IDEs like Eclipse allow you to run all test classes and Suites in a selected package or source directory.

 This is enough to make us reconsider whether it’s worth creating JUnit Suites in the first place.

But JUnit Suites are useful if you want to organize your tests in Java, independent of the capability of your build system.



0 0