JUnit 註解@SuiteClasses的工作原理

i042416發表於2020-09-21

Suppose I have four test cases in my project, the total methods to be tested:


JUnit 註解@SuiteClasses的工作原理


Based on the blog  Run only given sets of your unit test via @Category, it is possible to organize test methods within THE SAME CLASS to different categories via @Category, that is, the granularity to control which test methods should be executed is method level.

There is another annotation @SuiteClasses which can allows us to categorize test classes into different test suites, and once we specify a given test suite to be executed, all test classes within that suite would be executed one by one.

For example, I create a suite TestSuite1and2 and only put first and second test case into it, so when this test suite is executed, only three test methods ( 1 from first test case and 2 from second test case ) are executed:


JUnit 註解@SuiteClasses的工作原理


And the same logic for TestSuite2and3:


JUnit 註解@SuiteClasses的工作原理


If you need to integrate test suite execution into Maven, add the following parts in pom.xml:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>${runSuite}</include>
                    </includes>
                </configuration>
            </plugin>

Then use the following command line:


JUnit 註解@SuiteClasses的工作原理


You will get exactly the same result as in Eclipse:


JUnit 註解@SuiteClasses的工作原理


要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":

JUnit 註解@SuiteClasses的工作原理


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2722717/,如需轉載,請註明出處,否則將追究法律責任。

相關文章