Test conventions

From OpenNMS
Jump to: navigation, search

Differences

Unit tests Integration tests
What does it do? Tests a single unit of code quickly (<1s) Anything else
Enabled by default? Yes; use "-DskipTests=true" option to disable. No; use "-t" option to compile.pl or "-DskipITs=false"
When do they run in the Maven lifecycle? test goal, between compile and package integration-test and verify goals, between package and install
Dependencies No external dependencies Might depend on a Spring context, PostgreSQL database, DNS resolution, Internet connectivity, JNI/JNA, other applications (e.g.: Rscript)
Test file naming src/test/java/**/*Test.java src/test/java/**/*IT.java
Maven plugin Surefire Failsafe
Running a single test on the command-line ./compile.pl -Dtest=FooTest test ./compile.pl -t -Dit.test=FooIT verify
# (note: this will run all unit tests; add -Dtest=AnyRandomUnitTest to minimize)
Compile, ignoring test failures ./compile.pl -Dmaven.test.failure.ignore=true install ./compile.pl -t -Dmaven.test.failure.ignore=true install

What is a unit test?

  • Takes less than a second.
  • Calling InetAddress.getLocalHost() is okay (make sure that your system can resolve its own hostname to an IP address)

What is an integration test?

  • Anything that connects to a database
  • Anything that interacts with the network (Internet, local network, or localhost)
  • Anything that does DNS resolution

Naming conventions

  • Unit tests
    • Surefire Inclusions and Exclusions
    • OpenNMS pattern
      • "**/*Test.java" - includes all of its subdirectories and all java filenames that end with "Test".
    • Other patterns that we aren't using
      • "**/Test*.java" - includes all of its subdirectories and all java filenames that start with "Test".
      • "**/*TestCase.java" - includes all of its subdirectories and all java filenames that end with "TestCase".
  • Integration tests
    • Failsafe Inclusions and Exclusions
    • OpenNMS pattern
      • "**/*IT.java" - includes all of its subdirectories and all java filenames that end with "IT".
    • Other patterns that we aren't using
      • "**/IT*.java" - includes all of its subdirectories and all java filenames that start with "IT".
      • "**/*ITCase.java" - includes all of its subdirectories and all java filenames that end with "ITCase".

Setting up your system for running integration tests

  • PostgreSQL needs to be running with the normal trust setting used with OpenNMS.
  • An opennms user exists in PostgreSQL along with an opennms database. E.g.:
    • ( cd / && sudo -u postgres createuser --superuser opennms )
    • ( cd / && sudo -u postgres createdb -E UTF8 opennms )
  • Ensure you have rrdtool installed and the rrdtool binary in your path and available (or linked to) /usr/local/bin/rrdtool (newts-repository-converter expects it here).
  • Ensure you have R installed and the Rscript binary somewhere in your path