Maven unit test report and test coverage, maven unit test
For junit unitsTestReport: similar results
------------------------------------------------------- T E S T S-------------------------------------------------------Running com.cn.qin.actionTest.UserActionTestsdffsdfsdfTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.061 sec - in com.cn.qin.actionTest.UserActionTestResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0[INFO] [INFO] <<< cobertura-maven-plugin:2.5.1:cobertura (cobertura-report) < [cobertura]test @ Struts <<<[INFO] [INFO] --- cobertura-maven-plugin:2.5.1:cobertura (cobertura-report) @ Struts ---[INFO] Cobertura 1.9.4.1 - GNU GPL License (NO WARRANTY) - See COPYRIGHT fileCobertura: Loaded information on 3 classes.Report time: 298ms
The running report is output by junit itself, which is similar to the report we run in Eclipse. The preceding example shows that three use cases are run, and 0 are not in line with the expected results. The number of failed use cases is 0, and the number of ignored use cases is 0. SkipUnit Test, You can run the following command
1. mvn package-DskipTestsYou may ask, why can Maven search for the test classes we have compiled on its own? In fact, the sentence is later than the configuration. Maven automatically searches for classes under src/test/java. When the classes under this folder comply with the following specifications, Maven considers them as units by default.
Test CasesClass. Test *. java: any directory
Test* Test. java: the class ending with Test in any directory * TestCase. java: the class ending with TestCase in any directory. If you want to save project building time within a period of time, ignore unit tests for the moment. You can configure the following in pom. xml:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.5</version> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build>
After the project is fully developed, comment out the test cases.
This module has two test case classes. What should I do if I only want to run a test case. Run the following command: test-Dtest = AccountImageServiceImplTest, which specifies the test case to run. Of course, you need to comment out the configuration that ignores the test case in the pom file. You can also test multiple test cases: mvn Test-Dtest = AccountImageServiceImplTest and AccountImageUtilTest. You can also use fuzzy match to test mvn test-Dtest = * Test.
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.5</version> <configuration> <includes> <include>**/*Test.java</include> </includes> <excludes> <exclude>**/AccountImageUtilTest.java</exclude> </excludes> </configuration> </plugin> </plugins></build>
Among them, the primary des is the class to be tested, and the excludes is the test case to be excluded. Fuzzy match can be used. ** Used to match any component path, * used to match any class.
Configure pom. xml in the unit test report of Junit
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-report-plugin</artifactId> <version>2.12.2</version> <configuration> <showSuccess>false</showSuccess> </configuration> </plugin> </plugins></build>
The default report is txt. to generate an html report, run the following command: mvn surefire-report: report. this will generate an html report under the target/site. After testing, we found that maven-surefire-plugin can generate the txt and xml test results, maven-surefire-report-plugin is required for html reports.
4. Test reportThe basic test report has been described above. Let's take a look at the test coverage report. Run the following command: mvn cobertura: cobertura and its pom. xml configuration
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.5.1</version> </plugin> </plugins></build>
Common commands mvn cobertura: help view the help of the cobertura plug-in mvn cobertura: clean clear the running result of the cobertura plug-in mvn cobertura: check the check task of running cobertura mvn cobertura: cobertura runs the cobertura check task and generates a report. The report is generated in the cobertura: dump-datafile cobertura Datafile Dump Mojo mvn Cobertura directory under the target/site/cobertura directory: instrument Instrument the compiled classes has a site directory under the target folder. Below is a static site, which contains the coverage report of unit test. Detailed configuration can also refer to: http://zhanshenny.iteye.com/blog/1440571
5. SummaryThis time we introduced the Maven test, which can run the unit test cases of the project and generate a report. You can configure test options as needed to meet the test requirements of the project. I tested the pom. xml configuration plug-in myself.
<Build> <plugins> <plugin> <groupId> org. apache. maven. plugins </groupId> <artifactId> maven-surefire-plugin </artifactId> <version> 2.19.1 </version> <configuration> <! -- Set the included test classes --> <shortdes> <include> ******* </include> <include> */User * </include> </shortdes> <! -- Set no Test class --> <excludes> <exclude> Test * </exclude> </excludes> <! -- Skip the test phase, and errors may occur if there is a problem with writing the sequence class. This is generally not recommended. --> <! -- <Skip> true </skip> --> </configuration> </plugin> <! -- Build the project site report plug-in --> <plugin> <groupId> org. apache. maven. plugins </groupId> <artifactId> maven-site-plugin </artifactId> <version> 3.0-beta-3 </version> <configuration> <! -- Configure the site internationalization --> <locales> zh_CN </locales> <! -- Output code --> <outputEncoding> GBK </outputEncoding> </configuration> </plugin> <! -- Project API doc report --> <plugin> <groupId> org. apache. maven. plugins </groupId> <artifactId> maven-javadoc-plugin </artifactId> <version> 2.7 </version> </plugin> <! -- Unit test report html --> <plugin> <groupId> org. apache. maven. plugins </groupId> <artifactId> maven-surefire-report-plugin </artifactId> <version> 2.12.2 </version> <configuration> <showSuccess> false </showSuccess> </ configuration> </plugin> <! -- Test coverage report --> <plugin> <groupId> org. codehaus. mojo </groupId> <artifactId> cobertura-maven-plugin </artifactId> <version> 2.5.1 </version> <configuration> <formats> <format> html </format> <format> xml </format> </formats> </configuration> <executions> <execution> <id> cobertura-report </id> <goals> <goal> cobertura </ goal> </goals> <phase> test </phase> </execution> </executions> </plugin> </plugins> </build>
The generated test coverage rate is in the target/site/cobertura/of your project. The path below is file: ///D:/InstallSoft/V2/workspace/user-parent/Struts/target/site/cobertura/Index.html