Maven by Example - 4.12. Executing Unit Tests |
|
Now that your project has unit tests, let’s run them. You don’t have
to do anything special to run a unit test; the $ mvn test ... [INFO] [surefire:test] [INFO] Surefire report directory: ~/examples/ch-custom/simple-weather/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.sonatype.mavenbook.weather.yahoo.WeatherFormatterTest 0 INFO YahooParser - Creating XML Reader 177 INFO YahooParser - Parsing XML Response 239 INFO WeatherFormatter - Formatting Weather Data Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.547 sec Running org.sonatype.mavenbook.weather.yahoo.YahooParserTest 475 INFO YahooParser - Creating XML Reader 483 INFO YahooParser - Parsing XML Response Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.018 sec Results : Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 Executing You will often find yourself developing on a system that has failing
unit tests. If you are practicing Test-Driven Development (TDD), you
might use test failure as a measure of how close your project is to
completeness. If you have failing unit tests, and you would still like
to produce build output, you are going to have to tell Maven to ignore
build failures. When Maven encounters a build failure, its default
behavior is to stop the current build. To continue building a project
even when the Surefire plugin encounters failed test cases, you’ll
need to set the Ignoring Unit Test Failures. <project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <testFailureIgnore>true</testFailureIgnore> </configuration> </plugin> </plugins> </build> [...] </project>
The plugin documents (http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html) show that this parameter declares an expression: Plugin Parameter Expressions. testFailureIgnore Set this to true to ignore a failure during testing. Its use is NOT RECOMMENDED, but quite convenient on occasion. * Type: boolean * Required: No * User Property: maven.test.failure.ignore
This property can be set from the command line using the $ mvn test -Dmaven.test.failure.ignore=true You may want to configure Maven to skip unit tests altogether. Maybe
you have a very large system where the unit tests take minutes to
complete and you don’t want to wait for unit tests to complete before
producing output. You might be working with a legacy system that has a
series of failing unit tests, and instead of fixing the unit tests,
you might just want to produce a JAR. Maven provides for the ability
to skip unit tests using the $ mvn install -Dmaven.test.skip=true ... [INFO] [compiler:testCompile] [INFO] Not compiling test sources [INFO] [surefire:test] [INFO] Tests are skipped. ... When the Surefire plugin reaches the Skipping Unit Tests. <project> [...] <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip>true</skip> </configuration> </plugin> </plugins> </build> [...] </project>
|