星期四, 10月 03, 2024

控制Maven Unit Test及Integration Test

由於integration test不一定有環境跑,所以得區分
目標是unit test一定都會跑,而integration test有指定才跑
這裡利用maven-surefire及tag區分

1.Tag區分Unit及Integration Test

    @Test
    @Tag("UnitTest")
    @DisplayName("UnitTest1")
    public void testCase1() {
        assertEquals(1, 1);
    }

    @Test
    @Tag("UnitTest")
    @DisplayName("UnitTest2")
    public void testCase2() {
        assertEquals(1, 1);
    }

    @Test
    @Tag("IntegrationTest")
    @DisplayName("Integration")
    public void testCase3() {
        assertEquals(1, 1);
    }

2.maven-surefirem區分

在pom.xml裡,找plugins加入以下
...
<plugin>
	<groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-surefire-plugin</artifactid>
    <version>3.5.0</version>
    <executions>
        <!--mvn test-->
        <execution>
            <id>default-test</id>
            <phase>test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <!--run test cases with tag "UnitTest"-->
                <groups>UnitTest</groups>
            </configuration>
        </execution>
        
        <!--mvn integration-test-->
        <execution>
            <id>integration-test</id>
            <phase>integration-test</phase>
            <goals>
                <goal>test</goal>
            </goals>
            <configuration>
                <!--run test cases with tag "IntegrationTest"-->
                <groups>IntegrationTest</groups>
            </configuration>
        </execution>
    </executions>
</plugin>
 

跑mvn integration-test可以發現分別先跑了
1.mvn test
2.mvn integration-test

本想研究怎麼拿掉mvn test步驟
不過想想unit test每次都跑也不錯,就沒理了