Maven is configured to generate unit test report configuration

Report to junit unit test :
  1.  -------------------------------------------------------
  2.   T E S T S
  3.  -------------------------------------------------------
  4.  Running com.liuyan.account.mail.AccountImageServiceImplTest
  5.  ---------------------------------1990
  6.  Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.926 sec
  7.  Running com.liuyan.account.mail.AccountImageUtilTest
  8.  Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 sec
  9.
  10.  Results :
  12.  Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
  The running report is the output of junit's own report, which is similar to the report we run in Eclipse. The above means that 3 use cases have been run, 0 are inconsistent with the expected effect, 0 failed use cases, and 0 ignored use cases.
  If you need to skip unit tests , you can run the following command
   1.  mvn package -DskipTests
  You may ask, why is Maven able to find the test classes we wrote by itself? In fact, it is still the sentence that the convention is greater than the configuration. Maven automatically looks for the classes under src/test/java. When the classes under this folder conform to the following specifications, Maven considers them to be unit test case classes by default.
  Test*.java: any class starting with Test in any directory
  *Test.java: any class that ends with Test in any directory
  *TestCase.java: Any class that ends with TestCase in any directory.
  If you want to save project build time for a while, ignore unit tests altogether for now. Then you can configure the following in pom.xml
1.  <build>
2.      <plugins>
3.          <plugin>
4.              <groupId>org.apache.maven.plugins</groupId>
5.              <artifactId>maven-surefire-plugin</artifactId>
6.              <version>2.5</version>
7.              <configuration>
8.                  <skipTests>true</skipTests>
9.              </configuration>
10.          </plugin>
11.      </plugins>
12.  </build>
  When the project is completely developed, you can comment out the test case when you need it.
 This module has two test case classes, what if you only want to run one test case. run the following command
  1.  test -Dtest=AccountImageServiceImplTest
  This is to specify which test case to run. Of course, you need to comment out the configuration that ignores test cases in the pom file.
  Can also test multiple test cases
  1.  mvn test -Dtest=AccountImageServiceImplTest,AccountImageUtilTest
  Can also be tested using fuzzy matching
  1. mvn test -Dtest = * Test
1.  <build>
2.      <plugins>
3.          <plugin>
4.              <groupId>org.apache.maven.plugins</groupId>
5.              <artifactId>maven-surefire-plugin</artifactId>
6.              <version>2.5</version>
7.              <configuration>
8.                  <includes>
9.                      <include>**/*Test.java</include>
10.                  </includes>
11.                  <excludes>
12.                      <exclude>**/AccountImageUtilTest.java</exclude>
13.                  </excludes>
14.              </configuration>
15.          </plugin>
16.      </plugins>
17.  </build>
  includes is the class that needs to be tested, and excludes is the test case to be excluded. Fuzzy matching can be used. ** is used to match any path, * matches any class.
  Unit test report from Junit:
<reporting>
<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>
</reporting>
  The default generated report is txt. To generate an html report, you need to use the command mvn surefire-report:report. This will generate an html report under target/site
  Later, after testing, it was found that maven-surefire-plugin can already generate txt and xml test results. If you want html reports, you need maven-surefire-report-plugin
   4. Test report
  The basic test report has been introduced above, let's take a look at the test coverage report. Run the following command
  1. mvn cover: cover
  pom configuration
<project>
...
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.5.1</version>
</plugin>
</plugins>
</reporting>
...
</project>
  常用命令
  mvn cobertura:help          查看cobertura插件的帮助
  mvn cobertura:clean         清空cobertura插件运行结果
  mvn cobertura:check         运行cobertura的检查任务
  mvn cobertura:cobertura     运行cobertura的检查任务并生成报表,报表生成在target/site/cobertura目录下
  cobertura:dump-datafile     Cobertura Datafile Dump Mojo
  mvn cobertura:instrument    Instrument the compiled classes
  在target文件夹下出现了一个site目录,下面是一个静态站点,里面就是单元测试的覆盖率报告。
  详细配置还可参考:http://zhanshenny.iteye.com/blog/1440571
   5.  总结
  这次我们介绍了Maven的测试,可以运行项目的单元测试用例,并生成报告。使用者可以根据自己的需要配置测试选项以满足项目的测试需求。最后说一下,测试十分重要,往往大手笔的产品测试人员和开发人员的比例是2:1。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325524257&siteId=291194637