jenkins the definitive guide

(Jeff_L) #1

Appendix A. Automating Your Unit


and Integration Tests


A.1. Automating Your Tests with Maven


Maven is a popular open source build tool of the Java world, that makes use of practices such as
declarative dependencies, standard directories and build life cycles, and convention over configuration
to encourage clean, maintainable, high level build scripts. Test automation is strongly supported in
Maven. Maven projects use a standard directory structure: it will automatically look for unit tests in a
directory called (by default) src/test/java. There is little else to configure: just add a dependency
to the test framework (or frameworks) your tests are using, and Maven will automatically look for and
execute the JUnit, TestNG, or even Plain Old Java Objects (POJO) tests contained in this directory
structure.


In Maven, you run your unit tests by invoking the test life cycle phase, as shown here:


$ mvn test
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building Tweeter domain model
[INFO] task-segment: [test]
[INFO] ------------------------------------------------------------------------
...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running com.wakaleo.training.tweeter.domain.TagTest
Tests run: 13, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.093 sec
Running com.wakaleo.training.tweeter.domain.TweeterTest
Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.021 sec
Running com.wakaleo.training.tweeter.domain.TweeterUserTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec
Running com.wakaleo.training.tweeter.domain.TweetFeeRangeTest
Tests run: 10, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 sec
Running com.wakaleo.training.tweeter.domain.HamcrestTest
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.023 sec

Results :

Tests run: 38, Failures: 0, Errors: 0, Skipped: 0

In addition to executing your tests, and failing the build if any of the tests fail, Maven will produce a
set of test reports (again, by default) in the target/surefire-reports directory, in both XML and
text formats. For our CI purposes, it is the XML files that interest us, as Jenkins is able to understand
and analyze these files for its CI reporting:


$ ls target/surefire-reports/*.xml
Free download pdf