Skip to content
Jean-François Blanc edited this page Sep 22, 2016 · 1 revision

Context

Maven is based around the concept of a build lifecycle, the lifecycle is defined by a list of build phases, wherein a build phase represents a stage in the lifecycle.

The list of the existing phases can be found on the maven guide: Lifecycle Reference

There are 7 phases dedicated to the test of the project being built:

  1. generate-test-sources: generate any test source code for inclusion in compilation.
  2. process-test-sources: process the test source code, for example to filter any values.
  3. generate-test-resources: create resources for testing.
  4. process-test-resources: copy and process the resources into the test destination directory.
  5. test-compile: compile the test source code into the test destination directory
  6. process-test-classes: post-process the generated files from test compilation, for example to do bytecode enhancement on Java classes. For Maven 2.0.5 and above.
  7. test: run tests using a suitable unit testing framework. These tests should not require the code be packaged or deployed.

The nar-maven-plugin brings 3 goals dedicated to the test phases:

  • phase generate-test-sources: goal nar:nar-test-unpack
  • phase test-compile: goal nar:nar-testCompile
  • phase test: goal nar:nar-test

The aim of those goals is to build one or more executables which will be run during the test phase.

The documentation for those goals can be found in the nar-maven-plugin documentation: nar-maven-plugin test goals

Usage

Configuration for the goal nar-testCompile

Build options

To build the test executables, the configuration of the plugin can define some specific options for the compilation and the linking, see below the elements testOptions:

<configuration>
	...
	
	<c>
		<options>
			<!-- <option>...</option> -->
		</options>
		<testOptions>
			<!-- testOptions are added to regular options for test compilation -->
			<!-- <option>...</option> -->
		</testOptions>
	</c>
	
	<cpp>
		<options>
			<!-- <option>...</option> -->
		</options>
		<testOptions>
			<!-- testOptions are added to regular options for test compilation -->
			<!-- <option>...</option> -->
		</testOptions>
	</cpp>
	
	<linker>
		<options>
			<!-- No option if the library type is static and the "linker" is ar ! -->
			<!-- <option>...</option> -->
		</options>
		<testOptions>
			<!-- testOptions are added to regular options for test linking -->
			<!-- <option>...</option> -->
		</testOptions>
	</linker>
	
	...
</configuration>

Executables being built, and source files taken in account

The source files taken in account and the executables being built are dependant to the tests configuration.

Example, in src/test/c++ we have the following files:

  • utils.h
  • utils.cpp
  • test1.cpp
  • test2.cpp

And in the plugin configuration we have:

<configuration>
	...

	<tests>
		<test>
			<name>test1</name>
			<link>static</link>
			<run>true</run>
		</test>
		<test>
			<name>test2</name>
			<link>static</link>
			<run>true</run>
			<args>
				<arg>10</arg>
			</args>
		</test>
		<test>
			<name>test2</name>
			<link>static</link>
			<run>true</run>
			<args>
				<arg>20</arg>
			</args>
		</test>
	</tests>
</configuration>

2 test executables will be built, test1 and test2, the names are directly defined by the names of the tests (the executable test2 will be built only once even if there are 2 tests test2).

The executables will be built from the following source files:

  • test1: utils.cpp, test1.cpp
  • test2: utils.cpp, test2.cpp

Each executable will be built with all the test source files whose names don't correspond to the names of other tests. In the example above each executable will be built from 2 source files, a common file and a specific file, but we could have had only test1.cpp and test2.cpp and each executable would have been built from its source file only, or we could have had only a file main.cpp for example, and the 2 executables test1 and test2 would have been built only from this file (and would have been identical).

Configuration for the goal nar-test

Execution

The goal nar-test executes the test executables, and for each test we can specify the arguments to pass to the execution.

<configuration>
	...

	<tests>
		<test>
			<name>test1</name>
			<link>static</link>
			<run>true</run>
		</test>
		<test>
			<name>test2</name>
			<link>static</link>
			<run>true</run>
			<args>
				<arg>10</arg>
			</args>
		</test>
		<test>
			<name>test2</name>
			<link>static</link>
			<run>true</run>
			<args>
				<arg>20</arg>
			</args>
		</test>
	</tests>
</configuration>

test1 will be run once without argument.
test2 will be run once with the argument 10.
test2 will be run a second time with the argument 20.

Success / Failure

For each executable being run, nar-maven-plugin will check the return value of the execution, the value 0 is a success, a value different from 0 is a failure. If a test fails, the maven test phase stops there and the build of the project stops there on error.

Example

Example project

The project above is the example of this wiki page.
test1 and the first execution of test2 succeed, and the second execution of test2 fails:

[INFO] --- nar-maven-plugin:3.5.0:nar-test (default-nar-test) @ nar_tests ---
[INFO] Copied 0 test resources
[INFO] Running test test1 in /home/jfblanc/git/nar-test/target/test-nar/test-reports
[INFO] + cd /home/jfblanc/git/nar-test/target/test-nar/test-reports
[INFO] + /home/jfblanc/git/nar-test/target/test-nar/bin/amd64-Linux-gpp/test1
[INFO] Copied 0 test resources
[INFO] Running test test2 in /home/jfblanc/git/nar-test/target/test-nar/test-reports
[INFO] + cd /home/jfblanc/git/nar-test/target/test-nar/test-reports
[INFO] + /home/jfblanc/git/nar-test/target/test-nar/bin/amd64-Linux-gpp/test2 10
[INFO] Copied 0 test resources
[INFO] Running test test2 in /home/jfblanc/git/nar-test/target/test-nar/test-reports
[INFO] + cd /home/jfblanc/git/nar-test/target/test-nar/test-reports
[INFO] + /home/jfblanc/git/nar-test/target/test-nar/bin/amd64-Linux-gpp/test2 20
[WARNING] com.github.maven_nar.NarUtil$2@f08fdce
[WARNING] com.github.maven_nar.NarUtil$1@6bda1d19
[WARNING] com.github.maven_nar.NarUtil$3@28c86134
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.529 s
[INFO] Finished at: 2016-09-17T23:11:27+02:00
[INFO] Final Memory: 16M/249M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.github.maven-nar:nar-maven-plugin:3.5.0:nar-test (default-nar-test) on project nar_tests: exit code: 1 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException